Help with functions and arrays
Jun 21, 2011 at 9:41am UTC
Hi there I am currently trying to calculate an output based on an array passed into a function.
However I am stuck with how to pass the array into the function. I have the code written in the function to do what is required, however I cannot send in the information required to output. Please help. The c++ file inputs information from a file and is stored into multiple arrays. The code for the program is follows:
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
//An educational institution has a file that contains a list of student IDs, names and marks for
//eight subjects. The educational institution needs to know certain statistics based on this
//data.
#include <fstream>
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;
class Student
{
public :
int studentID;
string studentFirstName;
string studentLastName;
Student::Student(int ,string,string);
Student::Student(){};
int getID(){return studentID;};
string getFirstName(){return studentFirstName;};
string getLastName(){return studentLastName;};
};
Student::Student(int inID, string inFirstName, string inLastName)
{
Student newStudent[12];
for (int i=0;i<12;i++)
{
newStudent[i].studentID=inID;
newStudent[i].studentFirstName=inFirstName;
newStudent[i].studentLastName=inLastName;
}
}
class Grade : public Student
{
public :
int grade[8];
string subject;
double stuAverage;
double stuTotals;
Grade::Grade(int );
Grade::Grade(string);
Grade::Grade(){};
double getStudentAverage()
{
stuTotals = 0;
for (int i=0;i<8;i++)
{
stuTotals = (stuTotals + grade[i]);
}
stuAverage = stuTotals/8;
return stuAverage;
};
double getStudentTotal()
{
stuTotals = 0;
for (int i=0;i<8;i++)
{
stuTotals = (stuTotals + grade[i]);
}
return stuTotals;
};
string getSubject()
{
return subject;
};
int getValue(int inGrade){return grade[inGrade];};
};
Grade::Grade(int inGrade)
{
for (int i=0;i<8;i++)
{
grade[i]=inGrade;
}
};
Grade::Grade(string inSubject)
{
subject=inSubject;
};
class SubjectAverage : public Student
{
public :
string subjectName;
double countMarks;
double totalMarks;
void calculateAverageSubjects(Grade subjects[])
{
string name;
double total=0;
int matchedElements=0;
SubjectAverage avgArray[12];
bool found=false ;
for (int i=0;i<12;i++) //initialise array of SubjectAverages
{
avgArray[i].subjectName=="" ; //
avgArray[i].countMarks=0;
avgArray[i].totalMarks=0;
}
for (int i=0;i<12;i++) //for each record in array
{
name=subjects[i].subject; //get subject name from array
for (int j=0;j<8;j++) //for each if the grades
{
total=total+subjects[i].grade[j]; //get total of grades
}
int index=0; //index is the index into the subject averages array
while (index <= matchedElements && ! found) //search array of averages from beginning
{
if (avgArray[index].subjectName==name) //if matched with the current object name, not new so add total and increment count
{
found=true ;
avgArray[index].countMarks=avgArray[index].countMarks+8;
avgArray[index].totalMarks=avgArray[index].totalMarks+total;
}
index=index+1;
}
if (! found) //else write new record to array to the subject average Array
{
avgArray[matchedElements].subjectName=name;
avgArray[matchedElements].totalMarks=total;
avgArray[matchedElements].countMarks=8;
matchedElements++;
}
total=0;
found=false ;
}
for (int j=0;j<matchedElements;j++)
{
cout<<avgArray[j].subjectName<<" " <<(avgArray[j].totalMarks/avgArray[j].countMarks)<<endl;
}
}
};
int main()
{
Grade newStudent[12];
Grade results[8];
ofstream outFile("results.txt" , ios::app);
ifstream inData ("grades.txt" );
if (!outFile)
{
cout << "Cannot open export file.\n" ;
return 1;
}
if (!inData)
{
cout << "Cannot open input file.\n" ;
return 1;
}
int i=0;
for (i=0;i<12;i++)
{
inData>>newStudent[i].studentID;
inData>>newStudent[i].studentFirstName;
inData>>newStudent[i].studentLastName;
for (int k=0;k<8;k++)
{
inData>>newStudent[i].grade[k];
Grade(newStudent[i].grade[k]);
}
inData>>newStudent[i].subject;
Grade(newStudent[i].subject);
Student(newStudent[i].studentID, newStudent[i].studentFirstName, newStudent[i].studentLastName);
}
cout<<fixed<<setprecision(2);
for (int i=0;i<12;i++)
{
outFile<<newStudent[i].getID()<<" " <<newStudent[i].getFirstName()<<" " <<newStudent[i].getLastName()<<endl;
outFile<<" " ;
for (int j=0;j<8;j++)
{
outFile<<newStudent[i].getValue(j)<< " " ;
}
outFile<<" Average Mark: " <<newStudent[i].getStudentAverage()<<endl;
}
for (int i=0;i<12;i++)
{
cout<<newStudent[i].getID()<<" " <<newStudent[i].getLastName()<<" " <<newStudent[i].getSubject()<<" " <<newStudent[i].getStudentAverage()<<endl;
}
cout<<"\n \n ------------ Average Mark per Subject ------------" <<endl;
cout<<"Math: " <<"??" <<endl;
cout<<"Programming: " <<"??" <<endl;
cout<<"English: " <<"??" <<endl;
cout<<"Sport: " <<"??" <<endl;
cout<<"Drama: " <<"??" <<endl;
cout<<"Religion: " <<"??" <<endl;
cout<<"Science: " <<"??" <<endl;
cout<<"\n \n ------------ Number of Students per Subject ------------" <<endl;
cout<<"Math: " <<"??" <<endl;
cout<<"Programming: " <<"??" <<endl;
cout<<"English: " <<"??" <<endl;
cout<<"Sport: " <<"??" <<endl;
cout<<"Drama: " <<"??" <<endl;
cout<<"Religion: " <<"??" <<endl;
cout<<"Science: " <<"??" <<endl;
cout<<"\n\nThe grand total average of all subjects: " <<"??" <<endl;
calculateAverageSubjects(subject);
return 0;
}
Where it is outputting the subject average = ?? is what will be replaced by the call to calculateAverageSubjects(array).
Can someone please tell me where I have gone wrong.
Topic archived. No new replies allowed.