Thread: Need help writing......

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

    Need help writing......

    A program that reads in 7 float numbers and outputs the average, with the exception of the highest and lowest numbers.
    What I currently have:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main( ) {
        float n;
        float average;
        float o;
        float value = 0;
        
     //  cin >> o;
        n = 7;
        for(float i = 0; i < n; ++i) {
            cin >> value;
            average += value;
        }
        average /= n;
        cout << "Average score: "; //<< setprecision(3) << average;
         }
    The program currently works, but does not do the highest and lowest numbers. Can someone help me figure out how to do that? And if there are any problems with my current program, please let me know.

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Any help?

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    You don't initialize average. And you haven't bothered to get the lowest or highest inputs for exclusion.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Okay, first of all, that code that is commented out should not be. And I need help figuring out the lowest/highest inputs thing.

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Kelton2 View Post
    Okay, first of all, that code that is commented out should not be.
    That still leaves average uninitialized.


    Quote Originally Posted by Kelton2 View Post
    And I need help figuring out the lowest/highest inputs thing.
    If you can't even do that, I recommend you get a good book, or take a good online tutorial series.

    But, I'll give you a hint
    Code:
    if(value > highest) {
        highest = value; }

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Got it:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main( ) {
        float n;
        float average = 0;
        float o;
        float value = 0;
        float highest = 0;
        
     //  cin >> o;
        n = 7;
        for(float i = 0; i < n; ++i) {
                  highest = value;
            cin >> value;
            if(value > highest) {
                     highest = value;
                     }
            average += value;
        }
        average /= n;
        cout << "Average score: " << setprecision(3) << average;
         }
    Now I need the lowest, and more importantly how to make it exclude the highest and lowest

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Any help? I got the lowest condition, just need to make it affect the output.
    Last edited by Kelton2; 01-31-2015 at 08:12 PM.

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Nobody? I really need help. My code with the lowest condition is:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main( ) {
        float n;
        float average = 0;
        float o;
        float value = 0;
        float highest = 0;
        float lowest = 0;
        
     //  cin >> o;
        n = 7;
        for(float i = 0; i < n; ++i) {
        highest = value;
        lowest = value;
        cin >> value;
        if(value > highest) {
        highest = value;
        }
        if(value < lowest) {
                 lowest = value;
                 }
            average += value;
        }
        average /= n;
        cout << "Average score: " << setprecision(3) << average;
         }

  9. #9
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    It almost works now, but still not quite.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main( ) {
        float n;
        float average = 0;
        float o;
        float value = 0;
        float highest = 0;
        float lowest = 0;
        
     //  cin >> o;
        n = 7;
        for(float i = 0; i < n; ++i) {
        highest = value;
        lowest = value;
        cin >> value;
        if(value > highest) {
        highest = value;
        }
        if(value < lowest) {
                 lowest = value;
                 }
                 if(value != highest || lowest) {
            average += value;
            }
        }
        average /= n;
        average = average + 1;
        cout << "Average score: " << setprecision(3) << average;
         }

  10. #10
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Kelton2 View Post
    Code:
                 if(value != highest || lowest)
    does not do what you think it does. What you have written is identical to
    Code:
    if(value != highest || lowest != 0)

  11. #11
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    So how do I fix it?

  12. #12
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Now I have:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main( ) {
        float n;
        float average = 0;
        float o;
        float value = 0;
        float highest = 0;
        float lowest = 0;
        
     //  cin >> o;
        n = 7;
        for(float i = 0; i < n; ++i) {
        highest = value;
        lowest = value;
        cin >> value;
        if(value > highest) {
        highest = value;
        }
        if(value < lowest) {
                 lowest = value;
                 }
                 if(value != highest || value != lowest) {
            average += value;
            }
        }
        average /= n;
        // average = average + 1;
        cout << "Average score: " << setprecision(3) << average;
         }
    Still doesn't work...

  13. #13
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    You solve this by going back to pen and paper. If you don't understand the problem, then you can't solve it.

  14. #14
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I understand the problem. To average numbers, you add all the numbers together and then divide that by the amount of numbers. I need to exclude the highest and lowest numbers, you get the highest by setting the highest to the input number and if a new high is found, setting it to that. Same for lowest, but with new low.
    It's just that my implementation is wrong, and I can't understand why.

  15. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,656
    Write down the steps, not an high level idea.
    You will then see why it is not working.


    Some hints:

    Code:
        highest = value;
        lowest = value;
    Why do you do this everytime?

    Code:
    if(value != highest || value != lowest)
    If you do this within the loop, you get the 'current' highest or lowest, not the global one.
    So, wait for the loop to be done before subtracting the highest and lowest from the sum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing C
    By Zvezda in forum C Programming
    Replies: 16
    Last Post: 10-03-2011, 12:29 AM
  2. writing good code is like writing an artistic expression
    By renzokuken01 in forum C Programming
    Replies: 5
    Last Post: 02-03-2011, 08:48 PM
  3. hex writing
    By gamer in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2005, 01:00 PM
  4. writing a TSR
    By moi in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-06-2002, 11:00 AM
  5. Re-writing ls -F in C++
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 03-21-2002, 08:39 AM