a) Write a program that will allow user to enter five different scores for a stu
ID: 3748977 • Letter: A
Question
a) Write a program that will allow user to enter five different scores for a student. Store the scores in a five-element array named scores. Then display back the content of the array.
b) Modified your answer for question 1. In this version, the main function passes the calories array to a program-defined void function named displayArray, whose task is to display the contents of the array.
3) Modified your answer for question 2. The additional function will be a value-returning function named getAverage. The function will calculate the average scores of the student and then return the result to the main function.
Explanation / Answer
#include<iostream>
using namespace std;
//method to display array
void displayArray(float array[],int size)
{
int i=0;//to traverse each element of array
while(i<size)
{
cout<<array[i]<<" ";//displaying array elements
i++;
}
cout<<" ";
}
//method to compute average
float Compute_average(float array[],int size)
{
int i=0;//to traverse each element of array
int sum=0;//to store sum
while(i<size)
{
sum= sum+ array[i];//calculating sum
i++;
}
return sum/size;//calculating and returning average
}
//testing method
int main()
{
int size=5;
//declaring arrays
float scores[size],calories[size];
//reading input
int i=0;
cout<<"Enter "<<size<<" scores:";
while(i<size)cin>>scores[i++];//reading input
cout<<" Scores are:";
//calling display method
displayArray(scores,size);
//calling method to compute average
cout<<"Average is:"<<Compute_average(scores,size)<<endl;
i=0;
cout<<"Enter "<<size<<" calories:";
while(i<size)cin>>calories[i++];//reading input
cout<<" calories are:";
//calling display method
displayArray(calories,size);
return 0;
}
output:
Enter 5 scores:1 2 3 4 5
Scores are:1 2 3 4 5
Average is:3
Enter 5 calories:4 5 6 7 8
calories are:4 5 6 7 8
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.