Write a program that has a two-dimensional 3 by 5 integer array of gymnast scores in a competition. The scores must all be in the range of 0-10. Include nested loops to read in values into the array. Compute and print the average score for each gymnast in a seperate void function. Compute and print the overall score for each gymnast as well as the overall average(sum all 15 scores and divide by 15) in a seperate void function.
I have tried the problem but I don't know how to do the functions and everything messes up. I think there should be 5 gymnasts with three scores per each. Thank you!
1
Expert's answer
2017-10-10T15:37:07-0400
#include <iostream> using namespace std; void printGymnastAverage(int i, int score[]); void printAverage(int score[][5]); int main(void) { int score[3][5]; // two-dimensional 3 by 5 integer array of gymnast scores in a competition // nested loops to read in values into the array for(int i=0; i<3; i++) { for(int j=0; j<5; j++) { int s; do { cout << "Enter score " << (j+1); cout << " for gymnast " << (i+1) << ": "; cin >> s; }while(s<0 || s>10); // The scores must all be in the range of 0-10 score[i][j] = s; } } // Compute and print the average score for each gymnast for(int i=0; i<3; i++) { printGymnastAverage(i, score[i]); } cout << endl; // Compute and print the overall score for each gymnast as well as the overall average //(sum all 15 scores and divide by 15) printAverage(score); return 0; } void printGymnastAverage(int i, int score[]) { int sum = 0; for(int j=0; j<5; j++) { sum += score[j]; } cout << "Average score for gymnast " << (i+1) << " is : " << (double)sum/5.0 << endl; } void printAverage(int score[][5]) { int sum = 0; for(int i=0; i<3; i++) { for(int j=0; j<5; j++) { sum += score[i][j]; } } cout << "Overall Average score is : " << (double)sum/15.0 << endl; }
Comments
Leave a comment