C语言 C 中的平均值、最大值和最小值程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20769834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Average, max, and min program in C
提问by user3133973
So I'm coding in C, and I need to come up with code that will take n numbers from the user, and find their minimum, maximum, average, and sum of squares for for their values. So far I have the average and sum of squares portion, but the minimum and maximum is biting me.
所以我用 C 编码,我需要想出一些代码,从用户那里获取 n 个数字,并找到它们的最小值、最大值、平均值和平方和的值。到目前为止,我有平方部分的平均值和总和,但最小值和最大值正在咬我。
Keep in mind I'm at a very rudimentary level, and I have not reached arrays yet. All I know are logical operators, functions, loops, and the use of the stdlib.h, math.h, and stdio.h libraries.
请记住,我处于非常初级的水平,还没有达到数组。我只知道逻辑运算符、函数、循环以及 stdlib.h、math.h 和 stdio.h 库的使用。
This is what I have so far. The average function gave me a lot of problems when I tried to put float and double during compiling, so multiply it by a 1.0 fixed that. I have everything, just the minimum and maximum. I keep getting the last entry as my maximum, and a 0 for my minimum.
这是我到目前为止。当我在编译期间尝试放置 float 和 double 时,平均函数给我带来了很多问题,因此将其乘以 1.0 修复了该问题。我拥有一切,只有最低限度和最高限度。我一直将最后一个条目作为我的最大值,而 0 作为我的最小值。
#include<stdio.h>
int main()
{
float average;
int i, n, count=0, sum=0, squaresum=0, num, min, max;
printf("Please enter the number of numbers you wish to evaluate\n");
scanf_s("%d",&n);
printf("Please enter %d numbers\n",n);
while(count<n)
{
min=0;
max=0;
if(num>max)
max=num;
if(num<min)
min=num;
scanf_s("%d",&num);
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
}
average = 1.0*sum/n;
printf("Your average is %.2f\n",average);
printf("The sum of your squares is %d\n",squaresum);
printf("Your maximum number is %d\n",max);
printf("Your minimum number is %d\n",min);
return(0);
}
回答by Fiddling Bits
Your algorithm is not quite right. Below is the correct implementation:
你的算法不太对。下面是正确的实现:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(void)
{
float average;
int n, num, count = 0, sum = 0, squaresum = 0;
int min = INT_MAX, max = INT_MIN;
bool gotAnswer = false;
/* Don't Let User Enter Wrong Input */
while(!gotAnswer)
{
printf("Please enter the number of numbers you wish to evaluate: ");
if(scanf_s("%d", &n) != 1)
{
/* User Entered Wrong Input; Clean Up stdin Stream*/
while(getchar() != '\n')
{
continue;
}
}
else
{
/* User Input Was Good */
gotAnswer = true;
}
}
/* Clear stdin Stream Just In Case */
while(getchar() != '\n')
continue;
while(count < n)
{
/* Don't Let User Enter Wrong Input */
gotAnswer = false;
printf("Enter number %d: ", count + 1);
if(scanf_s("%d", &num) != 1)
{
/* User Entered Wrong Input; Clean Up stdin Stream */
while(getchar() != '\n')
continue;
/* Let User Try Again */
continue;
}
else
{
/* User Input Was Correct */
gotAnswer = true;
/* Clear stdin Stream Just In Case */
while(getchar() != '\n')
continue;
}
if(num > max)
max = num;
if(num < min)
min = num;
sum += num;
squaresum += num * num;
count++;
}
average = 1.0 * sum / n;
printf("Your average is %.2f\n", average);
printf("The sum of your squares is %d\n", squaresum);
printf("Your maximum number is %d\n", max);
printf("Your minimum number is %d\n", min);
system("pause");
return 0;
}
I've added error checking and recovery. Please ask if you have any questions about the logic.
我添加了错误检查和恢复。请询问您是否对逻辑有任何疑问。
回答by Robert Harvey
The way your code is currently written, minhas to start out at a high value (not 0), or the code won't work. The best value to choose is the maximum possible value for an int.
您的代码当前的编写方式min必须从高值(不是 0)开始,否则代码将无法工作。选择的最佳值是 的最大可能值int。
You should also consider whether or not you want to reset these variable each time through the loop.
您还应该考虑是否要在每次循环中重置这些变量。
回答by haccks
Enter the first numoutside the loop and assign that to maxmin
输入num循环外的第一个并将其分配给maxmin
scanf("%d",&num);
max = min = num;
Change your while loop to infinite loop
将您的 while 循环更改为无限循环
while(1) {...}
and now check for the condition that whether your counter countis equal to nis or not to breakout from the infinite loop
现在检查的条件,是否你的计数器count等于n为或不break从无限循环出
if(count == n)
break;
Full code after modification:
修改后的完整代码:
#include<stdio.h>
int main()
{
float average;
int i, n, count=0, sum=0, squaresum=0, num, min, max;
printf("Please enter the number of numbers you wish to evaluate\n");
scanf_s("%d",&n);
printf("Please enter %d numbers\n",n);
scanf_s("%d",&num);
max = min = num;
while(1)
{
if(num>max)
max=num;
if(num<min)
min=num;
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
if(count == n)
break;
scanf_s("%d",&num);
}
average = 1.0*sum/n;
printf("Your average is %.2f\n",average);
printf("The sum of your squares is %d\n",squaresum);
printf("Your maximum number is %d\n",max);
printf("Your minimum number is %d\n",min);
return(0);
}
回答by udit bansal
Assume your first number in the list as the minimum and maximum. Compare every next character with the current minimum and the current maximum and update accordingly.
假设您在列表中的第一个数字是最小值和最大值。将每个下一个字符与当前最小值和当前最大值进行比较并相应地更新。
回答by KD Techniques
int marks , marks_count=0 , max=0 , min=100 , marks_number=1;
float total , avg;
printf("Hit enter to input marks of 10 student.\n\n");
getchar();
do
{
printf("Input %d Mark : " , marks_number);
scanf("%d" ,& marks);
if (marks>max)
{
max=marks;
}
else if (marks<min)
{
min=marks;
}
marks_count++;
marks_number++;
total=total+marks;
}
while (marks_count<10);
while (marks_number<10);
avg=total/marks_count;
printf("\n\nAverage marks are : %.2f\n" , avg);
printf("Maximum marks are : %d\n" , max);
printf("Minimum marks are : %d\n\n\n" , min);
回答by AJ.
your while loop should look like
你的 while 循环应该看起来像
min=3;
max=0;
while(count<n)
{
scanf("%d",&num);
if(num>max)
max=num;
if(num<min)
min=num;
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
}
And I agree with Robert Harvey?.. You must set min
我同意罗伯特·哈维的观点吗?..你必须设置min
回答by Dmitry Bychenko
There're some issues in your code:
您的代码中存在一些问题:
- Where
numis read? You should do it beforeminandmax - When while loopexecutes first timeyou should just assign
numtomaxandmin.
- 在哪里
num读?你应该这样做之前min和max - 当while 循环第一次执行时,您应该只分配
num给maxandmin。
Something like that:
类似的东西:
int min = 0;
int max = 0;
// If your compiler supports C99 standard you can put
// bool first_time = true;
int first_time = 1;
while (count < n) {
printf("Please, enter the next number\n");
scanf_s("%d", &num);
// If your compiler supports C99 you can put it easier:
// if (first_time) {
if (first_time == 1) {
first_time = 0;
max = num;
min = num;
}
else {
if(num > max)
max = num;
if(num < min)
min = num;
}
...
回答by Ed Heal
Add a boolean, moved giving the values min, max 0 are the start of loop
添加一个布尔值,移动给出最小值,最大值 0 是循环的开始
#include<stdio.h>
int main()
{
float average;
int i, n, count=0, sum=0, squaresum=0, num, min, max;
bool first = true;
printf("Please enter the number of numbers you wish to evaluate\n");
scanf_s("%d",&n);
printf("Please enter %d numbers\n",n);
min=0;
max=0;
while(count<n)
{
scanf_s("%d",&num);
if (first) {
first = false;
min = max = num;
}
if(num>max)
max=num;
if(num<min)
min=num;
sum = sum+num;
squaresum = squaresum + (num*num);
count++;
}
average = 1.0*sum/n;
printf("Your average is %.2f\n",average);
printf("The sum of your squares is %d\n",squaresum);
printf("Your maximum number is %d\n",max);
printf("Your minimum number is %d\n",min);
return(0);
}
Should also consider to check the return value of scanf
还应该考虑检查 scanf 的返回值

