Drop Lowest Score

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 17
Reputation: pltndragon has a little shameless behaviour in the past 
Solved Threads: 0
pltndragon pltndragon is offline Offline
Newbie Poster

Drop Lowest Score

 
0
  #1
Jul 18th, 2009
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 <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;
}
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: Drop Lowest Score

 
0
  #2
Jul 18th, 2009
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:
  1. //Validate the input.
  2. while (scores[count] <= 0)
  3. {
  4. cout << "Zero or negative numbers not accepted.\n";
  5. cout << "Test Score #" << (count + 1) << ": ";
  6. cin >>scores[count];
  7. }

2>
  1. void showAverage(float, int);
change this to
  1. void showAverage(float, int, float);

3>
  1. showAverage( total, numScores );
  2.  
  3. //Get lowest
  4.  
  5. lowest = scores[0];
  6. for ( int count = 1; count < numScores; count++)
  7. {
  8. if(scores[numScores] < lowest)
  9. lowest = scores[numScores];
  10. }
Change this to
  1. //Get lowest
  2.  
  3. lowest = scores[0];
  4. for ( int count = 1; count < numScores; count++)
  5. {
  6. if(scores[numScores] < lowest)
  7. lowest = scores[numScores];
  8. }
  9.  
  10. showAverage( total, numScores , lowest);
  11.  
  12. //Then go on with your deletion of allocated memory.

4>
  1. void showAverage(float total, int numScores)
  2. {
  3. float average;
  4.  
  5. //Calculate the average
  6. .
  7. .
  8. .
  9. .
  10. }
change this to
  1. void showAverage(float total, int numScores ,float lowest)
  2. {
  3. float average;
  4.  
  5. //Calculate the average
  6. .
  7. .
  8. .
  9. .
  10. }
Last edited by csurfer; Jul 18th, 2009 at 1:14 am.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 17
Reputation: pltndragon has a little shameless behaviour in the past 
Solved Threads: 0
pltndragon pltndragon is offline Offline
Newbie Poster

Re: Drop Lowest Score

 
0
  #3
Jul 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Drop Lowest Score

 
0
  #4
Jul 18th, 2009
You've already sorted the data, so the lowest will be at one end or the other.
Adjust your loop start/end condition.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 17
Reputation: pltndragon has a little shameless behaviour in the past 
Solved Threads: 0
pltndragon pltndragon is offline Offline
Newbie Poster

Re: Drop Lowest Score

 
-1
  #5
Jul 18th, 2009
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 <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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,677
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 479
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Drop Lowest Score

 
0
  #6
Jul 18th, 2009
pltndragon,
Use code tags. See # icon at toolbar.
for example,

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


