Average, max, and min program in C

前端 未结 8 675
梦毁少年i
梦毁少年i 2021-01-21 02:55

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

8条回答
  •  北海茫月
    2021-01-21 03:08

    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;
        }
      ...
    

提交回复
热议问题