Thread: Need help one more time...

  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
    28,413
    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.
    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
    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
    28,413
    The body of your if statement's block is incorrect. You also need to initialise total to 0.
    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

  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 12: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, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12: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