See the difference,
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. void arrSelectSort(float *, int);
  7. void showArrPtr(float *, int);
  8. void showAverage(float, int);
  9.  
  10. int main()
  11. {
  12. float *scores, //To dynamically allocate an array
  13. total=0.0; //Accumulator
  14. ...
  15. return 0;
  16. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 17
Reputation: pltndragon has a little shameless behaviour in the past 
Solved Threads: 0
pltndragon pltndragon is offline Offline
Newbie Poster

Re: Drop Lowest Score

 
0
  #7
Jul 18th, 2009
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.

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. void arrSelectSort(float *, int);
  5. void showArrPtr(float *, int);
  6. void showAverage(float, int);
  7. int main()
  8. {
  9. float *scores, //To dynamically allocate an array
  10. total=0.0; //Accumulator
  11.  
  12. float lowest;
  13. int numScores; //To hold the number of test scores
  14. //Get the number of test scores.
  15. cout << "How many test scores would you like to process? ";
  16. cin >> numScores;
  17. //Dynamically allocate an array large enough to hold that many
  18. //test scores
  19. scores = new float[numScores];
  20. if(scores==NULL)
  21. return 0;
  22. //Get the test score for each test
  23. cout << "Enter the test scores below.\n";
  24. for (int count = 0; count < numScores; count++)
  25. {
  26. cout << "Test score #" << ( count + 1 ) << ": ";
  27. cin >> scores[count];
  28. //Validate the input.
  29. while (scores[count] < 0)
  30. {
  31. cout << "Zero or negative numbers not accepted.\n";
  32. cout << "Test Score #" << (count + 1) << ": ";
  33. cin >>scores[count];
  34. }
  35. }
  36. //Calculate the total scores
  37. for (int count = 0; count < numScores; count++)
  38. {
  39. total += scores[count];
  40. }
  41. //sort the elements of the array pointers
  42. arrSelectSort ( scores, numScores );
  43. //Will display them in sorted order.
  44. cout << "The test scores in ascending order are: \n";
  45. showArrPtr ( scores, numScores );
  46. //Get lowest
  47. lowest = scores[0];
  48. for ( int count = 1; count < numScores; count++)
  49. {
  50. if(scores[numScores] < lowest)
  51. lowest = scores[numScores];
  52. }
  53. //Function that holds total minus lowest grade.
  54. float lstotal;
  55. int number;
  56. lstotal = (total - lowest);
  57. number = numScores - 1;
  58. showAverage(lstotal, numScores);
  59. //Free memory.
  60. delete [] scores;
  61. return 0;
  62. }
  63. void arrSelectSort(float *array, int size)
  64. {
  65. int startScan, minIndex;
  66. float minElem;
  67. for (startScan = 0; startScan < ( size - 1 ); startScan++)
  68. {
  69. minIndex = startScan;
  70. minElem = array[startScan];
  71. for (int index = startScan + 1; index < size; index++)
  72. {
  73. if ( array[index] < minElem)
  74. {
  75. minElem = array[index];
  76. minIndex = index;
  77. }
  78. }
  79. array[minIndex] = array[startScan];
  80. array[startScan] = minElem;
  81. }
  82. }
  83. void showArrPtr(float *array, int size)
  84. {
  85. for (int count=0; count< size; count++)
  86. cout << array[count] << " ";
  87. cout << endl;
  88. }
  89. void showAverage(float lstotal, int number)
  90. {
  91. float average;
  92. //Calculate the average
  93. average = lstotal / number;
  94. //Display the results.
  95. cout << fixed << showpoint << setprecision(2);
  96. cout << "When dropping lowest score the average is: " << average << endl;
  97. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Drop Lowest Score

 
0
  #8
Jul 18th, 2009
*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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Drop Lowest Score

 
0
  #9
Jul 18th, 2009
Originally Posted by pltndragon View Post
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.
  1. //Get lowest
  2. lowest = scores[0];
  3. //Without the for loop code works fine.
  4. //for ( int count = 1; count < numScores; count++)
  5. //{
  6. //if(scores[numScores] < lowest)
  7. // Out of bounds
  8. //array index :array[numScores]
  9. //lowest = scores[numScores];
  10. //}
  11. //Function that holds total minus lowest grade.
  12. float lstotal;
  13. int number;
  14. lstotal = (total - lowest);
  15. number = numScores - 1;
  16. //showAverage(lstotal, numScores);
  17. //should be
  18. showAverage(lstotal, number);
  19. //Free memory.
  20. delete [] scores;
  21. return 0;
  22. }
  23. }
Last edited by zalezog; Jul 18th, 2009 at 3:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Drop Lowest Score

 
0
  #10
Jul 18th, 2009
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
  1. int numberOfScores;
  2. float average;
  3. cout << "please enter the number of test scores to enter: ":
  4. cin >> numberOfScores
  5. float * scores = new float[numberOfScorse];
  6. // ask the user for each score
  7. // run the sort and have the lowest score in scores[0]
  8. // then to get the average
  9. for (int i = 1 /*start at 1 because 0 is the lowest*/; 1 < numberOfScores; i++)
  10. {
  11. average += scores[i]; // this will sum up all the elements except the lowest
  12. }
  13. average /= (numberOfScores - 1); // divide buy the number of scores minus 1 because you dropped the lowest
  14. cout << "The average score is " << average << ".\n";
Last edited by NathanOliver; Jul 18th, 2009 at 4:27 pm.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC