[英]get average from while loop in C using the same variable
I have a variable total
that stores the total of miles and gallons every time user enters it. 我有一个变量
total
,它存储每次用户输入时的英里和加仑的总数。
When user hits -1, the program is suppose to add all the totals together and take the average. 当用户命中-1时,该程序假定将所有总数相加并取平均值。 I can't get the variable to add the previous total value to itself for the next iteration of the loop.
对于循环的下一次迭代,我无法获取将先前的总值添加到自身的变量。
I tried totals += totals;
我尝试了
totals += totals;
but that returns the last total value of the loop? 但这返回循环的最后一个总值?
what am i doing wrong? 我究竟做错了什么?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
const float EXIT_VALUE = -1.0f;
int main(void)
{
float gallons; // number of gallons used
float miles; // number of miles driven
float totals; // total = miles * gallons
int numOfEntries = 0; // number of entries
float avgConsumption = 0; // avg of all entries made by user
// get number of gallons from user
printf("%s%.1f%s", "Enter the gallons used (", EXIT_VALUE, " to end): ");
scanf("%f", &gallons);
// loops until user enter -1
while (gallons != EXIT_VALUE) {
//miles driven by user
printf("%s", "Enter the miles driven: ");
scanf("%f", &miles);
// calculate total miles per gallon
totals = miles/gallons;
printf("The miles/gallons for this tank was %.6f\n\n", totals);
// get number of gallons from user
printf("%s%.2f%s", "Enter the gallons used (", EXIT_VALUE, " to end): ");
scanf("%f", &gallons);
totals += totals;
numOfEntries++;
avgConsumption = totals / numOfEntries;
} // end while
printf("\nThe overall average miles/gallon was %.6f: ", avgConsumption);
_getch();
return 0;
} // end main
What you need to do is have a variable that adds up all of the totals, and then when the loop is complete, divides the number by your avgConsumption variable. 您需要做的是有一个将所有总数相加的变量,然后在循环完成后,将数字除以avgConsumption变量。
Here is a slight modification of your program: 这是您的程序的略微修改:
#include <stdio.h>
const float EXIT_VALUE = -1.0f;
int main(void)
{
float gallons; // number of gallons used
float miles; // number of miles driven
float totals = 0; // total = miles * gallons
int numOfEntries = 0; // number of entries
float avgConsumption = 0; // avg of all entries made by user
// get number of gallons from user
printf("%s%.1f%s", "Enter the gallons used (", EXIT_VALUE, " to end): ");
scanf("%f", &gallons);
// loops until user enter -1
while (gallons != EXIT_VALUE) {
//miles driven by user
printf("%s", "Enter the miles driven: ");
scanf("%f", &miles);
// calculate total miles per gallon
float curTotal = miles/gallons;
printf("The miles/gallons for this tank was %.6f\n\n", curTotal);
// get number of gallons from user
printf("%s%.2f%s", "Enter the gallons used (", EXIT_VALUE, " to end): ");
scanf("%f", &gallons);
totals += curTotal;
numOfEntries++;
} // end while
avgConsumption = totals / numOfEntries;
printf("\nThe overall average miles/gallon was %.6f: ", avgConsumption);
return 0;
} // end main
What I did was use totals to add up all of the miles/gallons you received in your loop. 我所做的是使用总计来累加您在循环中收到的所有英里/加仑。 I used a local variable curTotal to store the actual miles/gallons calculation.
我使用局部变量curTotal来存储实际的英里/加仑计算。 This variable is reset for every iteration of the loop.
每次循环迭代都会重置此变量。 After th loop concludes, I calculated avgConsumption, instead of calculating it in the loop which produces different results.
循环结束后,我计算了avgConsumption,而不是在产生不同结果的循环中进行了计算。 This gives you the average number of miles per gallon reported by users.
这为您提供了用户报告的每加仑平均英里数。
I dont think you are getting the right "total" at all 我认为您根本没有得到正确的“总计”
It is getting over-written every time you reiterate through the while-loop. 每次您在while循环中重复执行时,它都会被覆盖。
Make another variable to hold the "total" and one variable that you can store "miles/gallons" by -- like 'MPG' 制作另一个变量来保存“总计”,并创建一个变量来存储“英里/加仑”,例如“ MPG”
Then at the end you can do Total = Total + MPG. 然后最后可以执行Total = Total + MPG。
what am i doing wrong?
我究竟做错了什么?
Code is mis-calculating totals. 代码错误地计算了总数。
To average multiple quotients like many miles_per_gallon (MPG), usually the approach is to divide the total_miles/total_gallons. 要平均多个商(例如许多Miles_per_gallon(MPG)),通常的方法是将total_miles / total_gallons相除。 This is not the only definition of the average MPG yet I am certain the best to use here.
这不是平均MPG的唯一定义,但我敢肯定,这是最好的定义。 @Yunnosch
@Yunnosch
// Pseudo code
total_miles = 0.0
total_gallons = 0.0
while (still_getting_input)
miles = get_input();
gallons = get_input();
print miles, gallons, miles/gallon
total_miles += miles
total_gallons += gallons
print total_miles, total_gallons, total_miles/total_gallon
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.