getting average score of 5 of highest 4 input values

Hi there

I am SO stuck on an assigment question. I am definitely doing something wrong,i have chopped and changed it so many times, i just cant get it right. Anyone willing to help?
Kind regards
Rome

tried this:
void getScore(int &);
void calcAverage(int, int, int, int, int);
int findLowest (int, int, int, int, int);
int main()

{
int test1, test2, test3, test4, test5; // 5 user input test scores

// Call getScore once for each test score to be input
getScore(test1);
getScore(test2);
getScore(test3);
getScore(test4);
getScore(test5);

// Call calcAverage to calculate and display the average
calcAverage(test1, test2, test3, test4, test5);

return 0;

void getScore(int &score)
{
cout << "Enter a test score: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << "Enter a test score between 0 and 100: ";
cin >> score;
}

void calcAverage(int s1, int s2, int s3, int s4, int s5)
{
int average, sum, lowest;

sum = (s1 + s2 + s3 + s4 + s5) - lowest;

average = sum / 4;


int findLowest(int s1, int s2, int s3, int s4, int s5)
{
int lowest;

lowest = s1;

if (s2 < lowest)
lowest = s2;
if (s3 < lowest)
lowest = s3;
if (s4 < lowest)
lowest = s4;
if (s5 < lowest)
lowest = s5;
return lowest;
}

ended up with this:

#include <iostream>
using namespace std;
void displayOutput(int getScore, float calcAverage, int everage);
int getScore();
float calcAverage();



int findLowest(test1, test2, test3, test4, test5)

double lowest = test1;

if (lowest > test2)
lowest = test2;
if (lowest > test3)
lowest = test3;
if (lowest > test4)
lowest = test4;
if (lowest > test5)
lowest = test5;
}
int main()
{
int test1, test2, test3, test4, test5; // 5 user input test scores
int getScore;
int findLowest;
float averageScore;
for (int i = 0; i < 5; i++)
{
// Call getScore once for each test score to be input
test1 = getScore();
test2 = getScore();
test3 = getScore();
test4 = getScore();
test5 = getScore();
// Call calcAverage to calculate and display the average
averageScore = calcAverage(test1, test2, test3, test4, test5);
// Display the average


}
return 0;
}

int getScore()
{
for (int i = 0; i < 5; i++)
{
cout << "Please enter a test score. ";
cin >> getScore;
}
while (getScore < 0 || getScore > 100)
{
cout << "Valid test scores range from 0 - 100.\n";
cout << "Please enter a test score. ";
cin >> getScore;
}
}

float calcAverage(int s1, int s2, int s3, int s4, int s5)
{
//Fill in code
int average, sum, lowest;

sum = (s1 + s2 + s3 + s4 + s5) - lowest;

average = sum / 4;

}




void displayOutput(int getScore, float calcAverage, float everage)

{
and now i dont know where to from here....
First of all, your code seems to be lacking some closing brackets ('}'). Make sure to fix that.

Anyway, there's nothing wrong with your logic [as far as I can tell after a quick glance]. The problem is that you don't understand the concept of "scope".

Every function and every variable has a "scope". This defines what it can or can't see (for a function), and what can and what can't access it (for a variable).

For example, the variables "average, sum, lowest" are only known in calcAverages(), because that's where you declare them. "lowest" also exists in findLowest(), but it's not the same variable, and they'll never exist at the same time. One is in the scope of calcAverages(), the other in the scope of findLowest().

Basically, your functions are doing their job, but they're lacking communication. calcAverages() needs the result of findLowest() (i.e. the variable "lowest") but it can't access it, because of scope issues.

Communication can be done in several ways. You can "upgrade" the scope of the variable (make it global, i.e. every function can see and use it), or you can use Return values and function calls. The second option is usually the best:

1
2
3
4
5
int findLowest(int s1.. etc) {
   int lowest;
   // logic to determine lowest goes here
   return lowest;
}

Now, the function findLowest() will report its findings. The return value contains the result of the function. Now, we can use it in other functions.

1
2
3
4
5
6
int calcAverage(int s1....etc) {
    int sum, average, lowest;
    lowest = findLowest(s1...etc);
    // average finding logic here
    return average;
}


Now, you can get the average by calling the function. It will call findLowest() by itself, when needed.

(P.S.: calcAverage returns an int, so the result will be rounded down to the next int. To get the exact result, you'll need to cast ints to floats and change the return value.)


Try using the <algorithm> library and use vector, sort and a bit of division. (Assuming I understood the question correctly that is!)
Hi guys,

thanks so much. After MANY hours at this, i only get one error (5 times!) any idea why it is doing this? any help would be greatly appreciated!
Kind regards
Rome'
PS, you guys rock!
#include <iostream>
using namespace std;

void getScore(int &);
void displayOutput(float averageScore);
int findLowest(int,int,int,int,int);
float averageScore;
float calcAverage(int, int, int, int, int);


int main()
{
int test1, test2, test3, test4, test5; // 5 user input test scores
float averageScore;
for (int i = 0; i < 5; i++)
{
// Call getScore once for each test score to be input
test1 = getScore();
test2 = getScore();
test3 = getScore();
test4 = getScore();
test5 = getScore();


// Call calcAverage to calculate and display the average
float averageScore = calcAverage(test1, test2, test3, test4, test5);
// Display the average
displayOutput(averageScore);
}
return 0;

}// end of main function

void getScore(int &score)
{
//Fill in code
cout << "Enter a test score: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << "Enter a test score between 0 and 100: ";
cin >> score;
}
}// end of function getScore

float calcAverage(int s1, int s2, int s3, int s4, int s5)
{
//Fill in code
int averageScore, sum, lowest;

sum = (s1 + s2 + s3 + s4 + s5) - lowest;

averageScore = sum / 4;

}// end of function calcAverage

int findLowest(int s1, int s2, int s3, int s4, int s5)
{
// Fill in code
int lowest;

lowest = s1;

if (s2 < lowest)
lowest = s2;
if (s3 < lowest)
lowest = s3;
if (s4 < lowest)
lowest = s4;
if (s5 < lowest)
lowest = s5;
return lowest;
}//



void displayOutput(float averageScore)
{
cout << "The average is: " << averageScore << endl;
}
Use code tags:
http://cplusplus.com/articles/z13hAqkS/

What are the errors you are getting? Saying "there were 5" isn't very helpful.
^this.

At first glance, I found these mistakes:
-'average score' is defined twice within the main() scope: once at the start, and again when you call the function calcAverage(). Remove the word 'float' from the second one!
-'average score' is also defined globally (at the start). Not an error (any lower-scoped variable with the same way will, by default, have priority over the globally scoped one), but it's useless at best, and dangerous at worst.
-calcAverage() returns a float, but calculates an int. Define 'averageScore' and 'sum' as a float inside that function!
hi Gaminic,

To be honest, i was not 100% sure where to define them so did like a true blonde would and do it as many times as i could get away with. I have removed the word "float" from the function. I have removed the "averageScore" declaration in globally and ensured that sum, averScore and lowest is float. but now i am a bit confused. I defined averageScore already in main. now in calcAverage, i am doing it again with sum and lowest, yet when i take it out of calcAverage (so just float sum, lowest) i get an extra error saying "undefined averageScore". Am i missing something?
Here is my code, sorry i dont quite know how to get my code so nice looking like yours:

#include <iostream>
using namespace std;

void getScore(int &);
void displayOutput(float averageScore);
int findLowest(int,int,int,int,int);
float calcAverage(int, int, int, int, int);


int main()
{
int test1, test2, test3, test4, test5; // 5 user input test scores
float averageScore;
for (int i = 0; i < 5; i++)
{
// Call getScore once for each test score to be input
test1 = getScore();
test2 = getScore();
test3 = getScore();
test4 = getScore();
test5 = getScore();


// Call calcAverage to calculate and display the average
averageScore = calcAverage(test1, test2, test3, test4, test5);
// Display the average
displayOutput(averageScore);
}
return 0;

}// end of main function

void getScore(int &score)
{
//Fill in code
cout << "Enter a test score: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << "Enter a test score between 0 and 100: ";
cin >> score;
}
}// end of function getScore

float calcAverage(int s1, int s2, int s3, int s4, int s5)
{
//Fill in code
float averageScore, sum, lowest;

sum = (s1 + s2 + s3 + s4 + s5) - lowest;

averageScore = sum / 4;

}// end of function calcAverage

int findLowest(int s1, int s2, int s3, int s4, int s5)
{
// Fill in code
int lowest;

lowest = s1;

if (s2 < lowest)
lowest = s2;
if (s3 < lowest)
lowest = s3;
if (s4 < lowest)
lowest = s4;
if (s5 < lowest)
lowest = s5;
return lowest;
}//



void displayOutput(float averageScore)
{
cout << "The average is: " << averageScore << endl;
}
ok, after some head scratching, i have this but get 2 errors:

#include <iostream>
using namespace std;

int getScore();
void displayOutput(float averageScore);
int findLowest(int,int,int,int,int);
float calcAverage(int, int, int, int, int);


int main()
{
int test1, test2, test3, test4, test5; // 5 user input test scores
float averageScore;
for (int i = 0; i < 5; i++)
{
// Call getScore once for each test score to be input
test1 = getScore();
test2 = getScore();
test3 = getScore();
test4 = getScore();
test5 = getScore();


// Call calcAverage to calculate and display the average
averageScore = calcAverage(test1, test2, test3, test4, test5);
// Display the average
displayOutput(averageScore);
}
return 0;

}// end of main function

void getScore(int &score)
{
//Fill in code
cout << "Enter a test score: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << "Enter a test score between 0 and 100: ";
cin >> score;
}
}// end of function getScore

