3

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.

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.

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.

#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);
}
user3133973
  • 47
  • 1
  • 1
  • 3

9 Answers9

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.

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
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.

You should also consider whether or not you want to reset these variable each time through the loop.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
1

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);
}
haccks
  • 104,019
  • 25
  • 176
  • 264
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.

udit bansal
  • 106
  • 3
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

AJ.
  • 4,526
  • 5
  • 29
  • 41
0

There're some issues in your code:

  1. Where num is read? You should do it before min and max
  2. When while loop executes first time you should just assign 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;
    }
  ...
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    @Ed Heal: boolean (bool) appeared in C99 only; since there's no information in the question which C standard is used, I've put int – Dmitry Bychenko Dec 25 '13 at 07:04
  • @DimitryBychenko - We are banging into 2014 so one assumes that one has a bit more modern compiler that that! – Ed Heal Dec 25 '13 at 07:07
  • @EdHeal Tell all of the people using Microsoft's compiler to switch compilers if they want to learn anything beyond C89! Quickly! :-P –  Dec 25 '13 at 07:33
0

Add a boolean, moved giving the values min, max 0 are the start of loop

#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

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0
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);
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
0

You can use this code, it checks if the loop starts for the first time or not. If it runs for the first time it assigns the value of n to the minimum and the maximum variable and after that, it continues. When it runs a second time it checks and finds that the program runs a second time thus it does not initialize the variables with the value of n without comparing.

int n, limit, sum = 0, minimum, maximum;
float average;
bool firstTime = "true";

printf("\nEnter Limit: ");
scanf("%d", &limit);

printf("\nEnter %d numbers: \n", limit);
for (int i = 0; i < limit; i++)
{
    scanf("%d", &n);
    if (firstTime)
    {
        minimum = n;
        maximum = n;
        firstTime = false;
    }
    if (minimum > n)
    {
        minimum = n;
    }

    if (maximum < n)
    {
        maximum = n;
    }
    sum = sum + n;
}

average = sum / limit;
printf("\nMinimum: %d", minimum);
printf("\nMaximum: %d", maximum);
printf("\nSum: %d", sum);
printf("\nAverage: %.3lf", average);
Nishal
  • 1
  • 3