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

Q: (C++) Why isn\'t my testScores sorting in ascending order ? It appears that m

ID: 3679779 • Letter: Q

Question

Q: (C++) Why isn't my testScores sorting in ascending order? It appears that my SortTestScores function does not work because the output just couts the original unsorted vector of test scores Please use STL vector member functions and show commented code. Thanks!

Purpose: to write a program that uses a STL vector to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a STL function that sorts them in ascending order. Your program should also calculate the average score. Display the sorted list of scores and averages with appropriate headings.

----------------------------------------------------------------------------

Code:

#include
#include
#include    //Needed to define vectors
using namespace std;

float Average(vector scores, int n);
void SortTestScores(vector scores, int number);

void main()
{
   //Variable declaration.
   vector testScores;   //A vector of integers
   float averageScore, score;
   int number, i; //size to store number of test scores, i to loop variable

   //Input how many test scores.
   cout << "How many test scores are you entering?: ";
   cin >> number;

   //Tell user to input the data.
   cout << "Enter the test scores: " << endl;

   for (i = 0; i < number; i++)
   {
       cout << "Test Score of Student #" << (i + 1) << ": ";
       cin >> score;
       //Pushing onto back (end) of the vector.
       testScores.push_back(score);
   }
   //Call the function to sort test scores.
   SortTestScores(testScores, number);

   //Outputting sorted testScores
   cout << "Sorted TestScores:";

   for (i = 0; i < number; i++)
   {
       cout << testScores.at(i);
   }

   //Function call to avg the scores & to return the avgscore

   double avgScore = Average(testScores, number);
   //Outputting Average test score
   cout << " Average testScore is:" << avgScore << endl;
   system("pause");
} // end of main

//Sort Function definition

void SortTestScores(vector scores, int number)
{
   int temp;
   int i, j;
   for (i = 1; i < number; i++)
   {
       for (j = 0; j < number - i; j++)
       {
           if (scores.at(j) > scores.at(j + 1))
           {
               temp = scores.at(j);
               scores.at(j) = scores.at(j + 1);
               scores.at(j + 1) = temp;
           } //end if
       } // end loop k
   }// end loop i

}

//Function to calculate avg test score
float Average(vector score, int n)
{
   int i;
   float avg = 0.0;

   //to calculate sum of scores
   for (i = 0; i < n; i++)
   {
       avg += score.at(i);
   }//end for

   avg = avg / n;
   //returning avg value
   return avg;
}
//end the Average

Explanation / Answer

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

float Average(vector<int>& scores, int n); //Declaration of Average Function
void SortTestScores(vector<int>& scores, int number); //Declaration of function that sorts given vector

int main()
{
   //Variable declaration.
vector <int>testScores;   //A vector of integers
float averageScore, score;
   int number, i; //size to store number of test scores, i to loop variable

   //Input how many test scores.
   cout << "How many test scores are you entering?: ";
   cin >> number;

   //Tell user to input the data.
   cout << "Enter the test scores: " << endl;

   for (i = 0; i < number; i++)
   {
       cout << "Test Score of Student #" << (i + 1) << ": ";
       cin >> score;
       //Pushing onto back (end) of the vector.
       testScores.push_back(score);
   }
   //Call the function to sort test scores.
   SortTestScores(testScores, number);

   //Outputting sorted testScores
   cout << "Sorted TestScores:";

   for (i = 0; i < number; i++)
   {
     cout << testScores.at(i)<<endl;
   }

   //Function call to avg the scores & to return the avgscore

   double avgScore = Average(testScores, number);
   //Outputting Average test score
   cout << " Average testScore is:" << avgScore << endl;
   system("pause");
} // end of main

//Sort Function definition

void SortTestScores(vector<int>& scores, int number)
{
   int temp;
   int i, j;
   for (i = 1; i < number; i++)
   {
       for (j = 0; j < number - i; j++)
       {
           if (scores.at(j) > scores.at(j + 1))
           {
               temp = scores.at(j);
               scores.at(j) = scores.at(j + 1);
               scores.at(j + 1) = temp;
           } //end if
       } // end loop k
   }// end loop i

}

//Function to calculate avg test score
float Average(vector<int>& score, int n)
{
   int i;
   float avg = 0.0;

   //to calculate sum of scores
   for (i = 0; i < n; i++)
   {
       avg += score.at(i);
   }//end for

   avg = avg / n;
   //returning avg value
   return avg;
}
//end the Average