Saturday, 25 February 2012

Simple code for structures in functions

#include<iostream>
#include<string>
using namespace std;

struct student
{
string name;
int marks;
char grade;
};

//declarationz
void get_stud(student list[]);
void stud_grade(student list[]);
void stud_toper(student list[]);

//main funtion.
int main()
{
const int size=10;
student stud[size];
get_stud(stud);
stud_grade(stud);
stud_toper(stud);
cout<<endl;
return 0;
}

//definationz

//function 2 get name n marks of studentz from user.
void get_stud(student list[])
{
for(int i=0;i<10;i++)
{
cout<< "enter student's name for index "<<i<<" = "<<endl;
cin>>list[i].name;
cout<< "enter student's marks for index "<<i<<" = "<<endl;
cin>>list[i].marks;
}


//function to find grade.
void stud_grade(student list[])
{
for(int i=0;i<10;i++)
{
if(list[i].marks >= 80)
{
list[i].grade='A';
cout<<"grade of student at "<<i<<" index="<<list[i].grade<<endl;
}
else if(list[i].marks >= 70)
{
list[i].grade='B';
cout<<"grade of student at "<<i<<" index="<<list[i].grade<<endl;
}
else if(list[i].marks >= 60)
{
list[i].grade='C';
cout<<"grade of student at "<<i<<" index="<<list[i].grade<<endl;
}
else if(list[i].marks >= 50)
{
list[i].grade='D';
cout<<"grade of student at "<<i<<" index="<<list[i].grade<<endl;
}
else if(list[i].marks >= 40)
{
list[i].grade='E';
cout<<"grade of student at "<<i<<" index="<<list[i].grade<<endl;
}
else
{
list[i].grade='F';
cout<<"grade of student at "<<i<<" index="<<list[i].grade<<endl;
}
}
cout<<endl;
}

//function to find topper student.
void stud_toper(student list[])
{
int max=0;
for(int i=0;i<10;i++)
{
if(list[max].marks<list[i].marks)
max=i;

}
cout<< "The toper student is \""<<list[max].name<< "\" having "<<list[max].marks<< " marks and "<<list[max].grade<<" grade."<<endl;
}

No comments:

Post a Comment