In: Computer Science
**C++ Programming Language**
a program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. The objective of this assignment is to select a language that you have never used before and modify the program from the text so that it outputs the number of A's, B's, C's, D's and F's. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade is any score that is not an F, but is at least 10% less than the average. Any scores that are within 10% of the average (either less than or greater than) are C's.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#include<iostream>
#include<cmath>
using namespace std;
//method to calculate the average value of elements in an array
//and return it
double calculateAverage(double numbers[], int count){
double sum=0;
//summing values
for(int i=0;i<count;i++){
sum+=numbers[i];
}
//finding average
double avg=(double)sum/count;
return avg;
}
//method to display the required statistics about numbers in an array/collection
void displayStats(double numbers[], int count){
//finding average
double avg=calculateAverage(numbers,count);
//initializing number of As, Bs...Fs and number of values above
//average to 0.
int A=0, B=0, C=0, D=0, F=0, aboveAvg=0;
//looping through each element in numbers array
for(int i=0;i<count;i++){
//checking if number is above average
if(numbers[i]>avg){
aboveAvg++;
}
//finding the grade
if(numbers[i]>=(avg+avg*0.2)){
//value >= 20% greater than the average
A++;
}else if(numbers[i]>=(avg+avg*0.1)){
//value >= 10% greater than the average
B++;
}else if(numbers[i]<=(avg-avg*0.2)){
//value <= 20% less than the average
F++;
}else if(numbers[i]<=(avg-avg*0.1)){
//value <= 10% less than the average
D++;
}else{
//value within 10% of average
C++;
}
}
//displaying stats to the user
cout<<"Average value: "<<avg<<endl;
cout<<"Number of values greater than average: "<<aboveAvg<<endl;
cout<<"Number of As: "<<A<<endl;
cout<<"Number of Bs: "<<B<<endl;
cout<<"Number of Cs: "<<C<<endl;
cout<<"Number of Ds: "<<D<<endl;
cout<<"Number of Fs: "<<F<<endl;
}
int main(){
//creating an array containing some numbers
double collection[]={90,98,70,65,50,75,40,92,87,77};
//displaying stats
displayStats(collection, 10);
return 0;
}
/*OUTPUT*/
Average value: 74.4
Number of values greater than average: 6
Number of As: 3
Number of Bs: 1
Number of Cs: 3
Number of Ds: 1
Number of Fs: 2