Working with 2D arrays
mattzorx (21)
Nov 3, 2009 at 2:17am UTC
I am working on a project using 2D arrays to display a class's individual grades, averages, and a class average. I am able to display the grades and class average, but my STUDENT AVERAGE Letter Grade is returning a number, when I thought my if else ladder was telling it to return the Char. Any help would be much appreciated.
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
#include <iostream>
#include <iomanip>
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 );
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];
}
}
//display scores with function
showNamesAndScores(scores, names, rows);
//display avg
double avg = calcAvg(scores, rows);
cout<<"Avg: " <<avg<<endl;
//display LetterGrade
double sAvg = studentAvg(scores, avg);
cout<<"Letter Grade: " <<sAvg<<endl;
}//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)
{
double avg;
int sum = 0;
char grade;
for (int i=0; i<rows; i++)
{
sum += scores[i][columns];
}
avg = sum/rows;
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' ;
return grade;
}//endstudentAvg
Last edited on Nov 3, 2009 at 7:20am UTC
mbittel12 (21)
Nov 3, 2009 at 4:13am UTC
I believe it is something very simple that I am also overlooking. In the bottom of your function studentAvg could you put in a cout << grade to see what it does and let me know.
mattzorx (21)
Nov 3, 2009 at 4:22am UTC
apparently these kids ace their tests. i input 100s for all grades. should be an A where that F is right?
[Session started at 2009-11-02 22:20:14 -0600.]
Enter Name of student1: bob
Enter Name of student2: tim
Enter Name of student3: john
Enter Name of student4: matt
Enter Name of student5: alice
Enter 4 scores for row 1: 100 100 100 100
Enter 4 scores for row 2: 100 100 100 100
Enter 4 scores for row 3: 100 100 100 100
Enter 4 scores for row 4: 100 100 100 100
Enter 4 scores for row 5: 100 100 100 100
bob 100 100 100 100
tim 100 100 100 100
john 100 100 100 100
matt 100 100 100 100
alice 100 100 100 100
Avg: 100
F Letter Grade: 70
The Debugger has exited with status 0.
mattzorx (21)
Nov 3, 2009 at 7:00am UTC
UPDATE:
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
#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
Topic archived. No new replies allowed.