Write a working C++ program to ask the user for a set of grades which are to be
ID: 3889088 • Letter: W
Question
Write a working C++ program to ask the user for a set of grades which are to be stored in an array in increasing sequence. The user must first provide the number which represents the count of all grades to be provided. For example, if there will be 10 grades, the user is first prompted to provide the number 10. Subsequently. each grade is provicted, one at a time. Allow for a maximum of 30 grades in defining the array. Whien all the grades have been sorted properly. print them all,along with the average. Process the array using a function which is invoked from the main body of the program. The main body asks for the count of grades and then invokes the grade sequencing function You must write in proper C+ syntax and actually execute the program to ensure it works properlyExplanation / Answer
#include <iostream.h>
#define MAX 30
//function declaration
int sortGrades(int arr[], int len);
int main()
{
//array declaration
int gradeList[MAX];
int size,i;
int result=0;
//read total number of elements to read
cout<<"Enter total number of grades: ";
cin>>size;
//input validation
if(size<0 || size>MAX)
{
cout<<"Please enter valid range!"<<endl;
return -1;
}
//read n grades
for(i=0;i<size;i++)
{
cout<<"Enter grade values "<<endl;
cin>>gradeList[i];
}
//print input grade values
cout<<"Unsorted Grade values:"<<endl;
for(i=0;i<size;i++)
{
cout<<gradeList[i]<<" ";
}
cout<<endl;
result = sortGrades(gradeList,size);
return result;
}
// Calculate sorting - ASCENDING ORDER
int sortGrades(int arr[], int len){
int temp;
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
//print sorted grade values
cout<<"Sorted (Ascending Order) Grade values:"<<endl;
for(i=0;i<len;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
//Calculate average of grades
float sum=0.0f;
float avg=0.0f;
for(int k=0;k<len;k++)
{
sum=sum + arr[k];
avg= sum/len;
}
//print average of grades
cout<<"Average of grades: "<<avg;
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.