Thread: Averaging the average of the numbers above the average program doesn't work

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Averaging the average of the numbers above the average program doesn't work

    I have a program that takes the average of entered numbers, and then averages only the numbers from the entered numbers over the average of all the numbers, and outputs that. For example:
    Code:
    What size array? 5
    5 6 7 8 9
    
    8.50 is the mean above the mean.
    But my program doesn't work. Instead, I get:
    Code:
    What size array? 5
    5 6 7 8 9
    
    
    35.00 is the mean above the mean.
    Can someone please help?
    My code is:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    float Mean(const float array[100], int n) {
    float average;
    for(int i = 0; i < n; ++i) {
        average += array[i];
    
    
    }
    average /= n;
    return average;
    }
    int main( ) {
        float n;
        float average;
        float o;
        float value = 0;
        float array[100];
        float newaverage = 0; 
        cout << "What size array? ";
        cin >> o;
        n = o;
        for(int i = 0; i < n; ++i) {
         //   newarray = array[i];
            cin >> array[i];
            newaverage = average;
          if(newaverage < average) {
                       newaverage = average;
                        }
            average += array[i];
            
        }
        newaverage /= n;
        cout << fixed << setprecision(2) << average << " is the mean above the mean.";
         }

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Update: I worked on it a bit and I've made some progress but it still doesn't work.
    Can someone help? My current code:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    float Mean(float array[], float n) {
    float average;
    for(int i = 0; i < n; ++i) {
        average += array[i];
    
    
    }
    average /= n;
    return average;
    }
    int main()
    {
        int mean;
        int number1, number2;
        float array1[100], array2[100];
    
    
        cout << "What size array? ";
        cin >> number1;
    
    
         for(int i = 0; i < number1; ++i) {
            cin >> array1[i];
       }
    
    
        Mean(array1, number1);
    
    
        
        for(int condition = 0; condition < number1; condition++) {
                number2 = 0;
                if(array1[condition] > mean) {
                           array2[number2] = number2;          
                           number2++;
                                     
                           }
                           }
     
        cout << Mean(array2, number2);
    }

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    You must set the assignment of 'number2' outside the loop, or else it will be reset on every iteration.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by Alpo View Post
    You must set the assignment of 'number2' outside the loop, or else it will be reset on every iteration.
    Okay, now it gives me 3.4 (still wrong)
    Current code:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    float Mean(float array[], float n) {
    float average;
    for(int i = 0; i < n; ++i) {
        average += array[i];
    
    
    }
    average /= n;
    return average;
    }
    int main()
    {
        int mean;
        int number1, number2;
        float array1[100], array2[100];
    
    
        cout << "What size array? ";
        cin >> number1;
    
    
         for(int i = 0; i < number1; ++i) {
            cin >> array1[i];
       }
    
    
        Mean(array1, number1);
    
    
        number2 = 0;
        for(int condition = 0; condition < number1; condition++) {
            //    number2 = 0;
                if(array1[condition] > mean) {
                           array2[number2] = number2;          
                           number2++;
                                     
                           }
                           }
    
        cout << Mean(array2, number2);
    }
    Last edited by Kelton2; 02-12-2015 at 08:12 PM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Admittedly I am still unclear on your description of the problem. I do not understand what the "mean above the mean" is yet, so I cannot help you with the math.

    This may be precluding others from helping as well.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by whiteflags View Post
    Admittedly I am still unclear on your description of the problem. I do not understand what the "mean above the mean" is yet, so I cannot help you with the math.

    This may be precluding others from helping as well.
    It's displaying the mean of all the values entered greater than the mean.

  7. #7
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    You aren't initializing "average" on line 5.
    You aren't assigning array1[condition] to array2[number2] on line 37.
    You haven't assigned 'mean' on line 30.

    Why is 'mean' an int?
    Why is 'n' a float?
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Your Mean function needs correcting. Set average to 0 before you start adding.

    Also, array2 does not actually contain numbers from array1. You assigned elements as follows:
    Code:
                if(array1[condition] > mean) {
                           array2[number2] = number2;          
                           number2++;
                                      
                           }
    number2 has nothing to do with array1's elements, it stands for where you are in array2.

    edit: must be faster!

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by Alpo View Post
    You aren't initializing "average" on line 5.
    You aren't assigning array1[condition] to array2[number2] on line 37.
    You haven't assigned 'mean' on line 30.

    Why is 'mean' an int?
    Why is 'n' a float?
    Average fixed.
    Line 37 assignment fixed.
    Mean assignment fixed.
    As for the last two, does it make a difference?
    Current code:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    float Mean(float array[], float n) {
    float average = 0;
    for(int i = 0; i < n; ++i) {
        average += array[i];
    
    
    }
    average /= n;
    return average;
    }
    int main()
    {
        int mean = 0;
        int number1, number2;
        float array1[100], array2[100];
    
    
        cout << "What size array? ";
        cin >> number1;
    
    
         for(int i = 0; i < number1; ++i) {
            cin >> array1[i];
       }
    
    
        Mean(array1, number1);
    
    
        
        number2 = 0;
        for(int condition = 0; condition < number1; condition++) {
            //    number2 = 0;
                if(array1[condition] > mean) {
                           array2[number2] = number2;          
                           number2++;
                                     array1[condition] = array2[number2];
                           }
                           }
          cout << Mean(array2, number2);
    }

  10. #10
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Kelton2 View Post
    As for the last two, does it make a difference?
    'n' being a float is probably less of a problem than 'mean' being an int. If your mean function returns a float, you don't won't it truncated, or the results could be wonky.

    As for the rest, here look:

    Code:
        Mean(array1, number1); // Since you are using the variable 'mean' later, I assumed this would assign it a value?
    Code:
    if(array1[condition] > mean) {
        array2[number2] = number2;          
        number2++;
        // array1[condition] is a value larger that 'mean' (see above).
        // You need to assign array2[number2] the value array1[condition]
        // The incrementing of 'number2' should come after, so you don't have junk in array2[0]
        array1[condition] = array2[number2];
    }
    


    Last edited by Alpo; 02-12-2015 at 08:41 PM. Reason: Got variables mixed
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  11. #11
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by Alpo View Post
    'n' being a float is probably less of a problem than 'mean' being an int. If your mean function returns a float, you don't won't it truncated, or the results could be wonky.

    As for the rest, here look:

    Code:
        Mean(array1, number1); // Since you are using the variable 'mean' later, I assumed this would assign it a value?
    Code:
    if(array1[condition] > mean) {
        array2[number2] = number2;          
        number2++;
        // array1[condition] is a value larger that 'mean' (see above).
        // You need to assign array2[number2] the value array1[condition]
        // The incrementing of 'number2' should come after, so you don't have junk in array2[0]
        array1[condition] = array2[number2];
    }
    


    Still not working:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    float Mean(float array[], float n) {
    float average = 0;
    for(int i = 0; i < n; ++i) {
        average += array[i];
     
     
    }
    average /= n;
    return average;
    }
    int main()
    {
        float mean = 0;
        int number1, number2;
        float array1[100], array2[100];
     
     
        cout << "What size array? ";
        cin >> number1;
     
     
         for(int i = 0; i < number1; ++i) {
            cin >> array1[i];
       }
     
     
        mean = Mean(array1, number1);
     
     
         
        number2 = 0;
        for(int condition = 0; condition < number1; condition++) {
            //    number2 = 0;
                if(array1[condition] > mean) {
                           array2[number2] = array1[condition];          
                           
                                     array1[condition] = array2[number2];
                           }
                           number2++;
                           }
          cout << Mean(array2, number2);
    }

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Number2 is not necessarily the length of array2. This is because number2 always increases, even if the if comparing the mean to some element is false.
    You will need to move that statement.

    Also really why are you not indenting? Do you actually enjoy reading code like that? The braces can't help you this way.

  13. #13
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by whiteflags View Post
    Number2 is not necessarily the length of array2. This is because number2 always increases, even if the if comparing the mean to some element is false.
    You will need to move that statement.

    Also really why are you not indenting? Do you actually enjoy reading code like that? The braces can't help you this way.
    Where should I move the statement?
    And if the indenting is really giving people that much of a problem, I will start indenting.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well I would appreciate it if you indented.

    Where should I move the statement?
    For once think and find it out yourself. This is a working alternative with a big hint: You can always do this instead: array1[condition] = array2[number2++]; since you will only need number2 increased after that assignment.

  15. #15
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by whiteflags View Post
    Well I would appreciate it if you indented.


    You can always do this instead: array1[condition] = array2[number2++]; since you will only need number2 increased after that assignment.
    Where do I need to put that? In place of
    array2[number2] = array1[condition];? I tried putting it in place of array1[condition] = array2[number2]; and it didn't work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 03-13-2014, 08:14 PM
  2. Average of numbers in a file
    By starghost3 in forum C Programming
    Replies: 1
    Last Post: 12-14-2011, 03:20 PM
  3. Average function is not averaging...
    By csharp100 in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2011, 05:59 PM
  4. Average of n numbers
    By anirban in forum Tech Board
    Replies: 2
    Last Post: 07-16-2008, 10:57 PM
  5. Newbie : Getting the average of 3 numbers
    By Swerve in forum C++ Programming
    Replies: 10
    Last Post: 08-27-2007, 05:02 PM