Your C++ program consists of an integer array of 5 elements that will store test
ID: 3709602 • Letter: Y
Question
Your C++ program consists of an integer array of 5 elements that will store test scores. It also consists of the following functions:- a) getData: This function reads and stores test scores in the array. b) calcAverage: This function displays the average of the scores. c) HighScore: This function displays the highest test score and also in what position it is in the array. In the program you cannot use global variables. You have to pass the array to the above said functions to calculate or to read in input
Explanation / Answer
#include <iostream>
using namespace std;
void getData(int a[], int size) {
cout<<"Enter "<<size<<" integers: "<<endl;
for(int i=0;i<size;i++) {
cin >> a[i];
}
}
void calcAverage(int a[], int size) {
int sum = 0;
for(int i=0;i<size;i++) {
sum+=a[i];
}
cout<<"Average: "<<sum/5.0<<endl;
}
void HighScore(int a[], int size) {
int max = a[0];
int maxIndex = 0;
for(int i=0;i<size;i++) {
if(max<a[i]) {
max = a[i];
maxIndex = i;
}
}
cout<<"Max: "<<max<<endl;
cout<<"Max Index: "<<maxIndex<<endl;
}
int main()
{
int a[5];
getData(a,5);
calcAverage(a,5);
HighScore(a,5);
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.