Thread: Need help writing......

  1. #31
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Kelton2 View Post
    Code:
       if(value != highest || value != lowest) {
        average /= 5;
    }
    Oh dear. You should ask your school for a refund.


    Quote Originally Posted by Kelton2 View Post
    And what is good indentation?
    C Indentation

  2. #32
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    OK, 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;
        }
     
        average += value;
     
    }
        if (value != highest || value != lowest) {
        average /= 5;
    }
    // average = average + 1;
    cout << "Average score: " << setprecision(3) << average;
    }
    So how do I fix it? I followed some of the previous posts, but it still is not giving me the correct result.

  3. #33
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I suggest you add a simple print statement that prints the highest and lowest between line 31 and 33. Then reread post #15, pay particular attention to the first question: "Why do you do this everytime?"
    I followed some of the previous posts,
    Why just "some"?

    Jim

  4. #34
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    It correctly shows 9.1 as the highest, but incorrectly shows 8 as the lowest.
    As for why do you do this everytime, I thought that was the way to record the highest and lowest. If I am wrong please explain why and help me get it right.

  5. #35
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Why just "some"?
    O_o

    Obviously afraid of witches...

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #36
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Kelton2 View Post
    As for why do you do this everytime, I thought that was the way to record the highest and lowest. If I am wrong please explain why and help me get it right.
    You record the highest and lowest alright, but overwrite them just afterwards with value.

  7. #37
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    You mean in
    Code:
    highest = value;
    and
    Code:
    lowest = value;
    ?
    If so, how else am I supposed to do it? My logic there is if a new highest is found, highest becomes that highest, and same for lowest. I must be doing it wrong somehow though.

  8. #38
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The simplest way to do it would be to initialize these variables with INT_MIN and INT_MAX respectively.
    (highest with INT_MIN , not the other way around)
    You can find these macros in <limits.h>

  9. #39
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Sorry, I can't use any other header files in my situation.

  10. #40
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by manasij7479 View Post
    The simplest way to do it would be to initialize these variables with INT_MIN and INT_MAX respectively.
    (highest with INT_MIN , not the other way around)
    You can find these macros in <limits.h>
    Only, he's using floats, not ints.

    The best approach is to set the highest and lowest to the first value given, and compare from thereon.

  11. #41
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Quote Originally Posted by Yarin View Post
    Only, he's using floats, not ints.

    The best approach is to set the highest and lowest to the first value given, and compare from thereon.
    But that's what I'm doing, and it's not working.

  12. #42
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Kelton2 View Post
    But that's what I'm doing, and it's not working.
    -_-
    Do you know what for does?

  13. #43
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Yarin View Post
    Only, he's using floats, not ints.
    Oops..missed that.
    The best approach is to set the highest and lowest to the first value given, and compare from thereon.
    Yes, but implementing that takes a little bit more wizardry!

  14. #44
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    So does the first value have to be given outside the loop?

  15. #45
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Kelton2 View Post
    So does the first value have to be given outside the loop?
    Yes.
    Take the value input once outside the loop.
    Assign that value to both highest and lowest.
    And then run the loop n-1 times instead of n times.

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