arrays and functions
May 5, 2012 at 1:21am UTC
I am having trouble with the function I wrote, which is using values in an array. I am not sure what I did wrong, as this is my first time to write a function using array values/arrays.
Here is the code:
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
using namespace std;
float calcMean (float [], int );
int main()
{
const int size = 15;
int arrSSN[size];
float mean;
float arrTest1[size];
float arrTest2[size];
float arrTest3[size];
float arrProg1[size];
float arrProg2[size];
float arrProg3[size];
float arrProg4[size];
float arrProg5[size];
float arrProg6[size];
float arrFinal[size];
float arrNumber[size];
char arrLetter[size];
ifstream inc;
inc.open("studentScores.dat" );
if (inc.fail() ) cout << "BAD" << endl;
int i = 0;
int countA = 0;
int countB = 0;
int countC = 0;
int countD = 0;
int countF = 0;
while (!inc.eof()) {
inc >> arrSSN[i];
inc >> arrTest1[i];
inc >> arrTest2[i];
inc >> arrTest3[i];
inc >> arrProg1[i];
inc >> arrProg2[i];
inc >> arrProg3[i];
inc >> arrProg4[i];
inc >> arrProg5[i];
inc >> arrProg6[i];
inc >> arrFinal[i];
//calculate individual overall score
arrNumber[i] = ( (arrTest1[i] + arrTest2[i] + arrTest3[i]) * 50/300) + ( (arrProg1[i] + arrProg2[i] + arrProg3[i] + arrProg4[i] + arrProg5[i] + arrProg6[i]) * 25/300) + (arrFinal[i] * 25/100);
if (arrNumber[i] >= 89.6) {
arrLetter[i] = 'A' ;
countA++;
}
else if (arrNumber[i] >= 79.6) {
arrLetter[i] = 'B' ;
countB++;
}
else if (arrNumber[i] >= 69.6) {
arrLetter[i] = 'C' ;
countC++;
}
else if (arrNumber[i] >= 59.6) {
arrLetter[i] = 'D' ;
countD++;
}
else {
arrLetter[i] = 'F' ;
countF++;
}
i++;
}//loop delimiter
inc.close();
cout << "STUDENT ID" << setw(14) << "NUMERIC GRADE" << setw(14) << "LETTER GRADE" << endl;
for (int c = 0; c < i; c++)
{
cout << arrSSN[c] << setw(10) << fixed << setprecision(2) << arrNumber[c] << setw(10) << arrLetter[c] << endl;
}
cout << "Number of As:" << countA << endl;
cout << "Number of Bs:" << countB << endl;
cout << "Number of Cs:" << countC << endl;
cout << "Number of Ds:" << countD << endl;
cout << "Number of Fs:" << countF << endl;
//calculate overall mean
mean = calcMean(arrNumber, size);
cout <<"Class Average: " << fixed << setprecision(2) << mean << endl;
//determine highest and lowest
float maximum = 0;
float minimum = 100;
for (int i=0; i < size; i++) {
if (arrNumber[i] < minimum)
minimum = arrNumber[i];
if (arrNumber[i] > maximum)
maximum = arrNumber[i];
}
cout << "Maximum: " << maximum << endl;
cout << "Minimum: " << minimum << endl;
return 0;
}
//*************************************calcMean*********************************
//Name: calcMean
//Parameters: arr catches arrNumber
// index catches size
//Return: mean
//
//
//******************************************************************************
float calcMean(float arr[], int index) {
float sum = 0;
float mean;
for (int i = 0; i < index; i++) {
sum += arr[index];
mean = sum/index;
}
cout << mean;
return mean;
}
Output:
I cannot figure out why it is not computing the class average and instead reports a zero.
May 5, 2012 at 11:11am UTC
Perhaps it is because I close the data file too early?
May 5, 2012 at 11:25am UTC
1 2 3 4 5
for (int i = 0; i < index; i++) {
sum += arr[i];
mean = sum/index;
}
mean = sum/index;
Last edited on May 5, 2012 at 11:35am UTC
Topic archived. No new replies allowed.