1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//file level scope
const int columns = 4;
const int nColumns = 20;
//prototype for 2d array
void showNamesAndScores(int scores[][columns], char names[][nColumns], int);
//calcAvg
double calcAvg(int X[][columns], int);
//studentAvg
char studentAvg(int X[][columns], int);
//deviation
void deviation(int X[][columns], double Y[][columns], int, double);
//variance
void variance(double Y[][columns],int);
int main()
{
//declare rows & columns
const int rows = 5;
//declare 2d array
int scores[rows][columns];
char names[rows][nColumns];
//populate 2d array
for(int i=0; i<rows ;i++)
{
cout<<"Enter Name of student"<<i+1<<": ";
cin>>names[i];
cout<<endl;
}
for(int i=0; i<rows; i++)
{
cout<<"Enter "<<columns<<" scores for row "<<i+1<<": ";
for (int j=0; j<columns; j++)
{
cin>>scores[i][j];
}
cout<<endl;
}
//display scores with function
showNamesAndScores(scores, names, rows);
//display avg
double avg = calcAvg(scores, rows);
deviation(scores, devi, rows, avg); //error: 'devi' not declared in this scope. ???
cout<<"Class Avg: "<<avg<<endl;
variance(devi,rows); //error: 'devi' not declared in this scope. ???
}//endmain
//defines a function to accept 2d array
void showNamesAndScores(int scores[][columns], char names[][nColumns], int rows)
{
for(int i=0; i<rows; i++)
{
cout<<names[i];
cout<<setw(15);
for(int j=0; j<columns; j++)
{
cout<<scores[i][j]<<" ";
}
cout<<endl;
}
}//end names and scores
//calcAvg
double calcAvg(int X[][columns], int s)
{
double a;
double sum = 0;
for(int i=0; i<s; i++)
{
for(int j=0; j<columns; j++)
{
sum += X[i][j];
}
}
a = sum/(s*columns);
return a;
}//endcalcAvg
//studentAvg
char studentAvg(int scores[][columns], int rows)
{
int sum = 0;
char grade;
for(int j=0; j<columns; j++)
{
sum += scores[rows][j];
}
double avg = sum/rows;
cout<<"avg"<<avg;
if(avg >= 90)
grade = 'A';
else if(avg >= 80 && avg < 90)
grade = 'B';
else if(avg >= 70 && avg < 80)
grade = 'C';
else if(avg >= 60 && avg < 70)
grade = 'D';
else
grade = 'F';
cout<<"Letter Grade: ";
return grade;
}//endstudentAvg
//calcDeviation
void deviation(int scores[][columns],double devi[][columns], int rows, double avg)
{
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
devi[i][j] = scores[i][j] - avg;
}
}
}//endDeviation
//calcVariance
void variance(double devi[][columns], int rows)
{
double v = 0;
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
v += pow(devi[i][j], 2);
}
}
v= v/rows;
cout<<"Variance: "<<v<<endl;
}//endVariance
|