Thread: Need help writing......

  1. #46
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Okay, now I have the right lowest and highest (the couts for them is debugging code) but the average is still wrong.
    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;
    float oth = value;
    //  cin >> o;
    n = 7;
    cin >> oth;
    highest = oth;
    lowest = oth;
    for (float i = 1; i < n; ++i) {
     //   highest = value;
       // lowest = value;
        cin >> value;
        
        if (value > highest) {
            highest = value;
        }
        if (value < lowest) {
            lowest = value;
        }
      
        average += value;
      
    }
    cout << highest << endl;
    cout << lowest << endl;
        if (value != highest || value != lowest) {
        average /= 5;
    }
    // average = average + 1;
    cout << "Average score: " << setprecision(3) << average;
    }

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

  3. #48
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Your if statement is all mangled now. You are saying if value isn't highest or lowest then calculate the average. So if your last value input is the highest or lowest you won't even average.

    At this point you aren't taking the values out that you don't want. So your average after the loop equals the sum of ALL values. You have the correct value of lowest and highest but you need to remove the sum of the two from your average before executing.

    Make sure to remove that if statement because it doesn't do what you think it does.

    You are very close though.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  4. #49
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Okay, I now get 7.7 instead of 7.82 with 9.2,4.3,6,8.7,7.3,8.0,and 9.1.
    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;
    float oth = value;
    //  cin >> o;
    n = 7;
    cin >> oth;
    highest = oth;
    lowest = oth;
    for (float i = 1; i < n; ++i) {
     //   highest = value;
       // lowest = value;
        cin >> value;
        
        if (value > highest) {
            highest = value;
        }
        if (value < lowest) {
            lowest = value;
        }
      
        average += value;
      
    }
    //cout << highest << endl;
    //cout << lowest << endl;
     //   if (value != highest || value != lowest) {
          average = average - highest + lowest;
        average /= 5;
    //}
    // average = average + 1;
    cout << "Average score: " << setprecision(3) << average;
    }

  5. #50
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Code:
    average = average - highest + lowest;
    Think about that for a second. C++ handles the operator order of addition and subtraction from left to right so you aren't getting the value you think you are. Just in numerical terms think about how it would evaluate.

    10 - 4 + 5

    So it would execute 10-4 and be 6 then add 5 to six so you would end up with 11.

    A set of parenthesis could completely change that.

    10 - (4 + 5)

    Now you will get 4+5 executed first which is 9 then that will be subtracted from 10 and you will get 1.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  6. #51
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Still not working:
    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;
    float oth = value;
    //  cin >> o;
    n = 7;
    cin >> oth;
    highest = oth;
    lowest = oth;
    for (float i = 1; i < n; ++i) {
     //   highest = value;
       // lowest = value;
        cin >> value;
        
        if (value > highest) {
            highest = value;
        }
        if (value < lowest) {
            lowest = value;
        }
      
        average += value;
      
    }
    cout << highest << endl;
    cout << lowest << endl;
     //   if (value != highest || value != lowest) {
          average = average - (highest + lowest);
        average /= 5;
    //}
    // average = average + 1;
    cout << "Average score: " << setprecision(3) << average;
    }

  7. #52
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Solved

  8. #53
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Congratulations xD
    Last edited by Lesshardtofind; 02-01-2015 at 03:24 PM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  9. #54
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Now that it's solved, I guess I can show this:
    Code:
    #include <istream>
    #include <limits>
    #include <algorithm>
    
    float jck::FindAverage(std::istream& input) {
        long count = 0L;
        float sum = 0.0;
        float highest = std::numeric_limits<float>::min();
        float lowest = std::numeric_limits<float>::max();
        float value;
        
        while (input >> value) {
            highest = std::max<float>(value, highest);
            lowest = std::min<float>(value, lowest);
            sum += value;
            ++count;
        }
        
        return (sum - highest - lowest) / (count - 2L);
    }
    Just showing what's possible. You can do a lot in a few lines if you just stop and think. ... And use a few functions to simplify logic (even if you have to make your own).

  10. #55
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by whiteflags View Post
    Now that it's solved, I guess I can show this:
    Code:
    #include <istream>
    #include <limits>
    #include <algorithm>
    
    float jck::FindAverage(std::istream& input, const int& repitition) {
        long count = 0L;
        float sum = 0.0;
        float highest = std::numeric_limits<float>::min();
        float lowest = std::numeric_limits<float>::max();
        float value;
        
        while (input >> value) {
            highest = std::max<float>(value, highest);
            lowest = std::min<float>(value, lowest);
            sum += value;
            if( ++count > repetition - 1 )
            {
                break; //?
            }
        }
        
        return (sum - highest - lowest) / (count - 2L);
    }
    Just showing what's possible. You can do a lot in a few lines if you just stop and think. ... And use a few functions to simplify logic (even if you have to make your own).
    What stops the loop?
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  11. #56
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    End of file. That is how the function is designed to know when to stop.

    Or a read error. I'd throw an exception of some sort instead of returning if that could happen, I suppose. There are a lot of reasonable responses and it still would depend on the programmer's needs.

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