Adding Arrays
Oct 10, 2010 at 3:17pm UTC
Hey Everyone,
I wrote a program to practise inheritance. It basically asks the student to enter his exam results for mid-term and end-term for an array of subjects and it calculates the average according to this statement:
1 2 3 4 5 6 7
void Average::getAverage()
{
for (i=0;i<9;i++)
{
average[i] = ((MidMarks[i]) + (FinalsMarks[i]))/2;
}
}
The problem is adding two arrays produces a memory address. I know why this happens - but I don't know how to fix it, could anyone help me out with that.
If it is necessary, this is the complete 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
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int i;//global variable
class Student
{
public :
void getData();
void printData();
private :
string name;
int ID;
};
void Student::getData()
{
cout<<"Enter Name: " ; cin>>name;
cout<<"Enter ID: " ; cin>>ID;
}
void Student::printData()
{
cout<<"Name: " <<name<<"\t" <<"ID: " <<ID<<endl;
}
class Exam : public Student
{
public :
void getMids();
void getFinals();
protected :
int MidMarks[8];
int FinalsMarks[8];
};
void Exam::getMids()
{
cout<<"Enter Marks received in the Mid-Term exams for following subjects: " <<endl;
for (int i=0;i<9;i++)
{
cout<<"\tSubject " <<i+1<<":\t" ; cin>>MidMarks[i];
}
}
void Exam::getFinals()
{
cout<<"Enter Marks received in the End-Term exams for following subjects: " <<endl;
for (int i=0;i<9;i++)
{
cout<<"\tSubject " <<i+1<<":\t" ; cin>>FinalsMarks[i];
}
}
class Average : public Exam
{
public :
void getAverage();
protected :
int average[8];
};
void Average::getAverage()
{
for (int i=0;i<9;i++)
{
average[i] = ((MidMarks[i]) + (FinalsMarks[i]))/2;
}
}
class Results : public Average
{
public :
Results();
void displayResults();
};
Results::Results()
{
getData();
getMids();
getFinals();
getAverage();
system("clear" );
printData();
}
void Results::displayResults()
{
cout<<"Subject\t\tMid Term Marks(%)\tEnd Term Marks(%)\tAverage" <<endl;
cout<<"----------------------------------------------------------------------------------" <<endl;
for (int l=0;l<9;l++)
{
cout<<l+1<<"\t\t" <<MidMarks[l]<<"\t\t\t" <<FinalsMarks[l]<<"\t\t\t" <<average<<endl;
}
}
int main()
{
Results Person;
Person.displayResults();
return 0;
}
Oct 10, 2010 at 3:43pm UTC
1 2 3
int average[8]
int FinalsMarks[8];
for (int i=0;i<9;i++)
remember that valid index for these arrays are 0 up to 7
Topic archived. No new replies allowed.