只需要在C中搞定...想要制作计算用户输入的任何数字的平均值的程序。将变化的数字分配给单个变量
该程序工作正常,我不能'逻辑'如何做下一部分。
我需要能够把他们输入的每个数字和他们的平均值,使用他们输入的第一个scanf除以的数量。我只为他们输入的每个数字分配了一个变量,但在看了代码后,我意识到需要使用微积分或一些编程技巧才能够无限地(有效)执行此操作。基本上,变量需要每次都改变,所以总和可以被分开,然后被分成总数。我在咆哮......
任何能理解我愚蠢问题的人都能给我一些建议吗?那太棒了......
我的包含和int main()在那里,不用担心。只觉得没有必要用已知的东西来混淆它。另外,我不会做任何速记 - 我觉得现在不需要。
// Base variables
int iUserReq, iNumCounter = 0;
// Each individual number
double dUserNum = 0.0;
// Calculation
double dNumSum = 0.0, dNumAvg = 0.0;
// Ask user for the number of variables to be averaged... will come in handy
printf("Please input how many numbers you would like to average together, as a number. For example, 10.\nTry to keep it low, because you're going to be putting them all in manually. > ");
scanf("%d", &iUserReq);
// If user inputs 0 or negative number, keep asking until they put in a positive number
while(iUserReq <= 0)
{
printf("Please input a number greater than 0. > ");
scanf("%d", &iUserReq);
}
// This adds a counter, so for the number of numbers the user wants to average, it will loop that many times and ask for an input that many times
// I.e. they want to average 10 numbers, it asks for 10 numbers
// THIS IS WHERE I'M STUCK... HELP?
while(iNumCounter < iUserReq)
{
printf("Please input number. > ");
scanf("%lf", &dUserNum);
iNumCounter = iNumCounter + 1;
}
return 0;
谢谢...
巴格尔
你已经声明了一个名为dNumSum的变量..你认为你应该如何处理它? –
请记住,平均值是所有输入*的总和*除以输入*的数量。 –
继续Joachim的评论 - 您可以保留所输入数字的总和,因为用户输入数字,然后除以任意点的数字数以显示连续的平均值 –