Thread: Min, Max, Mean

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    6

    Min, Max, Mean

    I'm trying to write a C code in codeblocks to ask a user for the amount of numbers they want to enter, then output the min, max, and average. the average and min are not working properly and the imput error is not working

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main(void)
    {
       float sum, min = 0, max = 0, mean = 0;
       double x, y;
       int i = 0, test = 0, test1 = 0;
    
    
    do{
             printf("How many values are to be entered?\n");
             test1 = scanf("%f", &sum);
    
    
             if(test1 == 0 || sum < 0)
             {
                while(getchar() != '\n');
                printf("INPUT ERROR!\n");
                test1 = 0;
             }
    
    
    }
    while(test1 == 0);
    
    
             printf("\n");
    
    
    for(i = 1; i <= sum; i++)
    
    
            do{
                printf("Value %d: ", i);
                test = scanf("%lf", &x);
    
    
            if(test == 0);
            {
             while(getchar() != '\n');
             printf("INPUT ERROR!\n");
            }
             }
    while(test == 0);
    
    
          y = y +x;
    
    
    
    
          if(i == 1){
            min = x;
            max = x;
          }
    
    
          if(x > max){
             max = x;
          }
          else if (x < min){
             min = x;
          }
    
    
          mean = y / sum;
    
    
       printf("The minimum value is %g, ",min);
       printf("the maximum value is %g, ",max);
       printf("and the average value is %g\n",mean);
    
    
    return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(test == 0);
    Yeah, watch out for mis-placed semicolons completely changing the meaning of your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to indent your code properly. The idea is that whenever control enters a more local scope, e.g., a function, if statement, or loop body, you indent by one level. This one level of indent could be a fixed number of spaces (e.g., 4; your editor can be configured to produce that fixed number of spaces when you press tab) or a literal tab. Be consistent. When control leaves that scope, the indent goes down by one level.

    Next, even though the value that you want to read may be floating point values, the number of values itself is a non-negative integer, not a floating point value. The name sum is probably a poor choice for the number of values as it suggests a sum of the values instead. Names like num or count are more typical.

    When reading with scanf, check that the return value is the number of assignments expected, or not. Don't check with 0. I wouldn't even use test1, but rather check the return value directly, and have this done in a function so that I can loop by calling the function and checking its return value.

    You should write functions that do one thing and do it well. This means breaking up a large function into smaller helper functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Tags for this Thread