Give a brief analysis on the code given below and a summary #include \"stdafx.h\
ID: 3911309 • Letter: G
Question
Give a brief analysis on the code given below and a summary
#include "stdafx.h"
#include <iostream>
using namespace std;
#define students 3
#define exams 4
void showchoices() {
cout << "Enter a choice" << endl;
cout << "0 Print the array of grades" << endl;
cout << "1 Print sum of each column" << endl;
cout << "3 Print average of each grades" << endl;
cout << "4 End the Program" << endl;
}
void printArray(int grades[][exams]) {
cout << "Array having following content" << endl;
for (int i = 0; i < students; i++) {
for (int j = 0; j < exams; j++) {
cout << grades[i][j] << "";
}
cout << endl;
}
cout << endl;
}
void sumOfrow(int grades[][exams]) {
int sum;
for (int i = 0; i < students; i++) {
sum = 0;
for (int j = 0; j < exams; j++) {
sum += grades[i][j];
}
cout << "Sum of row" << i + 1 << "is" << sum << endl;
}
cout << endl;
}
void sumOfcol(int grades[][exams]) {
int sum;
for (int i = 0; i < exams; i++) {
sum = 0;
for (int j = 0; j < students; j++) {
sum += grades[j][i];
}
cout << "Sum of Column" << i + 1 << "is" << sum << endl;
}
cout << endl;
}
void getAverage(int grades[][exams]) {
int sum = 0;
for (int i = 0; i < students; i++) {
for (int j = 0; j < exams; j++) {
sum += grades[i][j];
}
double average = sum;
average = average / (students*exams);
cout << "average is:" << average << endl;
}
cout << endl;
}
void processGrades(int grades[][exams], int choice) {
if (choice == 0) {
printArray(grades);
}
else if (choice == 1) {
sumOfrow(grades);
}
else if (choice == 2) {
sumOfcol(grades);
}
else if (choice == 3) {
getAverage(grades);
}
else if (choice != 4) {
cout << "Invalid choice, Try Again.";
}
}
int main()
{
int choice = 0;
int studentGrades[students][exams] = {
{77,68,86,73},
{96,87,89,78},
{70,90,86,81} };
while (choice != 4) {
showchoices();
cin >> choice;
processGrades(studentGrades, choice);
}
return 0;
}
Explanation / Answer
/*
Summary:
The program processes the data for three students.Each student has 3
exam grades.The program didplays the grades of students as a 2-d
array.Each row represents one student where as each column shows each of the exam.It calculates the total sum for each exam (i.e columnwise).It calculates
the average grade for all the students and all the exams combined
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
#define students 3
#define exams 4
/*The function displays all the menu options */
void showchoices() {
cout << "Enter a choice" << endl;
cout << "0 Print the array of grades" << endl;
cout << "1 Print sum of each column" << endl;
cout << "3 Print average of each grades" << endl;
cout << "4 End the Program" << endl;
}
/*The function prints the grades of all the students.Students
are shown rwo wise with four exams in a row */
void printArray(int grades[][exams]) {
cout << "Array having following content" << endl;
for (int i = 0; i < students; i++) {
for (int j = 0; j < exams; j++) {
cout << grades[i][j] << "";
}
cout << endl;
}
cout << endl;
}
/*This function calcultes the sum of a row.It means total sum of
all the exams for a student */
void sumOfrow(int grades[][exams]) {
int sum;
for (int i = 0; i < students; i++) {
sum = 0;
for (int j = 0; j < exams; j++) {
sum += grades[i][j];
}
cout << "Sum of row" << i + 1 << "is" << sum << endl;
}
cout << endl;
}
/*This function calcultes the sum of a clolumn.It means total sum of
all a particular exam for all students */
void sumOfcol(int grades[][exams]) {
int sum;
for (int i = 0; i < exams; i++) {
sum = 0;
for (int j = 0; j < students; j++) {
sum += grades[j][i];
}
cout << "Sum of Column" << i + 1 << "is" << sum << endl;
}
cout << endl;
}
/*This function calcultes the average grade of all the exams including
all the students. */
void getAverage(int grades[][exams]) {
int sum = 0;
for (int i = 0; i < students; i++) {
for (int j = 0; j < exams; j++) {
sum += grades[i][j];
}
double average = sum;
average = average / (students*exams);
cout << "average is:" << average << endl;
}
cout << endl;
}
/*This function acts as per the choosen menu.As per the menu
it calls appropriate functions. */
void processGrades(int grades[][exams], int choice) {
if (choice == 0) {
printArray(grades);
}
else if (choice == 1) {
sumOfrow(grades);
}
else if (choice == 2) {
sumOfcol(grades);
}
else if (choice == 3) {
getAverage(grades);
}
else if (choice != 4) {
cout << "Invalid choice, Try Again.";
}
}
/* This is the main function where data gets populated for
the exams of each student.Menu choices are displayed and
appropriate functions are called with the help pf processGrades
function which takes choice and the 2-d array consisting of
exam grades as inputs. */
int main()
{
int choice = 0;
int studentGrades[students][exams] = {
{77,68,86,73},
{96,87,89,78},
{70,90,86,81} };
while (choice != 4) {
showchoices();
cin >> choice;
processGrades(studentGrades, choice);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.