943,814 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2477
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 18th, 2009
0

Drop Lowest Score

Expand Post »
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;

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.
Similar Threads
Reputation Points: 0
Solved Threads: 0
Newbie Poster
pltndragon is offline Offline
17 posts
since Jul 2009
Jul 18th, 2009
0

Re: Drop Lowest Score

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

Few mistakes present:

1>
//Validate the input.
Quote ...
while (scores < 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
This should be:
c++ Syntax (Toggle Plain Text)
  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>
c++ Syntax (Toggle Plain Text)
  1. void showAverage(float, int);
change this to
c++ Syntax (Toggle Plain Text)
  1. void showAverage(float, int, float);

3>
c++ Syntax (Toggle Plain Text)
  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
c++ Syntax (Toggle Plain Text)
  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>
c++ Syntax (Toggle Plain Text)
  1. void showAverage(float total, int numScores)
  2. {
  3. float average;
  4.  
  5. //Calculate the average
  6. .
  7. .
  8. .
  9. .
  10. }
change this to
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jul 18th, 2009
0

Re: Drop Lowest Score

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.
Reputation Points: 0
Solved Threads: 0
Newbie Poster
pltndragon is offline Offline
17 posts
since Jul 2009
Jul 18th, 2009
0

Re: Drop Lowest Score

You've already sorted the data, so the lowest will be at one end or the other.
Adjust your loop start/end condition.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 18th, 2009
-1

Re: Drop Lowest Score

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.
Reputation Points: 0
Solved Threads: 0
Newbie Poster
pltndragon is offline Offline
17 posts
since Jul 2009
Jul 18th, 2009
0

Re: Drop Lowest Score

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

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


See the difference,
C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 18th, 2009
0

Re: Drop Lowest Score

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 0
Solved Threads: 0
Newbie Poster
pltndragon is offline Offline
17 posts
since Jul 2009
Jul 18th, 2009
0

Re: Drop Lowest Score

*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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 18th, 2009
0

Re: Drop Lowest Score

Click to Expand / Collapse  Quote originally posted by pltndragon ...
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)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jul 18th, 2009
0

Re: Drop Lowest Score

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)
  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.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009

This thread is solved

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.
Message:
Previous Thread in C++ Forum Timeline: Error: Debug assertion failure(Queue)
Next Thread in C++ Forum Timeline: Undefined reference - Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC