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

1) Grades with input validation (C++). Modify grade programs with arrays and fun

ID: 3681809 • Letter: 1

Question

1) Grades with input validation (C++). Modify grade programs with arrays and functions. (the program should minimally have functions for average, min, max, # above average, etc. total of 4 functions) Add logic (to the main) so that if the user enters an invalid grade (lower than zero, higher than 100), the user will be issued an error message and then be allowed to reenter the grade. This will repeat as long as the user enters invalid grades. You will need a While loop. (See next slide for hints.) (filename = "grade-validation.cpp"). Include adequate testing and sample output. 2) Selection Sort (C++). Add selection sort function to grade program above. Program should display list of grades in sorted ascending order. (filename = "grade-sort.cpp"). Include testing and sample output. (Hint: do not modify the selection sort function in any way!!)

I need help in part 2 heres the selection sort function you cant change

void sort(double x[], int npts)
{
double min_value;
int min_index;
double temp;
for(int i= 0; i<npts - 1; i++)
{
  for(int j = i + 1; j<npts; j++)
  {
   if(x[j] < min_value)
   {
    min_value = x[i];
    min_index = j;
   }
  }

and heres the code  
  temp = x[min_index];
  x[min_index] = x[i];
  x[i] = temp;
}
return;
}

and here my code for the main function

#include <iostream>
using namespace std;

double average(double x[], int n);
double maximum(double x[], int n);
double minimum(double x[], int n);
int nAboveAvg(double x[], int n);
void sort(double x[], int npts);


int main()
{
double grades[50];
int ngrades;

cout<<"How many grades? (max = 50) ";
cin>>ngrades;

//create for loop to get grades from user
for(int i = 0; i<ngrades; i++)
{
  cout<<"Enter grade ";
  cin>> grades[i];
  while(grades[i]< 0 || grades[i] > 100)
  {
   cout<<"Invalid grade- please enter again"<<endl;
   cin>>grades[i];
  }
}

//call the functions
double avg = average(grades, ngrades);
double max = maximum(grades, ngrades);
double min = minimum(grades, ngrades);
int nAbove = nAboveAvg(grades, ngrades);
sort(grades, ngrades);
//display results

cout << "Average = " << avg << endl;
cout << "# above average = " << nAbove << endl;
cout<<"Max value is = "<<max<<endl;
cout<<"Min value is = "<<min<<endl;
cout<<"Array sorted "<<sort<<endl;

}

Explanation / Answer

1)

#include <iostream>

using namespace std;

double average(double x[], int n);

double maximum(double x[], int n);

double minimum(double x[], int n);

int nAboveAvg(double x[], int n);

int main()

{

   double grades[50];

   int ngrades;

   cout<<"How many grades? (max = 50) ";

   cin>>ngrades;

   //create for loop to get grades from user

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

   {

          cout<<"Enter grade ";

      cin>> grades[i];

      while(grades[i]< 0 || grades[i] > 100)

      {

          cout<<"Invalid grade- please enter again"<<endl;

          cin>>grades[i];

      }

}

//call the functions

double avg = average(grades, ngrades);

double max = maximum(grades, ngrades);

double min = minimum(grades, ngrades);

int nAbove = nAboveAvg(grades, ngrades);

//display results

cout << "Average = " << avg << endl;

cout << "# above average = " << nAbove << endl;

cout<<"Max value is = "<<max<<endl;

cout<<"Min value is = "<<min<<endl;

}

//Minimum function

double minimum(double x[], int npts)

{

double min = x[0];

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

{

        if(x[i] < min)

        {

            min = x[i];

        }

    }

return min;

}

2.)