In: Computer Science
2. Write a c++ program that takes from the user the number of courses and constructs 3 dynamic 1D arrays with size courses+1. Each array represents a student. Each cell in the array represents a student’s mark in a course. In the last cell of each 1D array you should calculate the average mark of that student. Then output the average mark of all students in each course. Delete any allocated memory. Example Number of courses : 4 50 60 70 20 100 90 80 70 80 90 100 30 Output: 50 60 70 20 50 100 90 80 70 85 80 90 100 30 75 Avg of course 1 = 76.66 Avg of course 2 = 80 Avg of course 3 = 83.33 Avg of course 4 = 40.
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//variable declaration
int courses;
//display message
cout<<"Enter the number of courses: ";
cin>>courses;
float avgStu[3], avgCourse[courses];
//dynamic array declaration
int *std1 = new int[courses+1];
int *std2 = new int[courses+1];
int *std3 = new int[courses+1];
//display message
cout<<"Enter the marks of student: "<<endl;
avgStu[0] = 0;
avgStu[1] = 0;
avgStu[2] = 0;
//input the markes in each of the courses and calculate the
average
for(int j=0; j<courses; j++)
{
cin>>std1[j];
avgStu[0] = avgStu[0] + std1[j];
avgCourse[j] = avgCourse[j] + std1[j];
}
//input the markes in each of the courses and calculate the
average
for(int j=0; j<courses; j++)
{
cin>>std2[j];
avgStu[1] = avgStu[1] + std2[j];
avgCourse[j] = avgCourse[j] + std2[j];
}
//input the markes in each of the courses and calculate the
average
for(int j=0; j<courses; j++)
{
cin>>std3[j];
avgStu[2] = avgStu[2] + std3[j];
avgCourse[j] = avgCourse[j] + std3[j];
}
//set the student average at the last column
std1[courses] = avgStu[0] / (float) courses;
std2[courses] = avgStu[1] / (float) courses;
std3[courses] = avgStu[2] / (float) courses;
//display the result
cout<<endl<<"Student 1 ";
for(int j=0; j<=courses; j++)
{
cout<<setw(5)<<std1[j]<<" ";
}
cout<<endl<<"Student 2 ";
for(int j=0; j<=courses; j++)
{
cout<<setw(5)<<std2[j]<<" ";
}
cout<<endl<<"Student 3 ";
for(int j=0; j<=courses; j++)
{
cout<<setw(5)<<std3[j]<<" ";
}
cout<<endl<<"Average: ";
for(int i=0; i<courses; i++)
cout<<setw(5)<<setprecision(2)<<avgCourse[i]/3.0<<"
";
return 0;
}
OUTPUT: