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.

Recommended Answers

All 11 Replies

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
.
.
.
.
}

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.

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

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.

commented: Getting fed up with seeing unformatted code, and people keeping asking you to use them -7

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;
}

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;
}

*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.

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;
}
}

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";

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.

its not a problem glad to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.