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. S
Enter the first num
outside the loop and assign that to max
min
scanf("%d",&num);
max = min = num;
Change your while loop to infinite loop
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
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);
}
your while loop should look like
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
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.
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
.
You should also consider whether or not you want to reset these variable each time through the loop.
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.
There're some issues in your code:
num
is read? You should do it before min
and max
num
to max
and 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;
}
...