Need help one more time...

This is a discussion on Need help one more time... within the C Programming forums, part of the General Programming Boards category; The point of this program is to calculate the average of the array and then tell how many numbers are ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    14

    Need help one more time...

    The point of this program is to calculate the average of the array and then tell how many numbers are greater than or equal to the array. I am having trouble now with the calculating the amount greater than or equal to the array. Here's what I have so far:

    Code:
    #include <stdio.h>
    double average (int ary[ ]);
    int main (void)
    {
        double ave;
        int ary[10];
        int numbers;
        int total;
        int equal = 0;
        int x;
        printf("Enter 10 numbers: \n");
        for (numbers = 0; numbers < 10; numbers++)
            scanf("%d", &ary[numbers]);
            
        ave = average(ary);
        printf("Average : %f\n", ave);
        
        for (x = 0; x < 10; x++)
            {
               if (ary[numbers] >= ave)
               {
                  total = equal + 1;
               }
            }
        printf("Total number that is greater than or equal to the average: %d\n", total);
        system ("pause");
        return 0;
    }
    
    double average (int ary[ ])
    {
           int sum = 0;
           int i;
           for (i = 0; i < 10; i++)
               sum += ary[i];
               
           return (sum / 10.0);
    }
    I keep getting 2 for the answer. Sorry if I'm getting annoying with all these help questions. Thanks again

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    You need a count of the number of numbers that are greater than or equal to the average. This is similiar to how you computed the sum of the numbers, except that now you would be incrementing a count.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    So my if statement is incorrect?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,274
    The body of your if statement's block is incorrect. You also need to initialise total to 0.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
        for (x = 0; x < 10; x++)
            {
               if (ary[numbers] >= ave)
               {
                  total = equal + 1;
               }
            }
    you never updated numbers, the loop will finish with either [strike]total = 0 or total = 10[/strike]
    replace numbers with x and I think it will do what you want it to

    edit:

    you never update equal so it will keep reassigning total to be the same number

    total++; should do it

    Code:
        for (x = 0; x < 10; x++)
            {
               if (ary[x] >= ave)
               {
                  total++;
               }
            }
    Last edited by ಠ_ಠ; 03-15-2009 at 01:31 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    14
    Thanks! works great. Thank you guys for all your help again. You saved my grade! Is there a way to rep you on this forum?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 01:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21