Given the sparse table that connects between s students and c courses and assigns a grade to each student in a course.
a)define a proper node for the above table.
b)define a class sparse table class, with proper data members and the following member functions
-function add student to a course
-function find number of courses taken by praticular student.
-function average of grades in a praticular course.
-function number of fail grades in particular course.
-function average of a student.
CODE
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class course
{
public:
course(string z,int c)
{
Namecourse=z;
Markcourse=c;
next=NULL;
}
course()
{next=NULL; }
string Namecourse;
int Markcourse;
course *next;
};
class Stundents :public course
{
public:
int NoStundent;
string NameStundent;
Stundents *nextStundent;
course *headcourses;
course *tailcourses;
Stundents(){
nextStundent=NULL;
headcourses=NULL;
tailcourses=NULL;
}
Stundents(int x,string y,int a,string z[],int c[]){
NoStundent=x;
NameStundent =y;
nextStundent=NULL;
headcourses=NULL;
tailcourses=NULL;
for(int i=0;i<a;i++)
{
if(tailcourses == NULL)
{
tailcourses =new course(z[i],c[i]);
headcourses=tailcourses;
}
else
{
tailcourses->next=new course(z[i],c[i]);
tailcourses=tailcourses->next;
}}
}
};
class Opertors
{
private:
Stundents *head;
Stundents *tail;
public:
Opertors()
{
head=NULL;
tail=NULL;
}
void AddStudent();
void Print();
void SearchByNo(int x);
void SearchByCourse(string x);
};
void main()
{
Opertors n;
int z;
n.AddStudent();
cout<<"Enter no.student : ";
cin>>z;
n.SearchByNo(z);
string x;
cout<<"Enter Course Name:"<<endl;
cin>>x;
n.SearchByCourse(x);
}
void Opertors::AddStudent()
{
ifstream input;
input.open("data.dat");
int x;string y;int a;string z[20];int c[20];
for(int i=0;i<3;i++)
{
input>>x>>y>>a;
for(int j=0;j<a;j++)
{
input>>z[j];
input>>c[j];
}
if(tail == NULL)
{
tail=new Stundents(x,y,a,z,c);
head=tail;
}
else
{
tail->nextStundent=new Stundents(x,y,a,z,c);
tail=tail->nextStundent;
}
}
input.close();
}
void Opertors::Print()
{
Stundents *head1=head;
while(head1 !=NULL)
{
cout<<head1->NoStundent<<" "<<head1->NameStundent<<" ";
course *h2=head1->headcourses;
while(h2!=NULL)
{
cout<<h2->Namecourse<<" "<<h2->Markcourse<<" ";
h2=h2->next;
}
head1=head1->nextStundent;
cout<<endl;
}
}
void Opertors::SearchByNo(int x)
{
Stundents *head1=head;
while(head1 !=NULL)
{
if (head1->NoStundent == x)
{
cout<<head1->NoStundent<<" "<<head1->NameStundent<<" ";
course *h2=head1->headcourses;
while(h2!=NULL)
{
cout<<h2->Namecourse<<" "<<h2->Markcourse<<" ";
h2=h2->next;
}
cout<<endl;
break;
}
head1=head1->nextStundent;
}
}
void Opertors::SearchByCourse(string x)
{
Stundents *head1=head;
while(head1 !=NULL)
{
course *h2=head1->headcourses;
while(h2 != NULL)
{
if(x == h2->Namecourse)
cout<<head1->NameStundent<<" "<<h2->Markcourse<<endl;
h2=h2->next;
}
head1=head1->nextStundent;
}
}