I have to find the lowest input given, then average out the total minus the lowest score. I am having a bit of trouble with my averageScore function finding the lowest score from the array. I am getting very odd numbers as my output. Any suggestions on how to adjust this would be appreciated. Thanks in advance.
#include <iostream>
#include <cstdlib>
using namespace std;
//function prototypes
double* allocate(int&);
double averageScore(int&);
int main()
{
double* testArray;
int numOfScores;
double average;
testArray = allocate(numOfScores);
average = averageScore(numOfScores);
//delete memory created
delete[] testArray;
return 0;
}
//function to collect user info, dynamically allocate
double* allocate(int &numOfScores)
{
double* testArray;
//prompt user for scores
cout << "How many test scores would\n";
cout << "you like to process: ";
//user input validation
if(!(cin >> numOfScores))
{
cout << "Invalid input!\n";
cout << "Program termination, please\n";
cout << "restart the program." << endl;
exit(0);
}
else if(numOfScores < 0)
{
cout << "Invalid input!\n";
cout << "Program termination, please\n";
cout << "restart the program." << endl;
exit(0);
}
//dynammically allocate an arrray to hold the scores
testArray = new double[numOfScores];
//get the scores from user
for (int count = 0; count < numOfScores; count++)
{
cout << "Enter Score: ";
//user input validation
if(!(cin >> testArray[count]))
{
cout << "Invalid input!\n";
cout << "Program termination, please\n";
cout << "restart the program." << endl;
exit(0);
}
else if(testArray[count] < 0.0)
{
cout << "Invalid input!\n";
cout << "Program termination, please\n";
cout << "restart the program." << endl;
exit(0);
}
}
return testArray;
}
//function to calculate the average score
double averageScore(int &numOfScores)
{
double* testArray;
double total,
average,
scores[0],
lowest;
lowest = scores[0];
//calculate total scores entered
for(int count = 0; count < numOfScores; count++)
{
total += testArray[count];
//find lowest score entered
for(int count = 1; count < numOfScores; count++)
{
if (testArray[numOfScores] < lowest)
lowest = scores[numOfScores];
}
}
//average the total amount of scores drop the lowest
average = (total - lowest) / numOfScores;
cout << "The average test score is: " << average << endl;
cout << "Lowest is: " << lowest << endl;
return average;
}
There are a lot of problems with your
averageScorefunction, but i’ll cover the most basic one for now.First off, you should pass it some sort of data. Right now you’re using
testArrayI don’t even see where it is allocated. I’m surprised that you’re not getting segmentation faults when you run this.But it’s also not initialized. In c++, when you declare a pointer, the variable it points to has a value. It has a garbage value, and if you perform arithmetic operations with that garbage value, then your output will be garbage too.
You have to make your list of scores available to your
averageScorefunction, preferably by passing them in as a parameter.the beginning of your averaging function looks like the following:
instead it should look like this
when you use
&numOfScoresinstead ofnumOfScores, that means that if you changenumOfScoresin youraverageScorefunction, than it will change in yourmainfunction as well, and you shouldn’t do that.now, on the
double* testArray;line, you’re declaring a brand new pointer, named “testArray”, and there’s no meaningful data in it, although it might be full of garbage. there might be other double pointer variables, named “testArray” in your code, but none of them are in the scope of youraverageScorefunction. If you passtestArrayin, in your method call, you’ll then be able to use it. for example:double someNumber = testArray[i].Bare in mind that your array is also being passed by reference. If you would rather pass it by value, you can try
but don’t quote me on that one
Once you’ve done that, your code will still have some issues, but the output should be meaningful enough that you’ll hopefully be able to work those out on your own.