【问题标题】:How to make a table in C using loops如何使用循环在 C 中制作表格
【发布时间】:2015-02-17 04:16:25
【问题描述】:

您如何制作如图所示的表格(您使用的是理想气体定律)。音量值应该从最左边的列开始,开始音量并以相等的步长增加 这样最右边一列的成交量就是停止成交量。对于表中的每个条目,计算给定温度和体积下的压力,请帮助。

成交量:10.00 --- 18.89 --- 27.78 --- 36.67

温度:

300.00 - 24.94 --- 13.20 --- 8.98 --- 6.80

400.00 - 33.26 --- 17.61 ---11.97--- 9.07

500.00 - 41.57 --- 22.01 ---14.97--- 11.34

#include <stdio.h>
int main(void)
{
float vol1, vol2;
float temp1, temp2;
float R = 8.314;
int mole;
int rows;
int columns = 8;



printf("Enter the starting volume (in meters cubed):");
scanf("%f",&vol1);
while(vol1<0)
{
    printf("Error: Enter a number that is positive:");
    scanf("%f",&vol1);
}

printf("Enter the ending volume (in meters cubed):");
scanf("%f",&vol2);
while(vol2<0)
{
    printf("Error: Enter a number that is positive:");
    scanf("%f",&vol2);
}

printf("Next enter the starting temperature (in kelvin):");
scanf("%f",&temp1);
while(temp1<0)
{
    printf("Error: Enter a number that is positive:");
    scanf("%f",&temp1);
}
printf("Enter the ending temperature (in kelvin):");
scanf("%f",&temp2);
while(temp2<0)
{
    printf("Error: Enter a number that is positive:");
    scanf("%f",&temp2);
}
printf("Enter the number of moles:");
scanf("%f",&mole);
while(mole<0)
{
    printf("Error: Enter a number that is positive:");
    scanf("%f",&mole);
}

printf("How many rows should the temperature value have?\n");
scanf("%d",&rows);
while(rows<1)
{
    printf("Error: Enter a number that is positive:");
    scanf("%d",&rows);
}






return 0;
}

【问题讨论】:

  • 你的问题不是很清楚。提供更多细节,您希望我们做什么(我无法理解您的表格)。
  • 为什么提示输入值而不是让用户将它们作为参数传递?这是一个非常不自然的界面。

标签: c loops for-loop while-loop


【解决方案1】:

我想我知道你在问什么,所以我会改写你的问题,以便清楚。您正在尝试打印出一个 2D 表格,其中的值在 x 方向(在本例中为体积)以相等的步长变化,在 y 方向(在本例中为温度)具有相等的值。我认为第 1 卷是开始卷,第 2 卷是结束卷?温度也一样?

这样做的关键是使用嵌套的 for 循环

像这样的

for (x=vol1; x<vol2; x + volstepsize)
{
    for(y=temp1; y<temp2; y + tempstepsize)
    {
       compute gas law equation here using x and y and do a print statement
    }
  perform a blank print line statement here to indent/start the next row
}

通过嵌套 for 循环,您将按顺序打印出行和列;这是访问和编写二维表的基本概念。

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多