Thread: Help with Lowest Score drop program.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Help with Lowest Score drop program.

    Hi all, I'm having a bit of trouble with a homework assignment. I'm supposed to write a program that calculates the average of a number of tests scores while dropping the lowest. I'm stuck on a G++ compiler error and I've not made any progress on it. Here's my code:
    Code:
    #include <iostream>#include <iomanip>
    using namespace std;
    
    
    void start();
    void getScore(int&);
    void calcAvg(int scores[5]);
    int findLowest(int scores[5]);
    
    
    char run;
    
    
    int main()
    {
        int    scores;
        
        start();
        while (run == 'Y' || run == 'y')
            {
            getScore(scores);
            calcAvg(scores);
            start();
            }
    return 0;
    }
    
    
    // fucntion to change value of run variable, allowing user to run again or quit
    void start()
        {
        cout << "Type Y to run program, Q to quit." << endl;
        cin >> run;
        cin.ignore();
        }
    
    
    // function to get scores from user input
    void getScore()
    {
        int size = 5;
        double scores[5];
    
    
        cout << "Please enter test scores." << endl;
    
    
        for ( int i=0 ; i<5 ; i++ )
        {
            cout << "Enter score " << ( i + 1 ) << " : ";
            cin >> scores[i];
    
    
        // check to make sure the score is valid - find out if this works on arrays    
        while (scores[i] < 0 || scores[i] > 100)
            {
            cout << "Please enter a vaild score between 0 and 100: ";
            cin >> scores[i];
            }
        }
    }
    
    
    // fucntion to find the lowest score entered via array loop
    int findLowest(int scores[5])
        {
        int lowest = 0;        // starts with the first index of the array
        for (int i=1 ; i<5 ; ++i)    // loop to find lowest score by through comparison
            {
            if (scores[i] < scores[lowest])
                {
                lowest=i;
                }
            }
        return lowest;    // return the lowest found value
        }
    
    
    // function to calculate the average of the scores, excluding the lowest
    void calcAvg(int scores[5])
        {
        double average=0;
        int sum=0;     // not sure about this
        for (int i=0 ; i<5 ; ++i)
            {
            sum+=scores[i];
            }
        average-=scores[findLowest(scores)];    // not sure about sytax here, check
        average/=4;
        }
    And here's the error:
    Code:
    avg_scoredrop.cpp: In function ‘int main()’:
    avg_scoredrop.cpp:38:17: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
    avg_scoredrop.cpp:25:6: error:   initializing argument 1 of ‘void calcAvg(int*)’ [-fpermissive]
    I've found a few links via google about this, but I haven't been able to determine what I'm doing wrong. I'm fairly sure the problem is in my function prototypes. Any advice would be greatly appreciated! Furthermore, general critiques of my code would be great too!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    CalcAvg() has an argument that is an array of int. When calling it, main() supplies a single int value.

    An "int" is not magically converted into an "array of int".
    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.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Okay. I tried changing the call in main to the following:
    calcAvg(scores[5]); but this gives an invalid type error. Changing the declaration of int scores to int scores[5] doesn't work either. I just don't understand what the syntax is supposed to be in this case. I've gone over the documentation on arrays but it's not really helping.


  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The thing is, your code has multiple things wrong. Random hacking (which you clearly are doing) is very unlikely to fix your problems. You actually need to sit back, and look at your code in entirety, not just at the lines that trigger compilation errors.

    Let's look at your code as a compiler would. Bear in mind that compilers are pedantic, ignorant, pieces of software.

    After the declaration of start(), the compiler sees a declaration of getScore(), as a function that accepts a reference to an int. The compiler then sees a declaration of calcAvg() as a function accepting an array of five int.

    Then the compiler sees the definition of main(). The first line of main() tells the compiler that scores is a single int.

    When you call getScore(scores) the compiler is happy, because scores is an int. An int variable can be passed to a function accepting a reference to an int.

    When you call calcAvg(scores) the compiler gets confused. calcAvg() is a function that accepts an array of int. scores is not an array. Hence the compiler emits an error message.

    Because of the above, you cannot fix the problem only by changing you you call calcAvg(). If you change calcAvg(scores) to calcAvg(scores[5]) the compiler will complain, for a different reason: array syntax cannot be used on a single int. Because an int is not an array.

    The thing is, changing scores so it is an array ("int scores[5]") will fix the problem with calling calcAvg(), but then you have the problem of passing an array to getScore() .... which, you have told the compiler, requires a reference to a single int, not an array.


    There is also a problem that the implementation of getScore() does not match the declaration. A C++ compiler doesn't mind that .... it just means you have overloaded the function. But the linker will complain, because main() calls a function named getScore() that accepts a reference. But there is no such function defined (i.e. implemented) anywhere .....


    C++ also supports the notion of scope. You have used the name "scores" in multiple places. Each one has different meanings.

    What I've done above is tell you what is WRONG with your code, from the perspective of a compiler (and linker).

    It is up to you to work out how to correct each of those things which are wrong.
    Last edited by grumpy; 03-23-2012 at 10:01 PM.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to drop 2 lowest numbers (grades) in a C program
    By h20skier in forum C Programming
    Replies: 6
    Last Post: 04-19-2011, 10:18 PM
  2. Drop lowest score?
    By unknownnoob in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2010, 02:27 PM
  3. Replies: 22
    Last Post: 11-13-2009, 05:51 PM
  4. Lowest Score Drop - C++
    By getName(C-Dub) in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2008, 07:02 PM
  5. Lowest Score Drop
    By naya22 in forum C++ Programming
    Replies: 16
    Last Post: 04-29-2007, 12:48 AM