CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Angry Find the Lowest of These

    Assignment: Lowest Score Drop

    This program is to calculate the average of a series of test scores, where the lowest score in the series is dropped. It should use the following functions:
    *getValues should ask for 5 test scores and store them in variables. Done
    *findLowest should determine which of the 5 scores is the lowest and return that value. Help!
    *calcAverage should calculate and display the average of the four highest scores. Done

    Input Validation: Do not accept test scores higher than 100 or lower than 0. Done
    ---------------------------------------------------------------------------------------------------------------------------------------------------


    Ok, as you can see from above, I'm mostly finished with this assignment, but I have a question about my code below (code in question is highlighted):
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int findLowest(int &, int &, int &, int &, int &);
    int calcAverage(int &, int &, int &, int &, int &, int &);
    
    int main()  //used as getValues
    {
        int test1, test2, test3, test4, test5, lowest, avg;
    
        cout << "Please enter the score for 5 tests:\n";
        cin >> test1;
        if (test1 > 100 || test1 < 0)
        {
            system("CLS");
            cout << "Error:\nInvalid input, please try again.\n\n";
            system("PAUSE");
            system("CLS");
            return 0;
        }
    
        cin >> test2;
        if (test2 > 100 || test2 < 0)
        {
            system("CLS");
            cout << "Error:\nInvalid input, please try again.\n\n";
            system("PAUSE");
            system("CLS");
            return 0;
        }
    
        cin >> test3;
        if (test3 > 100 || test3 < 0)
        {
            system("CLS");
            cout << "Error:\nInvalid input, please try again.\n\n";
            system("PAUSE");
            system("CLS");
            return 0;
        }
    
        cin >> test4;
        if (test4 > 100 || test4 < 0)
        {
            system("CLS");
            cout << "Error:\nInvalid input, please try again.\n\n";
            system("PAUSE");
            system("CLS");
            return 0;
        }
    
        cin >> test5;
        if (test5 > 100 || test5 < 0)
        {
            system("CLS");
            cout << "Error:\nInvalid input, please try again.\n\n";
            system("PAUSE");
            system("CLS");
            return 0;
        }
    
        lowest = findLowest(test1, test2, test3, test4, test5);
        avg = calcAverage(test1, test2, test3, test4, test5, lowest);
    
        cout << "\nLowest Test Score: " << lowest << endl;
        cout << "Average of Top 4 Scores: " << avg << endl << endl;
        system("PAUSE");
        return 0;
    }
    
    //***********************************************************************************************************************************************\\
    //***********************************************************************************************************************************************\\
    
    int findLowest(int &num1, int &num2, int &num3, int &num4, int &num5)
    {
        if (num1 < num2)
        {
            if (num1 < num3)
            {
                if (num1 < num4)
                {
                    if (num1 < num5)
                    {
                        return num1;
                    }
                }
            }
        }
    
        else if (num2 < num1)
        {
            if (num2 < num3)
            {
                if (num2 < num4)
                {
                    if (num2 < num5)
                    {
                        return num2;
                    }
                }
            }
        }
    
        else if (num3 < num1)
        {
            if (num3 < num2)
            {
                if (num3 < num4)
                {
                    if (num3 < num5)
                    {
                        return num3;
                    }
                }
            }
        }
    
        else if (num4 < num1)
        {
            if (num4 < num2)
            {
                if (num4 < num3)
                {
                    if (num4 < num5)
                    {
                        return num4;
                    }
                }
            }
        }
    
        else if (num5 < num1)
        {
            if (num5 < num2)
            {
                if (num5 < num3)
                {
                    if (num5 < num4)
                    {
                        return num5;
                    }
                }
            }
        }
    }
    
    //***********************************************************************************************************************************************\\
    //***********************************************************************************************************************************************\\
    
    int calcAverage(int &num1, int &num2, int &num3, int &num4, int &num5, int &low)
    {
        return (num1 + num2 + num3 + num4 + num5 - low) / 4;
    }
    Question: This code doesn't always give me the lowest score, after looking through it I now know why, but I can't figure out a better way to do this, can someone help me out a little please?

    Thank you for your time.
    Last edited by iiSoMeGuY 7x; May 10th, 2011 at 03:55 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Find the Lowest of These

    Code:
    int nLowest = num1;
    if(num2 < nLowest)
        nLowest = num2;
    //repeat the last two lines for the remaining numbers
    return nLowest;
    Last edited by GCDEF; May 10th, 2011 at 03:45 PM.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Find the Lowest of These

    You should either be passing your parameters by value or by const reference. For primitive types like int, value is slightly preferable. Passing by non-const reference when not necessary is bad form.

  4. #4
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Thumbs up Re: Find the Lowest of These

    Quote Originally Posted by GCDEF View Post
    Code:
    int nLowest = num1;
    if(num2 < nLowest)
        nLowest = num2;
    //repeat the last two lines for the remaining numbers
    return nLowest;
    wow, i cant believe i didnt think of that, lol...
    thanks man

  5. #5
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Find the Lowest of These

    Quote Originally Posted by Lindley View Post
    You should either be passing your parameters by value or by const reference. For primitive types like int, value is slightly preferable. Passing by non-const reference when not necessary is bad form.
    1.) This is my first assignment to get me started on function prototypes.
    2.) How is this bad form/not necessary?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Find the Lowest of These

    Quote Originally Posted by iiSoMeGuY 7x View Post
    2.) How is this bad form/not necessary?
    Passing by non-const reference means that changes made to those parameters within the function will be reflected in the variables *outside* the function. This is occasionally desirable (output parameters let a function "return" more than one value), but is not usually the intention.

    A rule of thumb is: it's always easier to find the bugs that the compiler can catch than those which don't show up until you run the code, so give the compiler all the help you can. If you don't intend a parameter to be modifiable by a function, then don't allow it to be. Make it a const reference. Or, pass by value so that changes made to that parameter within the function don't matter to any larger scope.

    Code:
    int findLowest(int, int, int, int, int);// pass by value
    int findLowest(int &, int &, int &, int &, int &);// pass by reference
    int findLowest(const int &, const int &, const int &, const int &, const int &);// pass by const reference
    Also, a temporary value (such as an integer literal) cannot be bound to a non-const reference, but can be bound to a const reference parameter or a value parameter.

    Code:
    int findLowest(int &, int &, int &, int &, int &);// pass by reference
    
    int main()
    {
        cout << findLowest(2,5,6,23,1);// compile error, this won't work
        int val2 = 2, val5 = 5, val6 = 6, val23 = 23, val1 = 1;
        cout << findLowest(val2,val5,val6,val23,val1);// but this will
    }
    Code:
    int findLowest(int, int, int, int, int);// pass by value
    int main()
    {
        cout << findLowest(2,5,6,23,1);// this is fine
        int val2 = 2, val5 = 5, val6 = 6, val23 = 23, val1 = 1;
        cout << findLowest(val2,val5,val6,val23,val1);// this is fine also
    }
    Last edited by Lindley; May 10th, 2011 at 05:02 PM.

  7. #7
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Find the Lowest of These

    ah, ok, i get it now, thanks for the tip

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured