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

Homework Ch. 9- Test Scores 30 points Write a program that dynamically allocates

ID: 3891355 • Letter: H

Question

Homework Ch. 9- Test Scores 30 points Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible. Input validation Do not accept negative numbers for test scores. Please submit the following: 1. Your flowchart 2. Your source code, a .cpp file The program output Points will be given based on the following requirements Program Specifications/ Correctness . Readability . Documentation e Code Efficiency Assignment Specifications See CIS-2541 Assignment Grading Guidelines for details

Explanation / Answer

Below is the solution:

code:

#include <iostream>
using namespace std;

// Function prototypes
double getScoreAvg(double *,int);
void getSortedScore(double *,int);

//main function
int main()
{
   int size,i; // To hold the size of the scores
   double *ptr; // To dynamically allocate an array
   double avg=0.0; // To hold the average of the scores

cout <<"Enter how many scores:"; //Enter score size
cin >> size;
again: //again block to enter the score size is negative
if(size<0){ //check if the size is less than 0
   cout <<"Enter size again: ";
   goto again;
}
ptr=new double[size]; // Allocate memory

for(i=0;i<size;i++)
{

cout <<" Enter test score of student "<< i+1 <<":";
cin >> *(ptr+i);
}
getSortedScore(ptr,size);

avg=getScoreAvg(ptr,size);
cout <<"Test score in ascending order: ";
//print the test score in ascending order
for(i=0;i<size;i++)
{
   cout <<ptr[i] <<endl;
}
cout <<"Average Test Score= "<<avg<<endl; //prints the avearge score
delete [] ptr;
return 0;
}

//function to score average
double getScoreAvg(double * arr,int size)
{
   double sum=0; //variable sum to sum the test score
   for(int i=0;i<size;i++)
   {
       sum+=arr[i];
   }
  
   cout <<"average= "<<sum/(double)size<<endl;
  
   sum=sum/((double)size);
   return sum;

}

//function to sort the test score
void getSortedScore(double *ptr,int size)
{
   double mainnum;
   int i=0,j,mainindex;
  
   for(i=0;i<size;i++)
   {
       mainindex=i;
       mainnum=ptr[i];
       for(j=i+1;j<size;j++)
       {
           if(ptr[j]>mainnum)
           {
               mainnum=ptr[j];
               mainindex=j;
           }
       }
       ptr[mainindex]=ptr[i];
       ptr[i]=mainnum;
   }
}

sample output:

Enter how many scores:5
Enter test score of student 1:40
Enter test score of student 2:59
Enter test score of student 3:78
Enter test score of student 4:86
Enter test score of student 5:76
Test score in ascending order:
86
78
76
59
40
Average Test Score= 67.8