Académique Documents
Professionnel Documents
Culture Documents
Exercise: Write a C++ program to keep records and perform statistical analysis for a class of 20 students. The information of each student contains ID, Name, Sex, quizzes Scores (2 quizzes per semester), mid-term score, final score, and total score. The program will prompt the user to choose the operation of records from a menu as shown below: =================================================== MENU =================================================== 1. 2. 3. 4. 5. 6. 7. 8. Add student records Delete student records Update student records View all student records Calculate an average of a selected students scores Calculate total scores of a selected student Display the highest and lowest scores Sort students records by ID
Note: All students records store in a file stream C++ code solution: #include <cstdlib> #include <iostream>
struct student {
int stnumber; char stname[20]; char sex; float quizz1; float quizz2; float assigment; float midterm; float final; float total; int numberOfitem; };
void displayheading();
void Showmax(); void Showmin(); void Sortbyid(); bool Searchduplicate(int); //Menu contruction
cout<<"============================================"<<"\n"; cout<<" cout<<" cout<<" cout<<" cout<<" cout<<" cout<<" cout<<" cout<<" } void Add(){ 1.Add student records"<<"\n"; 2.Delete student records"<<"\n"; 3.Update student records"<<"\n"; 4.View all student records"<<"\n"; 5.Sort student records by ID"<<"\n"; 6.Sort student records by Total score"<<"\n"; 7.Display average score of a selected student"<<"\n"; 8.Display the highest and the lowest scores"<<"\n"; 9.Search student by ID"<<"\n";
again:
cout<<"\n"; cout<<"Enter student's Name:"; cin>>st[itemcount].stname; cout<<"\n"; cout<<"Enter student's Sex(F or M):";cin>>st[itemcount].sex; cout<<"\n"; cout<<"Enter student's quizz1 score:";cin>>st[itemcount].quizz1; cout<<"\n"; cout<<"Enter student's quizz2 score:";cin>>st[itemcount].quizz2; cout<<"\n"; cout<<"Enter student's assigment score:";cin>>st[itemcount].assigment; cout<<"\n"; cout<<"Enter student's mid term score:";cin>>st[itemcount].midterm; cout<<"\n"; cout<<"Enter student's final score:";cin>>st[itemcount].final; st[itemcount].total=st[itemcount].quizz1+st[itemcount].quizz2+ s[itemcount].assigment+st[itemcount].midterm+st[itemcount].final;
++itemcount;
return match; }
void writedata(){ //save to studentrecords.data file st[0].numberOfitem=itemcount; fstream file("studentrecords.dat",ios::out); file.write((char *)(&st),sizeof(st)); file.close();
cout<<"\n";} i=i+1;
void Delete(){ int id; cout<<"Enter student's ID:"; cin>>id; for(int i=0;i<itemcount;++i){ if((st[i].stnumber==id)&&(itemcount!=0)){
st[i].stnumber=0;
} }
void Update(){ int id; cout<<"Enter student's ID:"; cin>>id; for(int i=0;i<itemcount;++i){ if((st[i].stnumber==id)&&(itemcount!=0)){
cout<<"Enter quizz1 score:";cin>>st[i].quizz1; cout<<"Enter quizz2 score:";cin>>st[i].quizz2; cout<<"Enter assigment score:";cin>>st[i].assigment; cout<<"Enter mid term score:";cin>>st[i].midterm; cout<<"Enter final score:";cin>>st[i].final; st[i].total=st[i].quizz1+st[i].quizz2+st[i].assigment+st[i].midterm+st[i].final; }
void Sortbyid(){ int i, j; for (i = 0; i < itemcount; i++) for (j = itemcount - 1; j > i; j--) if (st[j].stnumber < st[j - 1].stnumber ) { student temp = st[j]; st[j] = st[j - 1]; st[j - 1] = temp; }
for (j = itemcount - 1; j > i; j--) if (st[j].total< st[j - 1].total ) { student temp = st[j]; st[j] = st[j - 1]; st[j - 1] = temp; } } void Displayaverage(){ int id; float avg; cout<<"Enter student's ID:"; cin>>id; for(int i=0;i<itemcount;++i){ if(st[i].stnumber==id){
//st[i].total=st[i].quizz1+st[i].quizz2+st[i].assigment+st[i].midterm+st[i].final; avg=st[i].total/5; } }
void DisplayHL(){
Showmax(); Showmin();
void Showmax(){ float max=st[0].total; for(int i=0;i<itemcount;++i){ for(int j=0;j<itemcount;++j) if(max<st[j].total) max=st[j].total;
} cout<<"The highest score is:"<<max<<"\n"; } void Showmin(){ float min=st[0].total; for(int i=0;i<itemcount;++i){ for(int j=0;j<itemcount;++j) if(st[j].stnumber!=0) if(min>st[j].total) min=st[j].total;
} void Searchbyid(){ int id; cout<<"Enter student's ID:"; cin>>id; for(int i=0;i<itemcount;++i) if(st[i].stnumber==id){ displayheading(); cout<<left<<setw(5)<<st[i].stnumber<<setw(20)<<st[i].stname<<setw(5)<<st[i].sex; cout<<setw(5)<<st[i].quizz1<<setw(5)<<st[i].quizz2<<setw(5)<<st[i].assigment <<setw(5)<<st[i].midterm<<setw(5)<<st[i]. final<<setw(5) <<st[i].total;
cout<<"\n"; } }
//show menu displaymenu(); int yourchoice; char confirm; do { cout<<"Enter your choice(1-9):"; cin>>yourchoice;
switch(yourchoice){ case 1:Add();break; case 2:Delete();break; case 3:Update();break; case 4:ViewAll();break; case 5:Sortbyid();break; case 6:Sortbytotal();break; case 7:Displayaverage();break; case 8:DisplayHL();break; case 9:Searchbyid();break; default:cout<<"invalid!\n"; }
cout<<"Press y or Y to continue:"; cin>>confirm; }while(confirm=='y'||confirm=='Y'); cout<<"Save?y or n:"; char c; cin>>c; if(c=='y'||c=='Y') writedata();
system("PAUSE");
return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { float a, b,c; //declare local variables float x,x1,x2,delta;
if((a==0) && (b==0)) cout<<"No root"<<endl;//no root else if(a==0) //one root { x=-c/b; cout<<"One root:"<<x<<endl;
else{ //calculate delta delta=b*b-4*a*c; if(delta<0) cout<<"No root"<<endl; //no root else{ //two roots x1=-b-sqrt(delta)/(2*a); x2=-b+sqrt(delta)/(2*a); cout<<"x1="<<x1<<"\tx2="<<x2<<endl; } }