A few questions..

I will be working on a program this evening to calculate the average of test scores while dropping the lowest test score.

I'll have a few questions, starting with this one. If you are willing to help, I'd greatly appreciate it. I have no tutors at my college that can assist me, so I have to rely on people helping me through here. Thanks ahead of time!

The first would be,

If I want to create a function to ask for the score to be inputed. They will enter 5 scores. How can I make it so when they enter it my cin>> will change for each score? So I can enter many test scores within that function, without creating separate cin>>'s. Any suggestions?
Try an array and a loop. Do you know how they work? You can find it in the documentation on this website. Arrays and loops (as well as any container with a subscript) play very nicely together.
But I see what you mean.
Last edited on
Create a vector for the scores. Use a loop, pushing back each new score into the vector.
With specifically five scores, STL is not necessary for this problem... but vector is never worse than an array.
This is what I have so far... please help. I don't fully understand arrays, never heard of vectors. I'm learning as I try.

This is giving me the error that s5 has not been intialized?


#include <iostream>
using namespace std;

void getScore(int , int , int , int , int );
void calcAverage(double, double, double, double, double);


int main()
{
int s1, s2, s3, s4, s5;
getScore(s1, s2, s3, s4, s5);
cout << "The five test scores are: ";
cout << s1 << endl << s2 << endl << s3 << endl << s4 << endl << s5 << endl;

cout << "The average of the 4 highest test scores is: ";
calcAverage(s1, s2, s3, s4, s5);

return 0;
}


void getScore(int s1, int s2, int s3, int s4, int s5)
{
cout << "Enter 5 test scores: ";
cin >> s1 >> s2 >> s3 >> s4 >> s5;
// Input Validation
while (s1 < 0 || s1 > 100 && s2 < 0 || s2 > 100 && s3 < 0 || s3 > 100 &&
s4 < 0 || s4 > 100 && s5 < 0 || s5 > 100)
{
cout << "Invaild value entered !!" << endl;
cout << "Please enter a value between 0 through 100: ";
cin >> s1 >> s2 >> s3 >> s4 >> s5;
}
}

void calcAverage(double s1, double s2, double s3, double s4, double s5)
{
cout << (s1 + s2 + s3 + s4 + s5) / 4 << endl;
}
Last edited on
@tummychow: with vector I get to do

vec.erase(std::min_element(vec.begin(), vec.end()));

to drop the lowest score and

int avg = std::accumulate(vec.begin(), vec.end(), 0) / vec.size();

to find the average.

Array is (almost) never the answer.

@littlemissb: you need to pass the variables by reference to getScore() if you want that function to change s1-s5 in main().
How do you "pass the variables"?
Is there anyway of me using this code that works and placing it in functions to fulfill my assignment. I understand this code, but I don't see how I can include functions.

#include <iostream>

using namespace std;

int main()
{
int scores[5], temp;
do {
cout << "Please enter a score: ";
cin >> scores[0];
} while(scores[0] < 0 || scores[0] > 100);

for(int i = 1; i < 5; i++){
do {
cout << "\n\nPlease enter a score: ";
cin >> scores[i];
} while(scores[i] < 0 || scores[i] > 100);

if(scores[i] < scores[0]) {
temp = scores[i];
scores[i] = scores[0];
scores[0] = temp;
}
}

double average = (scores[1] + scores[2] + scores[3] + scores[4]) / 4;
cout << "\n\nAverage of the four highest scores: " << double(average) << "\n\n";

return 0;
}
You should read all of these:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
And don't forget code tags.

Undeniably, vectors will streamline the performance of the program. I don't deny that much. But although I'd gladly use vectors for pretty much any personal application, the OP is doing this for a course and (evidently) has not covered vectors in class yet. I've found that my teachers *really* don't like it when I take advantage of prior experience to do something not yet covered.

@OP: Here's how you use vectors. The entire interface can be examined here: http://www.cplusplus.com/reference/stl/vector/
If you aren't experienced with STL or the class system, I'd honestly suggest you stick to arrays for the simplicity, even though vectors totally kick ass... I wonder if anybody's written an article on this website for basics of STL. I'm certainly not qualified enough to do it.
PLEASE HELP ME MAKE THIS WORK! It has one error : "findLowest must have a return value"

#include <iostream>
using namespace std;
int findLowest();
void getScore();
void calcAverage();


int main()
{
getScore();
calcAverage();

return 0;
}

int findLowest(int scores[5])
{
for(int i = 1; i < 5; i++)
{
if (scores[i] < scores[0])
{
int temp = scores[i];
scores[i] = scores[0];
scores[0] = temp;
}
}
}

void getScore(int scores[5])
{
do {
cout << "Please enter a score: ";
cin >> scores[0];
} while(scores[0] < 0 || scores[0] > 100);

for(int i = 1; i < 5; i++){
do {
cout << "\n\nPlease enter a score: ";
cin >> scores[i];
} while(scores[i] < 0 || scores[i] > 100);
}

int findLowest();
}

void calcAverage(int scores[5])
{
double average = (scores[1] + scores[2] + scores[3] + scores[4]) / 4;
cout << "\n\nAverage of the four highest scores: " << double(average) << "\n\n";
}
You still don't have code tags or indentation. With that amount of code, I'm not inclined to read anything without code tags.
And read over your error, then compare it to the code or section it's referring to. Do you see what the problem is? Errors are strikingly intuitive.
Topic archived. No new replies allowed.