float calcAverage(int s1, int s2, int s3, int s4, int s5)
{
//Fill in code
float averageScore, sum, lowest;

sum = (s1 + s2 + s3 + s4 + s5) - lowest;

averageScore = sum / 4;

return averageScore;
}

int findLowest(int s1, int s2, int s3, int s4, int s5)
{
// Fill in code
int lowest;

lowest = s1;

if (s2 < lowest)
lowest = s2;
if (s3 < lowest)
lowest = s3;
if (s4 < lowest)
lowest = s4;
if (s5 < lowest)
lowest = s5;
return lowest;
}//



void displayOutput(float averageScore)
{
cout << "The average is: " << averageScore << endl;
}


cpp(50) : warning C4700: uninitialized local variable 'lowest' used
1>Linking...
1>4.obj : error LNK2019: unresolved external symbol "int __cdecl getScore(void)" (?getScore@@YAHXZ) referenced in function _main
1>\\SBSERVER\UserShares\Rome\My Documents\Visual Studio 2008\Projects\Question 4 COS\Debug\Question 4 COS.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://\\sbserver\usershares\Rome\My Documents\Visual Studio 2008\Projects\Question 4 COS\Question 4 COS\Debug\BuildLog.htm"
1>Question 4 COS - 2 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Your declaration for getScore doesn't match your definition.

