Thread: Average/Sum Calculator

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    4

    Average/Sum Calculator

    Hello,

    So I was trying to make a program that would take 4 inputs from the user and return the sum and average of those numbers. It's just the average part of my function that's acting weird; it's taking the sum of the four inputs and multiplying it by 0.8125 instead of dividing it by the number of inputs. I once entered four 4s and got an average of 13 and a sum of 16 when it should have been an average of 4 and a sum of 16. Here's the code:

    Code:
    #include <stdio.h>
    
    void avg_sum(float, float, float, float, float *, float *);
    
    int main(void)
    {
        float average, sum; /* the average and sum of the inputs from the user */
        float input_1, input_2, input_3, input_4; /* the four inputs from the user */
        
        printf("Please enter the first of the floating point numbers: ");
        if (scanf("%f", &input_1) != 1)
        {    printf("There was a problem reading the first number\n");
        return 1;
        }
        
        printf("Please enter the second of the floating point numbers: ");
        if (scanf("%f", &input_2) != 1)
        {
            printf("There was a problem reading the second nubmer\n");
        return 1;
        }
        
        printf("Please enter the third of the floating point numbers: ");
        if (scanf("%f", &input_3) != 1)
        {
            printf("There was a problem reading the third number\n");
        return 1;
        }
        
        printf("Please enter the fourth of the floating point numbers: ");
        if (scanf("%f", &input_4) !=1)
        {
            printf("There was a problem reading the fourth number\n");
        return 1;
        }
        
        avg_sum(input_2, input_2, input_3, input_4, &average, &sum);
        
        printf("The average and sum of these four floating point numbers are %f and %f\n", average, sum);
        return 0;
    }
    
    void avg_sum(float input_1, float input_2, float input_3, float input_4, float *average_ptr, float *sum_ptr)
    {
        *average_ptr = input_1 + input_2 + input_3 + input_4/4;
        *sum_ptr = input_1 + input_2 + input_3 + input_4;
        return;
    }
    Thanks. I'm new here to the forum and C in general.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You made a typo error on this line:
    Code:
    avg_sum(input_2, input_2, input_3, input_4, &average, &sum);
    Notice that input_2 appears twice.

    Note that this:
    Code:
    *average_ptr = input_1 + input_2 + input_3 + input_4/4;
    is equivalent to:
    Code:
    *average_ptr = input_1 + input_2 + input_3 + (input_4 / 4);
    Since you need the sum separately anyway, why not compute the sum first, then use it to compute the arithmetic mean?

    Also, while this is a legal function declaration:
    Code:
    void avg_sum(float, float, float, float, float *, float *);
    You might as well repeat it from the function definition:
    Code:
    void avg_sum(float input_1, float input_2, float input_3, float input_4, float *average_ptr, float *sum_ptr);
    After all, the parameter names provide semantic information.
    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

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Recall PEMDAS (Parenthesis, exponent, multiply, division, addition, subtraction)

    look at line 45
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    4
    Thank you so much! It's fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Average
    By winterx in forum C++ Programming
    Replies: 3
    Last Post: 07-15-2008, 03:01 AM
  2. Average
    By slatka in forum C Programming
    Replies: 6
    Last Post: 01-11-2008, 01:02 AM
  3. help with average
    By tmoney$ in forum C Programming
    Replies: 3
    Last Post: 05-09-2003, 05:46 PM
  4. Average age in here....
    By Musicdip in forum A Brief History of Cprogramming.com
    Replies: 51
    Last Post: 07-02-2002, 05:56 AM
  5. Connect 4 with 'average-AI'...
    By Nutshell in forum Game Programming
    Replies: 28
    Last Post: 05-24-2002, 10:46 AM