gpt4 book ai didi

计算每加仑平均英里数

转载 作者:行者123 更新时间:2023-11-30 21:38:13 26 4
gpt4 key购买 nike

这里有没有C语言程序员可以帮我解决这个问题?我无法计算每加仑的平均英里数,而且我的头在旋转。如果有人能提供解决方案,我将不胜感激^_^

int x, number_of_tanks = 3;
double total_num1, total_num2;
double total_miles_per_gallon;
float division, avg;
float num1, num2;

for (x = 1; x <= 3; x++)
{
printf("Enter the number of gallons used for tank #%i: ",x);
scanf("%f", &num1);
fflush(stdin); /* clear input buffer */

printf("Enter the number of miles driven: ");
scanf("%f", &num2);
fflush(stdin); /* clear input buffer */

/*--------------------------------------------------------------*/
/* calculate and output the miles per gallon from user input. */
/* ------------------------------------------------------------ */

division = num2 / (float) num1;
printf("The miles per gallon for this tank %.1f divided by %.1f is %.1f", \
num2, num1, division);

total_num2 = total_num2 + num2;
printf("The total of miles is %f\n", total_num2);

total_num1 = total_num1 + num1;
printf("The total of gallons is %f\n", total_num1);
}

avg = (double) total_num2 / total_num1;
printf("Overall average miles per gallon for three tanks: %.1f", avg);

最佳答案

您没有初始化总计,因此它们是未定义的。当您开始添加它们时,您会得到未定义的结果。我敢打赌这就是你说的不起作用的意思。

这样做:

double total_num1 = 0;
double total_num2 = 0;

关于计算每加仑平均英里数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17356453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com