int getScore();

void getScore(int &score)

So the linker doesn't know where getScore() is when you call it in main().
Last edited on
i saw that too, changed to int getScore() and also to return 0; now i still get an error that lowest isnt being initialized...
ok..... now im almost there,
#include <iostream>
using namespace std;

int getScore();
void displayOutput(float averageScore);
int findLowest(int,int,int,int,int);
float calcAverage(int, int, int, int, int);


int main()
{
int test1, test2, test3, test4, test5; // 5 user input test scores
float averageScore;
{
// Call getScore once for each test score to be input
test1 = getScore();
test2 = getScore();
test3 = getScore();
test4 = getScore();
test5 = getScore();


// Call calcAverage to calculate and display the average
averageScore = calcAverage(test1, test2, test3, test4, test5);
// Display the average
displayOutput(averageScore);
}
return 0;

}// end of main function

int getScore()
{
int score;
//Fill in code
cout << "Enter a test score: ";
cin >> score;
while (score < 0 || score > 100)
{
cout << "Enter a test score between 0 and 100: ";
cin >> score;
}
return score;

}// end of function getScore

float calcAverage(int s1, int s2, int s3, int s4, int s5)
{
//Fill in code
float averageScore, sum, lowest;

sum = (s1 + s2 + s3 + s4 + s5) - lowest;

averageScore = sum / 4;

return averageScore;

}// end of function calcAverage

int findLowest(int s1, int s2, int s3, int s4, int s5)
{
// Fill in code
int lowest;

lowest = s1;

if (s2 < lowest)
lowest = s2;
if (s3 < lowest)
lowest = s3;
if (s4 < lowest)
lowest = s4;
if (s5 < lowest)
lowest = s5;
return lowest;
}//



void displayOutput(float averageScore)
{
cout << "The average is: " << averageScore << endl;
}

but i now get an error that lowest is being used before it was initialised....????
In calcAverage(), add lowest = findLowest(...); before the 'sum = ...' line.
Topic archived. No new replies allowed.