Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the code bellow so that the lowest test score is dropped. This score shou

ID: 3718586 • Letter: M

Question

Modify the code bellow so that the lowest test score is dropped. This score should not be included in the calculation of the average.

// Headers

#include "stdafx.h"

#include

#include

#include

#include

using namespace std;

// Function prototype

void sort(double*, int);

double average(double*, int);

int main()

{

int numTestScores = 0;

double *testScorePtr = nullptr;

double testAverage = 0;

// get the number of test scores

cout << " How many test scores will you enter? ";

cin >> numTestScores;

// validate the input

while (numTestScores < 0)

{

cout << "The number cannot be negative. ";

cout << "Enter another number: ";

cin >> numTestScores;

}

// allocate an array to hold the test scores

testScorePtr = new double[numTestScores];

// fill the array with test scores

for (int i = 0; i < numTestScores; i++)

{

cout << "Enter test score " << (i+1) << ": ";

cin >> testScorePtr[i];

// validate the input

while (testScorePtr[i] < 0)

{

cout << " Negative scores are not allowed. ";

cout << "Enter another score for this test: ";

cin >> testScorePtr[i];

}

}

// Sort the test scores

sort(testScorePtr, numTestScores);

// get the average of the test scores

testAverage = average(testScorePtr, numTestScores);

// display the resulting data

cout << " The test scores in ascending order, and their average, are: ";

cout << " Score" << endl;

cout << " ____" << endl;

for (int j = 0; j < numTestScores; j++)

{

cout << " " << fixed << setprecision(2) << setw(6) << /**(testScorePtr + j)*/ testScorePtr[j];

}

cout << " Average score: " << setprecision(2) << fixed << testAverage << endl << endl;

// free the dynamically-allocated memory

delete[] testScorePtr;

testScorePtr = nullptr;

return 0;

}

// sort the test scores

void sort(double* score, int size)

{

int startScan, minIndex;

double minValue;

for (startScan = 0; startScan < (size - 1); startScan++)

{

minIndex = startScan;

minValue = score[startScan];

for (int index = startScan + 1; index < size; index++)

{

if (score[index] < minValue)

{

minValue = score[index];

minIndex = index

}

}

score[minIndex] = score[startScan];

score[startScan] = minValue;

}

}

// average function

double average(double* score, int numScores)

{

double total = 0; // accumulator

// calc the total number of scores

for (int k = 0; k < numScores; k++)

total += score[k];

// return the average score

return (total / numScores);

}

Explanation / Answer

#include <iostream>

#include <cstdlib>

#include <string>

#include <iomanip>

using namespace std;

// Function prototype

void sort(double*, int);

double average(double*, int);

int main()

{

     int numTestScores = 0;

     double *testScorePtr = NULL;

     double testAverage = 0;

     // get the number of test scores

     cout << " How many test scores will you enter? ";

     cin >> numTestScores;

     // validate the input

     while (numTestScores < 0)

     {

          cout << "The number cannot be negative. ";

          cout << "Enter another number: ";

          cin >> numTestScores;

     }

     // allocate an array to hold the test scores

     testScorePtr = new double[numTestScores];

     // fill the array with test scores

     for (int i = 0; i < numTestScores; i++)

     {

          cout << "Enter test score " << (i+1) << ": ";

          cin >> testScorePtr[i];

          // validate the input

          while (testScorePtr[i] < 0)

          {

               cout << " Negative scores are not allowed. ";

               cout << "Enter another score for this test: ";

               cin >> testScorePtr[i];

          }

     }

     // Sort the test scores

     sort(testScorePtr, numTestScores);

     // get the average of the test scores

     testAverage = average(testScorePtr, numTestScores);

     // display the resulting data

     cout << " The test scores in ascending order, and their average, are: ";

     cout << " Score" << endl;

     cout << " ____" << endl;

     for (int j = 0; j < numTestScores; j++)

     {

          cout << " " << fixed << setprecision(2) << setw(6) << /**(testScorePtr + j)*/ testScorePtr[j];

     }

     cout << " Average score: " << setprecision(2) << fixed << testAverage << endl << endl;

     // free the dynamically-allocated memory

     delete[] testScorePtr;

     testScorePtr = NULL;

     return 0;

}

// sort the test scores

void sort(double* score, int size)

{

     int startScan, minIndex;

     double minValue;

     for (startScan = 0; startScan < (size - 1); startScan++)

     {

          minIndex = startScan;

          minValue = score[startScan];

          for (int index = startScan + 1; index < size; index++)

          {

               if (score[index] < minValue)

               {

                    minValue = score[index];

                   minIndex = index;

               }

          }

          score[minIndex] = score[startScan];

          score[startScan] = minValue;

     }

}

// average function

double average(double* score, int numScores)

{

     double total = 0;   // accumulator

    

     // store the lowest score

     double lowest = score[0];

     // calc the total number of scores

   for (int k = 0; k < numScores; k++)

    {

           total += score[k];

          

           // if current element is smallest

           if( score[k] < lowest )

                lowest = score[k];

    }

   

    // subtract the lowest score

    total -= lowest;

     // return the average score

     return (total / (double)(numScores) - 1);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote