CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  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.

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