1 Replies - 31425 Views - Last Post: 02 February 2009 - 11:00 AM Rate Topic: -----

#1 prozkater  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-February 09

c++ program to calculate sum and average marks of 5 subjects from 20 s

Posted 02 February 2009 - 04:45 AM

can anyone help me for my presentation tomorrow.

i have to write a c++ program to input marks of 5 subjects from 20 students and calculate the sum and average marks of all the students using arrays within a class. any help will be very much appreciated.

#include<iostream.h>

class student
	{
	int rno;
	char sname[21];
	float eng,maths,phy,chem,bio;
	float total;
	float ctotal()
	{
		float tot=eng+maths+phy+chem+bio;
		return tot;
	}
	public:
	void takedata();
	void showdata();
	};
	void student::takedata()
	{
	cout<<"\n enter name(max 20 char):";
	cin.getline(sname,21);
	cout<<"\n Enter rollno:";
	cin>>rno;
	cout<<"\n Marks\n";
	cout<<"English:";
	cin>>eng;
	cout<<"Maths:";
	cin>>maths;
	cout<<"Physics:";
	cin>>phy;
	cout<<"Chemistry:";
	cin>>chem;
	cout<<"Biology:";
	cin>>bio;
	cout<<endl;
	total=ctotal();
	}
	void student::showdata()
	{
	cout<<"Roll no:"<<rno<<endl;
	cout<<"\nName:"<<sname<<endl;
	cout<<"\n\tMarks\n";
	cout<<"English:"<<eng<<"\tMaths:"<<maths<<"\tPhysics:"<<phy<<"\tChemistry:"<<chem<<"\tBiology:"<<bio;
	cout<<"\nTotal:"<<total<<endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: c++ program to calculate sum and average marks of 5 subjects from 20 s

#2 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: c++ program to calculate sum and average marks of 5 subjects from 20 s

Posted 02 February 2009 - 11:00 AM

I think that I would start something like this:
#define SUBJECT_COUNT 5 
#define STUDENT_COUNT 20
class school {
  int studentGrade[STUDENT_COUNT][SUBJECT_COUNT];
  string studentName[STUDENT_COUNT];
  string subjectName[] = {"English", "Maths", "Physics", "Chemistry", "Biology"};
  // student[0][0] = student 1 subject 1, student[1][0] = student 2, subject 1, ....
  
  int studentTotal(int studentNumber) {
	int retGrade = 0;
	for(int i = 0; i < SUBJECT_COUNT; i++) 
	  retGrade = retGrade + sudentMarks[studentNumber][i];
	return retGrade;
  }
  float studentAverage(int studentNumber) {
	int sumGrade = 0;
	for(int i = 0; i < SUBJECT_COUNT; i++) 
	  sumGrade = sumGrade + sudentMarks[studentNumber][i];
	return ((float)sumGrade / (float)SUBJECT_COUNT);   
  }
  void getStudentData() {
	for (int i = 0; i < STUDENT_COUNT; i++) {
	  cout<<"\n Enter student " << i << "'s name(max 20 char):";
	  cin.getline(studentName[i],21);
	  cout<<"\n Marks\n";
	  for (int j = 0; j < SUBJECT_COUNT; j++) {
		cout<<  subjectName[j] << ": ";
		cin  >> studentGrade[i][j];
	  }
	}
	cout << "Done collecting data \n";	
  }  
  //...
};

int main() 
{
  school s = new school();
  s->getStudentData();
  //...
  return 0;
}  



Keep in mind that I just quickly wipped this up using your code as a starting point. I did not attempt to compile this in any way. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1