write a C ++ program to Create a 2-dimensional array with 4 rows and 30 columns.
ID: 3582137 • Letter: W
Question
write a C ++ program to Create a 2-dimensional array with 4 rows and 30 columns. Row represents sections of a course and column represents the students, value inside each position of the array is the Final exam grade for each students. Fill the array with random numbers between 40 and 100. Calculate the total, average, maximum, minimum for each section. 1.5. Generate a histogram for all the grades. Each score represents one dot on the histogram. To do that, first create an array of size 61 and then stores the count of each scores in the array .
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <time.h>
#include <cstdlib>
using namespace std;
int random_num()
{
static bool first = true;
if ( first )
{
srand(time(NULL));
first = false;
}
return 40 + rand() % (101 - 40);
}
void find_total(int a[4][30]) {
int total[30];
for(int i=0;i<30;i++) {
total[i]=0;
}
for(int i=0;i<30;i++) {
for(int j=0;j<4;j++) {
total[i]+=a[j][i];
}
}
cout<<" Total : ";
for(int i=0;i<30;i++) {
cout<<total[i]<<" ";
}
}
void find_max(int a[4][30]) {
int max[30];
for(int i=0;i<30;i++) {
int m=a[0][i];
for(int j=0;j<4;j++) {
if(a[j][i]>m) {
m=a[j][i];
}
}
max[i]=m;
}
cout<<" Maximum : ";
for(int i=0;i<30;i++) {
cout<<max[i]<<" ";
}
}
void find_min(int a[4][30]) {
int min[30];
for(int i=0;i<30;i++) {
int m=a[0][i];
for(int j=0;j<4;j++) {
if(a[j][i]<m) {
m=a[j][i];
}
}
min[i]=m;
}
cout<<" Minimum : ";
for(int i=0;i<30;i++) {
cout<<min[i]<<" ";
}
}
void find_avg(int a[4][30]) {
int avg[30];
for(int i=0;i<30;i++) {
int sum=0;
for(int j=0;j<4;j++) {
sum+=a[j][i];
}
avg[i]=sum/4;
}
cout<<" Average : ";
for(int i=0;i<30;i++) {
cout<<avg[i]<<" ";
}
}
void make_histogram(int a[4][30]) {
int histogram[61],c=0;
for(int i=0;i<61;i++) {
histogram[i]=0;
}
for(int i=40;i<=100;i++) {
for(int j=0;j<4;j++) {
for(int k=0;k<30;k++) {
if(a[j][k]==i) {
histogram[c]++;
}
}
}
c++;
}
cout<<" Histogram : ";
for(int i=0;i<61;i++) {
cout<<histogram[i]<<" ";
}
}
int main()
{
int a[4][30];
cout<<"Filling the array with random numbers... ";
for(int i=0;i<4;i++) {
for(int j=0;j<30;j++) {
a[i][j]=random_num();
}
}
for(int i=0;i<4;i++) {
cout<<endl;
for(int j=0;j<30;j++) {
cout<<a[i][j]<<" ";
}
}
find_total(a);
find_max(a);
find_min(a);
find_avg(a);
make_histogram(a);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.