Thread: Average of an Array

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    17

    Average of an Array

    I am writing a program to calculate the average of an array of 10 floating-point values. I am trying to write it so that the value entered by the user goes straight to the array.

    My problem is that the program isn't adding up the values in the array.

    Code:
    // Program to calculate the average of an array of 10 floating-point values
    
    
    #include <stdio.h>
    
    
    int main (void)
    {
        float grades[10], average;
        int i, gradeTotal = 0;
    
    
        for ( i = 1; i <= 10; ++i ) {
            printf ("Enter 10 values to be averaged #%i: ", i);
            scanf ("%i", &grades[i]);
    
    
            gradeTotal = gradeTotal + grades[i];
        }
    
    
        average = (float) gradeTotal / 10;
    
    
        printf ("\nGrade average = %.2f\n", average);
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    gradetotal should be a float, and i should start at 0.

    Also, I would try avoid scanf and use fgets() and atof() to convert to a float.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Quote Originally Posted by memcpy View Post
    gradetotal should be a float, and i should start at 0.

    Also, I would try avoid scanf and use fgets() and atof() to convert to a float.
    Changing i to 0 starts the numbering in the printf at 0, instead of 1.

    I changed gradeTotal to a float and instead of getting the last entry in the array, now I get 0.00.

    I have seen people say to avoid scanf several times. Why is that? I haven't learned how to use anything else yet.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In addition to memcpy's comments, the 10th array element doesn't exist (since you start from zero the largest you can go is size - 1).

    scanf ("%i", &grades[i]);
    %i doesn't convert input to a float, it converts input to a signed integer like %d does.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Code:
    float grades[10]
    You're telling the system to create an array of 10 floats containing indexes 0 through 9.

    Code:
    for ( i = 1; i <= 10; ++i )
    And you're now telling the system to iterate from the 1st to the 10th index, which doesn't exist (problem).

    Code:
    scanf ("%i", &grades[i]);
    There's nothing wrong with using scanf(), but you need to match types properly.

    Code:
    average = (float) gradeTotal / 10;
    You could also just put that calculation in your printf() statement. It's not any faster as far as the system is concerned, just another way to do things.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could also just put that calculation in your printf() statement. It's not any faster as far as the system is concerned, just another way to do things.
    Well, then why do it? I mean, do a calculation once, and store the answer in a variable, then you have that. Do it inline, and you have to calculate it every time.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Once you fix the bounds of your for loop, you can make a simple change to your printf:
    Code:
            printf ("Enter 10 values to be averaged #%i: ", i+1);
    (notice the +1)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Thank you all for your help, but I am still getting 0.00 after I enter the 10 values. Below is my updated code. Maybe I missed something one of you said.

    Code:
    // Program to calculate the average of an array of 10 floating-point values
    
    
    #include <stdio.h>
    
    
    int main (void)
    {
        float grades[10], average, gradeTotal = 0;
        int i;
    
    
        for ( i = 0; i <= 9; ++i ) {
            printf ("Enter 10 values to be averaged #%i: ", i+1);
            scanf ("%i", &grades[i]);
    
    
            gradeTotal = gradeTotal + grades[i];
        }
    
    
        average = gradeTotal / 10;
    
    
        printf ("\nGrade average = %.2f\n", average);
    
    
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Look at line 15.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Quote Originally Posted by failure67 View Post
    Look at line 15.
    I tried using fgets, although I admit that I may be using it wrong. With fgets, it says that the array needs to be char, instead of a float.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    scanf - C++ Reference

    What does the format specifier "i" mean?

    Note: I wish you to read the data I linked!

    Tim S.

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Quote Originally Posted by stahta01 View Post
    scanf - C++ Reference

    What does the format specifier "i" mean?

    Note: I wish you to read the data I linked!

    Tim S.
    Thanks for the reference! I have tried changing the %i in scanf to %f, but I didn't see any changes. The average is still 0.00.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    Enter 10 values to be averaged #1: 1
    Enter 10 values to be averaged #2: 2
    Enter 10 values to be averaged #3: 3
    Enter 10 values to be averaged #4: 4
    Enter 10 values to be averaged #5: 5
    Enter 10 values to be averaged #6: 6
    Enter 10 values to be averaged #7: 7
    Enter 10 values to be averaged #8: 8
    Enter 10 values to be averaged #9: 9
    Enter 10 values to be averaged #10: 10
    
    Grade average = 5.50
    Works for me.
    You do realize the total should be of type float to work with some inputs?

    Tim S.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The end condition would be "i < 10", not "i <= 9". What you have is not technically wrong, but increases chances of self-imposed confusion.

    After making the change, ensure you actually recompile and relink as appropriate. Depending on input you supply, average can also be zero.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    %i is fine for printing out and integer in the printf, but it is not the right thing for scanf to read in a float.

    Edit: Wow, I had this page loaded for far too long before I replied.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average (from and into an array)
    By browser in forum C Programming
    Replies: 4
    Last Post: 01-24-2010, 03:36 PM
  2. Average of an Array of integer
    By Emeighty in forum C++ Programming
    Replies: 3
    Last Post: 08-09-2008, 02:48 PM
  3. average of an array
    By mrsirpoopsalot in forum C Programming
    Replies: 20
    Last Post: 09-16-2006, 04:32 PM
  4. average in an array
    By s_ny33 in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2005, 01:03 PM
  5. average of total using array
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 05-13-2003, 05:27 PM