I have to drop the lowest score, and i can't include it in the calculation of the average. for input validation cant accept negative numbers for test score.
Quote ...
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float *scores, //To dynamically allocate an array
total=0.0; //Accumulator
float lowest;
int numScores; //To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process? ";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many
//test scores
scores = new float[numScores];
if(scores==NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << ( count + 1 ) << ": ";
cin >> scores[count];
//Validate the input.
while (scores < 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
}
//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//sort the elements of the array pointers
arrSelectSort ( scores, numScores );
//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr ( scores, numScores );
void showAverage(float total, int numScores)
{
float average;
//Calculate the average
average = (total - lowest) / (numScores - 1);
//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "When dropping lowest score the average is: " << average << endl;
}
I don't really know how not to include it in the calculation, or if someone could give me a hint as to how to drop a test score without including it in calculation. there is nothing in this chapter or past ones that talks about doing something like this .but the way i did it it should work, execpt it tells me that lowest is an undiclared indentifier in the void showAverage part. Also when i remove this part my input validation wont work properly, actually it doesn't work at all. Why is that is no different from how i had written it before.
It helped in getting the validation loop to work, But Getting it to display the average without the lowest score that is still not working, its giving me a reallly long number that is not even close. to the answer. I still think the last part for show average is wrong, but i can't figure out how to get the lowest grade out before calculating the average of the remaing grades.
I changed the code a bit, since the original question tells me that the score should not be included in the calculation of the average. So i did the following.
Quote ...
#include <iostream>
#include <iomanip>
using namespace std;
void arrSelectSort(float *, int);
void showArrPtr(float *, int);
void showAverage(float, int);
int main()
{
float *scores, //To dynamically allocate an array
total=0.0; //Accumulator
float lowest;
int numScores; //To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process? ";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many
//test scores
scores = new float[numScores];
if(scores==NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << ( count + 1 ) << ": ";
cin >> scores[count];
//Validate the input.
while (scores[count] < 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
}
//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//sort the elements of the array pointers
arrSelectSort ( scores, numScores );
//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr ( scores, numScores );
//Get lowest
lowest = scores[0];
for ( int count = 1; count < numScores; count++)
{
if(scores[numScores] < lowest)
lowest = scores[numScores];
}
//Function that holds total minus lowest grade.
float lstotal;
int number;
lstotal = (total - lowest);
number = numScores - 1;
showAverage(lstotal, numScores);
//Free memory.
delete [] scores;
return 0;
}
void arrSelectSort(float *array, int size)
{
int startScan, minIndex;
float minElem;
for (startScan = 0; startScan < ( size - 1 ); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ( array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrPtr(float *array, int size)
{
for (int count=0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}
void showAverage(float lstotal, int number)
{
float average;
//Calculate the average
average = lstotal / number;
//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "When dropping lowest score the average is: " << average << endl;
}
Since im not supposed to take the grade out directly in the calculation i added lstotal and number to go with the calculation and in main i defined what they are. The end result is not the answer but a really long number. I don't know if i linked them to a random/generated number or if i did it correctly and linked them to the original values input by user. Please assist in correcting my new mistake.
Ok i have fixed the code to properly show up how its supposed to be presented. Sorry about that im still learning how to use the features in this site properly.
So as far as the code it self, Why doesn't it calculate the average properly, or how do you get it to calculate the average without changing using the lowest grade in the calculation. Thanks.
Why doesn't it calculate the average properly, or how do you get it to calculate the average without changing using the lowest grade in the calculation. Thanks.
the easy way for you to not use the lowest score when you calculate the average is to sort your array to have the lowest score in the first element then use a for loop and start at the element after that. example
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.