A class STUDENT has 3 data members: Name, Roll Number, Marks of 5 subjects, Stream and member functions to input and display data. It also has a function member to assign stream on the basis of the table given below: Average Marks Stream 96% or more Computer Science 91% - 95% Electronics 86% - 90% Mechanical 81% - 85% Electrical 75% - 80% Chemical 71% - 75% Civil Declare a structure STUDENT and define the member functions. Write a program to define a structure STUDENT and input the marks of n (<=20) students and for each student allot the stream. (Don't use any array).
Answers
Answer:
#include <iostream>
#include <string>
using namespace std;
class student
{
char s_name[50];
int s_rno;
float s_marks[5];
char s_stream[25];
void assign()
{
float average=this->avg();
if(average>70&&average<76) strncpy(stream,"Civil",25);
else
if(average>75&&average<81) strncpy(stream,"Chemical",25);
else
if(average>80&&average<86) strncpy(stream,"Electrical",25);
else
if(average>85&&average<91) strncpy(stream,"Mechanical",25);
else
if(average>90&&average<96) strncpy(stream,"Electronics",25);
else
if(average>=96) strncpy(stream,"Computer Science",25);
else strncpy(stream,"No stream",25);
}
public:
student() {};
student(student &r)
{
rno=r.rno;
for(int i=0;i<5;i++,marks[i]=r.marks[i]);
strncpy(stream,r.stream,25);
strncpy(name,r.name,50);
}
void input()
{
cout<<"Enter roll no\n";
cin>>rno;
cin.ignore(1,'\n');
cout<<"ENter name\n";
cin.getline(name,50);
cout<<"Enter marks in five subjects\n";
for(int i=0;i<5;i++,cin>>marks[i]);
assign();
}
void display()
{
cout<<"\nROll no: "<<rno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Average : "<<student::avg()<<" %"<<endl;
cout<<"Stream: "<<stream<<endl;
}
float avg()
{
float average=0;
for(int i=0;i<5;i++,average+=marks[i]);
average/=5;
cout<<"FUNCTION AVERAGE(): AVG= "<<average<<endl; //Diagnostic message
return (average);
}
void search(student *,int n);
};
void student::search(student *a,int n)
{
int i=0;
int found=0;
char nm[50];
cin.ignore(1,'\n');
cout<<"ENter name to search\n";
cin.getline(nm,50);
while(i<n&&(!found))
{
if(strcmp(nm,a[i].name)==0)
{
found=1;
}
i++;
}
if(found) a[i].display();
else cout<<"NOT FOUND\n\n";
}
void highavg(student *stud,int n)
{
cout<<n<<endl;
float max=stud[0].avg();
int p=0;
for(int i=1;i<n;i++)
{
if(max<stud[i].avg())
{
max=stud[i].avg();
p=i;
}
}
Explanation: