I need help with my assignment. I'm supposed to enter 5 test scores. Then, it is supposed to drop the lowest one. Then, it is supposed to average the other 4 test scores. I can't seem to drop the lowest test score. The average score is way off as well. Does anyone know what where my mistake is at?
#include <iostream>
#include <iomanip>
using namespace std;
int getScore(int test, int i, int size, int &total);
int findLowest(int myArray[], int size);
int calcAverage(int lowest, int total);
int main()
{
const int size = 5;
int myArray[size];
int test = 0;
int total = 0;
int lowest = 0;
int i = 0;
getScore(test, i, size, total);
findLowest(myArray, size);
calcAverage(lowest, total);
return 0;
}
int getScore(int test, int i, int size, int &total)
{
for (i = 0; i < size; i++)
{
cout << "Enter a test score:";
cin >> test;
while (test < 0 || test > 100)
{
cout << "INVALID! Try Again:";
cin >> test;
}
}
total += test;
return total;
}
int calcAverage(int lowest, int total)
{
int sum = 0;
int average = 0;
sum = total - lowest;
average = sum / 4.0;
cout << "The average score is:" << average << endl;
return average;
}
int findLowest(int myArray[], int size)
{
int lowest = myArray[0];
for (int i = 1; i < 5; i++)
{
if (myArray[i] < lowest)
lowest = myArray[i];
}
First, Please use code tags. They help out alot. http://www.cplusplus.com/articles/jEywvCM9/
Second, for some reason, your program isn't receiving your array integers correctly. I have made some edits here to show you:
#include <iostream>
#include <iomanip>
#include <windows.h>
usingnamespace std;
int getScore(int test, int i, int &total);
void Score_Repeat(int myArray[]);
int findLowest(int myArray[]);
int calcAverage(int lowest, int total);
int main()
{
int myArray[5];
int test = 0;
int total = 0;
int lowest = 0;
int i = 0;
getScore(test, i, total);
Score_Repeat(myArray);
findLowest(myArray);
calcAverage(lowest, total);
return 0;
}
int getScore(int test, int i, int &total)
{
for (i = 0; i < 5; i++)
{
cout << "Enter a test score:";
cin >> test;
while (test < 0 || test > 100)
{
cout << "INVALID! Try Again:";
cin >> test;
}
}
total += test;
return total;
}
void Score_Repeat(int myArray[])
{
system("CLS");
cout<<"The Scores are:\n";
for (int i = 0; i <= 5; i++)
{
cout<<myArray[i]<<endl;
}
system("pause");
system("CLS");
}
int calcAverage(int lowest, int total)
{
int sum = 0;
int average = 0;
sum = total - lowest;
average = sum / 4.0;
cout << "The average score is:" << average << endl;
return average;
}
int findLowest(int myArray[])
{
int lowest = myArray[0];
for (int i = 1; i < 5; i++)
{
if (myArray[i] < lowest)
lowest = myArray[i];
}
cout << "The lowest score is:" << lowest << endl;
return lowest;
}
The problem is that you aren't storing the scores into the myArray. In getScore(), you read the scores into test and compute the total, but you don't store them in myArray.
I suggest the following:
1. Remove total from main(). There's no reason for it to be there. That means changing getScore and calcAverage so they don't take it as parameters.
2. Change getScore() to take myArray and size as the only parameters. Then change the code so it just populates myArray. That's all it should do.
3. Change calcAverage to take myArray, size and lowest as the parameters. calcAverage should compute total inside it.