| | |
Drop Lowest Score
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
#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 < 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;
}
Use code tags specific to a language it would be better. 
Few mistakes present:
1>
//Validate the input.
This should be:
2>
change this to
3>
Change this to
4>
change this to

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];
}
c++ Syntax (Toggle Plain Text)
//Validate the input. while (scores[count] <= 0) { cout << "Zero or negative numbers not accepted.\n"; cout << "Test Score #" << (count + 1) << ": "; cin >>scores[count]; }
2>
c++ Syntax (Toggle Plain Text)
void showAverage(float, int);
c++ Syntax (Toggle Plain Text)
void showAverage(float, int, float);
3>
c++ Syntax (Toggle Plain Text)
showAverage( total, numScores ); //Get lowest lowest = scores[0]; for ( int count = 1; count < numScores; count++) { if(scores[numScores] < lowest) lowest = scores[numScores]; }
c++ Syntax (Toggle Plain Text)
//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>
c++ Syntax (Toggle Plain Text)
void showAverage(float total, int numScores) { float average; //Calculate the average . . . . }
c++ Syntax (Toggle Plain Text)
void showAverage(float total, int numScores ,float lowest) { float average; //Calculate the average . . . . }
Last edited by csurfer; Jul 18th, 2009 at 1:14 am.
I Surf in "C"....
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
#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,
Use code tags. See # icon at toolbar.
for example,
[code=cplusplus]
..
.. statements
[/code]
See the difference,
Use code tags. See # icon at toolbar.
for example,
[code=cplusplus]
..
.. statements
[/code]
See the difference,
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Jul 2009
Posts: 17
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
#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; }
*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.
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.
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
//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; } }
Last edited by zalezog; Jul 18th, 2009 at 3:09 pm.
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
c++ Syntax (Toggle Plain Text)
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";
Last edited by NathanOliver; Jul 18th, 2009 at 4:27 pm.
if you write
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
![]() |
Similar Threads
- using SIGNLE arrays for grade book code (C++)
- Finding the lowest of five test scores (C++)
- Help with code about highest & lowest score in loops (C++)
- Help with code about highest & lowest score in loops (C#)
- help math.min method (Java)
- help with dynamically allocated arrays (C++)
- min and max values (C++)
Other Threads in the C++ Forum
- Previous Thread: Error: Debug assertion failure(Queue)
- Next Thread: Undefined reference - Help
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






