954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Drop Lowest Score

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.

#include #include 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 < 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 );

showAverage( total, numScores );

//Get lowest

lowest = scores[0]; for ( int count = 1; count < numScores; count++) { if(scores[numScores] < lowest) lowest = scores[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 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.

pltndragon
Newbie Poster
17 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

Use code tags specific to a language it would be better. :)

Few mistakes present:

1>
//Validate the input.
while (scores < 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
This should be:

//Validate the input.
while (scores[count] <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}


2>

void showAverage(float, int);

change this to

void showAverage(float, int, float);


3>

showAverage( total, numScores );

//Get lowest

lowest = scores[0];
for ( int count = 1; count < numScores; count++)
{
if(scores[numScores] < lowest)
lowest = scores[numScores];
}

Change this to

//Get lowest

lowest = scores[0];
for ( int count = 1; count < numScores; count++)
{
if(scores[numScores] < lowest)
lowest = scores[numScores];
}

showAverage( total, numScores , lowest);

//Then go on with your deletion of allocated memory.


4>

void showAverage(float total, int numScores)
{
float average;

//Calculate the average
.
.
.
.
}

change this to

void showAverage(float total, int numScores ,float lowest)
{
float average;

//Calculate the average
.
.
.
.
}
csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

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.

pltndragon
Newbie Poster
17 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

You've already sorted the data, so the lowest will be at one end or the other.
Adjust your loop start/end condition.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

#include #include 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.

pltndragon
Newbie Poster
17 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

pltndragon,
Use code tags. See # icon at toolbar.
for example,

[code=cplusplus]
..
.. statements
[/code]


See the difference,

#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
   ...
   return 0;
}
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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.

#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;
}
pltndragon
Newbie Poster
17 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

*shakes head*
Did you copy that from the forum, or from your editor?

Where the *** is the indentation?

Look at post #6, that code is indented.
Yours is an unreadable mess.

It'sliketryingtoreadEnglishwithallthespacestakenout.
Sureyoucanjustaboutmakeoutthewordsbutitwouldbeahellofaloteasierwithsomespaces.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
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.
//Get lowest
lowest = scores[0];
//Without the for loop code works fine.
//for ( int count = 1; count < numScores; count++)
//{
//if(scores[numScores] < lowest) 
// Out of bounds 
//array index :array[numScores]
//lowest = scores[numScores];
//}
//Function that holds total minus lowest grade.
float lstotal;
int number;
lstotal = (total - lowest);
number = numScores - 1;
//showAverage(lstotal, numScores);
//should be
showAverage(lstotal, number);
//Free memory.
delete [] scores;
return 0;
}
}
zalezog
Light Poster
47 posts since Oct 2008
Reputation Points: 53
Solved Threads: 13
 

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

int numberOfScores;
float average;
cout << "please enter the number of test scores to enter: ":
cin >> numberOfScores
float * scores = new float[numberOfScorse];
// ask the user for each score
// run the sort and have the lowest score in scores[0]
// then to get the average
for (int i = 1 /*start at 1 because 0 is the lowest*/; 1 < numberOfScores; i++)
{
        average += scores[i];  // this will sum up all the elements except the lowest
}
average /= (numberOfScores - 1); // divide buy the number of scores minus 1 because you dropped the lowest
cout << "The average score is " << average << ".\n";
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

Thank you NathanOliver, your example really helped out in trying to figure out the proper way of solving my problem. And you are right it does make everything so much more easier. I think others tried to tell me that before, but you explained it better. Also thanks to everyone for your tips along the way. It is much appreciated.

pltndragon
Newbie Poster
17 posts since Jul 2009
Reputation Points: 0
Solved Threads: 0
 

its not a problem glad to help.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You