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,数学。h,头。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.
这是我目前所拥有的。当我试图在编译过程中放入浮点和双精度浮点数时,平均函数给我带来了很多问题,所以乘以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);
}
7 个解决方案
#1
3
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.
我添加了错误检查和恢复。如果对逻辑有任何疑问,请提出来。
#2
2
The way your code is currently written, min
has 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.
您还应该考虑是否希望每次通过循环重置这些变量。
#3
1
Enter the first num
outside the loop and assign that to max
min
在循环外部输入第一个num并将其分配给最大最小值
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 count
is equal to n
is or not to break
out from the infinite loop
现在检查计数是否等于n的条件是否从无限循环中跳出
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);
}
#4
1
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.
假设列表中的第一个数字为最小和最大值。将每个下一个字符与当前最小字符和当前最大字符进行比较,并相应地更新。
#5
0
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
和我同意罗伯特·哈维♦. .必须设置最小
#6
0
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的返回值吗
#7
0
There're some issues in your code:
你的代码中有一些问题:
- Where
num
is read? You should do it beforemin
andmax
- num在哪里读书?你应该在最小和最大之前做
- When while loop executes first time you should just assign
num
tomax
andmin
. - 当while循环第一次执行时,应该将num分配给max和min。
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;
}
...
#1
3
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.
我添加了错误检查和恢复。如果对逻辑有任何疑问,请提出来。
#2
2
The way your code is currently written, min
has 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.
您还应该考虑是否希望每次通过循环重置这些变量。
#3
1
Enter the first num
outside the loop and assign that to max
min
在循环外部输入第一个num并将其分配给最大最小值
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 count
is equal to n
is or not to break
out from the infinite loop
现在检查计数是否等于n的条件是否从无限循环中跳出
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);
}
#4
1
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.
假设列表中的第一个数字为最小和最大值。将每个下一个字符与当前最小字符和当前最大字符进行比较,并相应地更新。
#5
0
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
和我同意罗伯特·哈维♦. .必须设置最小
#6
0
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的返回值吗
#7
0
There're some issues in your code:
你的代码中有一些问题:
- Where
num
is read? You should do it beforemin
andmax
- num在哪里读书?你应该在最小和最大之前做
- When while loop executes first time you should just assign
num
tomax
andmin
. - 当while循环第一次执行时,应该将num分配给max和min。
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;
}
...