Thread: Can't get this to work right..

  1. #1
    Registered User albireo's Avatar
    Join Date
    Mar 2014
    Posts
    30

    Can't get this to work right..

    Hello, I'm in a c++ coding class right now, and the teacher is going over independent functions. I'm supposed to make a program with 3 separate functions from main. Overall, the function is supposed to calculate the average percentage of 5 test scores. The first function is supposed to see if the score entered is within an upper and lower bound. The second is supposed find the lowest of the 5 scores (in order to drop one score from the average in the last function). Then finally, the average is supposed to calculate the average of the 5 functions, dropping the lowest score from the function (so in reality, its calculating an average of 4 variables).

    The biggest condition is she said that we are not allowed to use arrays, since we haven't learned them in the class yet (it's very basic so far.)

    Every time I run this program, the first function seems to get completely ignored, and the average always seems to come out as 0... I can't get this thing to work. I've tried switching some integers into floats/doubles (being a problem with a previous program), and this still isn't helping... Help?? It's due by tomorrow at midnight.

    Code:
    #include <iostream>#include <iomanip>
    
    
    int score1;
    int s1, s2, s3, s4, s5, upperBound, lowerBound, avg, lowest, num;
    char answer;
    int getValue (int, int, int);
    double calcAverage (int, int, int, int, int, int);
    int findLowest (int, int, int, int, int);
    
    
    using namespace std;
    
    
    int main () 
    {
        do
        {
        cout << "What is the highest score?" << endl;
        cin >> upperBound;
        cout << "What is the lowest score?" << endl;
        cin >> lowerBound;
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s1;
        s1 = num;
        getValue (upperBound, lowerBound, num);
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s2;
        s2 = num;
        getValue (upperBound, lowerBound, num);
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s3;
        s3 = num;
        getValue (upperBound, lowerBound, num);
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s4;
        s4 = num;
        getValue (upperBound, lowerBound, num);
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s5;
        s5 = num;
        getValue (upperBound, lowerBound, num);
        findLowest (s1, s2, s3, s4, s5);
        calcAverage (s1, s2, s3, s4, s5, lowest);
        cout << "Your average is: " << avg << endl;
        cout << "Do you wish to run this again? (y/n)" << endl;
        cin >> answer;
        }while (answer == 'y' or answer == 'Y');
    }
    
    
    int getValue( int lowerBound, int upperBound, int num )
    {
        while (num > lowerBound or num < upperBound)
        {
        cout << "This is not within our range, please enter another value" << endl;
        }
    }
    
    
    double findLowest( int s1, int s2, int s3, int s4, int s5 )
    {
        int lowest;
        if (s1 < s2 and s1 < s3 and s1 < s4 and s1 < s5)
        {
        lowest = s1;
        return lowest;
        }
        else if (s2 < s1 and s2 < s3 and s2 < s4 and s2 < s5)
        {
        lowest = s2;
        return lowest;
        }
        else if (s3 < s1 and s3 < s2 and s3 < s4 and s3 < s5)
        {
        lowest = s3;
        return lowest;
        }
        else if (s4 < s1 and s4 < s3 and s4 < s2 and s4 < s5)
        {
        lowest = s4;
        return lowest;
        }
        else if (s5 < s1 and s5 < s3 and s5 < s2 and s5 < s2)
        {
        lowest = s5;
        return lowest;
        }
    }
    
    
    double calcAverage( int s1, int s2, int s3, int s4, int s5, int lowest)
    {
        int avg;
        avg = ((s1+s2+s3+s4+s5)-lowest)/4;
        return avg;
    }
    Also, I don't know if it matters or not, but I am using Dev-C++ as the compiler.


    EDIT: My apologies if this gets on the nerves of some people (reading the sticky), could I at least get a pointer in the right direction?
    Last edited by albireo; 03-06-2014 at 02:35 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your findLowest function returns a number (usually, except when it doesn't -- see line 84). You then take that number and throw it in the trash. This is not going to be very useful when you want to use that number again, so instead of throwing it in the trash you should write it down somewhere (ie store it in a variable), something like
    Code:
    lowest_score = findLowest(a, b, c, d, e);
    Of course, that would require you having a variable, which you don't have any of (you appear to be about seven short).

  3. #3
    Registered User albireo's Avatar
    Join Date
    Mar 2014
    Posts
    30
    Quote Originally Posted by tabstop View Post
    Your findLowest function returns a number (usually, except when it doesn't -- see line 84). You then take that number and throw it in the trash. This is not going to be very useful when you want to use that number again, so instead of throwing it in the trash you should write it down somewhere (ie store it in a variable), something like
    Code:
    lowest_score = findLowest(a, b, c, d, e);
    GOTCHA! Did that with both the findLowest and calcAverage. Our teacher didn't explain that we had to save the return into a variable. Now I just need to get getValue working for the check... Thank you SO much!

    Of course, that would require you having a variable, which you don't have any of (you appear to be about seven short).

    EDIT: I got the other part working as well, here's the final code for anyone who wants to look at it. Arrays are next section, so we will see how this works.

    Code:
    #include <iostream>
    #include <iomanip>
    
    
    int score1;
    int s1, s2, s3, s4, s5, upperBound, lowerBound, avg, lowest, num, lowestScore, average, i;
    char answer;
    int getValue (int, int, int);
    double calcAverage (int, int, int, int, int, int);
    int findLowest (int, int, int, int, int);
    
    
    using namespace std;
    
    
    int main () 
    {
        do
        {
        cout << "What is the highest score?" << endl;
        cin >> upperBound;
        cout << "What is the lowest score?" << endl;
        cin >> lowerBound;
        for (i=1; i<=1; i++)
        {
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s1;
        num = s1;
        getValue (upperBound, lowerBound, num);
        }
        for (i=1; i<=1; i++)
        {
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s2;
        num = s2;
        getValue (upperBound, lowerBound, num);
        }
        for (i=1; i<=1; i++)
        {    
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s3;
        num = s3;
        getValue (upperBound, lowerBound, num);
        }
        for (i=1; i<=1; i++)
        {    
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s4;
        num = s4;
        getValue (upperBound, lowerBound, num);
        }
        for (i=1; i<=1; i++)
        {
        cout << "Enter a value in the range (" << lowerBound <<  "-" << upperBound << ")" << ":" << endl;
        cin >> s5;
        num = s5;
        getValue (upperBound, lowerBound, num);
        }
        findLowest (s1, s2, s3, s4, s5);
        lowestScore=findLowest (s1, s2, s3, s4, s5);;
        calcAverage (s1, s2, s3, s4, s5, lowestScore);
        average = calcAverage (s1, s2, s3, s4, s5, lowestScore);
        cout << "Your average is: " << average << endl;
        cout << "Do you wish to run this again? (y/n)" << endl;
        cin >> answer;
        }while (answer == 'y' or answer == 'Y');
    }
    
    
    int getValue( int lowerBound, int upperBound, int num )
    {
        if (num > lowerBound or num < upperBound)
        {
        cout << "This is not within our range, please enter another value" << endl;
        }
    }
    
    
    int findLowest( int s1, int s2, int s3, int s4, int s5 )
    {
        int lowest;
        if (s1 < s2 and s1 < s3 and s1 < s4 and s1 < s5)
        {
        lowest = s1;
        return lowest;
        }
        else if (s2 < s1 and s2 < s3 and s2 < s4 and s2 < s5)
        {
        lowest = s2;
        return lowest;
        }
        else if (s3 < s1 and s3 < s2 and s3 < s4 and s3 < s5)
        {
        lowest = s3;
        return lowest;
        }
        else if (s4 < s1 and s4 < s3 and s4 < s2 and s4 < s5)
        {
        lowest = s4;
        return lowest;
        }
        else if (s5 < s1 and s5 < s3 and s5 < s2 and s5 < s2)
        {
        lowest = s5;
        return lowest;
        }
    }
    
    
    double calcAverage( int s1, int s2, int s3, int s4, int s5, int lowest)
    {
        int avg;
        avg = ((s1+s2+s3+s4+s5)-lowestScore)/4;
        return avg;
    }

    Also, if anyone wants to show ways how this code can be spruced up, arrays or not, I would love to learn how to do it!
    Last edited by albireo; 03-06-2014 at 03:23 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Incidentally, having a set of variables (or function arguments) with names like s1,s2,s3,s4 is a classic sign that you need to learn about arrays. No, I'm not going to show you - you'll learn nothing that way.

    A loop of the form
    Code:
    for (i=1; i<=1; i++)
    is absolutely pointless, since it only ever executes the loop body once (with i equal to 1).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by albireo View Post

    Code:
    int findLowest( int s1, int s2, int s3, int s4, int s5 )
    {
        int lowest;
        if (s1 < s2 and s1 < s3 and s1 < s4 and s1 < s5)
        {
        lowest = s1;
        return lowest;
        }
        else if (s2 < s1 and s2 < s3 and s2 < s4 and s2 < s5)
        {
        lowest = s2;
        return lowest;
        }
        else if (s3 < s1 and s3 < s2 and s3 < s4 and s3 < s5)
        {
        lowest = s3;
        return lowest;
        }
        else if (s4 < s1 and s4 < s3 and s4 < s2 and s4 < s5)
        {
        lowest = s4;
        return lowest;
        }
        else if (s5 < s1 and s5 < s3 and s5 < s2 and s5 < s2)
        {
        lowest = s5;
        return lowest;
        }
    }
    Some ideas on how you could make this better:

    1. As you have it written, lowest is totally useless. It ceases to exist the moment the return happens, so you set the value and then immediately return.

    2. However, it's not a bad idea to have a lowest variable at all. Your process for finding the lowest number is pretty inefficient. You should be able to do the 5-variable case by comparing exactly four pairs of numbers - right now you've got somewhere between four and 20 different comparisons happening. If you really can't think of how, I can show you, but try to think of it on your own first.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    That is the most interesting function to find the lowest number in a set I've ever seen. Maybe I just need to get out more.
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Would... This work?
    By HelpLol in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2008, 04:43 AM
  2. why won't this work??
    By bcianfrocca in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2004, 02:36 PM
  3. Other GUI API's that work in .NET?
    By subnet_rx in forum Tech Board
    Replies: 5
    Last Post: 09-02-2004, 03:23 PM
  4. I can't work this out
    By Granger9 in forum Windows Programming
    Replies: 1
    Last Post: 08-20-2002, 11:35 AM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM