You are on page 1of 7

ITEI222 GROUP FINAL TERM PROJECT

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, e!, "ui##es cores $2 "ui##es per semester%, mid&term score, final score, and total score. The program 'ill prompt the user to choose the operation of records from a menu as sho'n (elo')

============================================== MENU ================================================ 1. 2. 3. 4. 5. #. (. ). .. /. Add student records Delete student records Update student records View all student records Calculate an avera e o! a selected student"s scores $%ow student w%o ets t%e &a' total score $%ow student w%o ets t%e &in total score *ind student +, -D $ort records +, total scores 0ro ra& E'it

Enter ,our c%oice11 Note: All students records store in an array of structures Solution: To keep this solution simple and easy to follow, we divide this solution in to different steps: Step 1: Declaring a structure called student to store the records. The structure student contains nine members: -stnumber string! to store the student" id -stname array of characters! to store the student"s name -se# char! to store the student"s se# -$ui%& float! to store the first $ui% score -$ui%' float! to store the second $ui% score -assignment float! to store the assignment score -midterm float! to store the mid-term score -final float! to store the final score -total float! to store the total score

t,pede! struct student 2 strin stnu&+er3 c%ar stna&e42563 c%ar se'3 !loat 7ui813 !loat 7ui823 !loat assi n&ent3 !loat &idter&3 !loat !inal3 !loat total3 93

Step 3: Defining the add(rec struct student)* st, int+ itemcount! function to add a new record to the the array of student ob,ects. This method takes two arguments. The first argument is the array of student ob,ects st! and the second argument is the number of items in the array. This function firstly checks the new record using the search function that is defined in the ne#t step! before it is allowed to be appended to the array to avoid duplicate records. -hen the new item is added the value of itemcount variable increases by & that means the number of records in the list increases.
void add:rec; struct student st4 6<int= ite&count> 2 a ain1 cout??@AnEnter studentBs -D1@3 cinCCst4ite&count6.stnu&+er3 i!;searc%;st<st4ite&count6.stnu&+er<ite&count>D=E1>2 cout??@F%is -D alread, e'istsAn@3 oto a ain3 9 cout??@Enter studentBs Na&e1@3 cinCCst4ite&count6.stna&e3 cout??@Enter studentBs $e';* or M>1@3cinCCst4ite&count6.se'3 cout??@Enter studentBs 7ui881 score1@3cinCCst4ite&count6.7ui8813 cout??@Enter studentBs 7ui882 score1@3cinCCst4ite&count6.7ui8823 cout??@Enter studentBs assi &ent score1@3cinCCst4ite&count6.assi &ent3 cout??@Enter studentBs &id ter& score1@3cinCCst4ite&count6.&idter&3 cout??@Enter studentBs !inal score1@3cinCCst4ite&count6.!inal3 st4ite&count6.total=st4ite&count6.7ui881Gst4ite&count6.7ui882Gst4ite&count6.a ssi &entGst4ite&count6.&idter&Gst4ite&count6.!inal3 GGite&count3

Step 4: Defining the search struct student st)*, strng id, int itemcount! method to search for the inde# of a target record. This method is useful as we need it to find the location of the target record in the array of student ob,ects. .t can help us to make sure the record does e#it before we allow the record for deletion or updating. .f the target element is found, the method returns the inde# of this element. .t return -&, if the target element is not found in the array.
int searc%;struct student st46< strin id<int ite&count>2

int !ound =E13 !or ;int i = 53 i ? ite&count == !ound==E13 iGG> 2 i! ;st4i6.stnu&+er == id> !ound=i3 else !ound=E1 3 9 return !ound3 9

Step 5: Defining the viewall student st)*, int itemcount! method to display the list of all records in the set. To display all records, we need a while loop to traverse through the array of student ob,ects.
HH!unction to view all records void viewall;struct student st46< int ite&count>2 int i=53 cout??le!t??setw;5>??@-D@??setw;25>??@NAME@??setw;5>??@$E/@ ??setw;5>??@I1@ ??setw;5>??@I2@??setw;5>??@As@??setw;5>??@Mi@??setw;5>??@*i@ ??setw;5>??@FJFAK@??@An@3 cout??@==========================================An@3 w%ile;i?=ite&count>2 i!;st4i6.stnu&+erD=@@>2 cout??le!t??setw;5>??st4i6.stnu&+er??setw;25>??st4i6.stna&e??setw;5> ??st4i6.se'3 cout??setw;5>??st4i6.7ui881??setw;5>??st4i6.7ui882??setw;5>??st4i6.assi &ent ??setw;5>??st4i6.&idter&??setw;5>??st4i6. !inal??setw;5> ??st4i6.total3 cout??@An@39 i=iG139

Step 6: Defining the delete struct student st)*, int+ itemcount! method to delete a target record from the array of student ob,ects. The user will be prompted to enter the id of student record that his/her want to delete. Then this id will be checked to make sure it does e#ist in the list. .f the target record or element really e#ists, the deletion process can be made. The deletion process starts by checking whether the target record is the last record, beginning or middle record. .f the target record is the last record in the list, we simply delete the record by supplying it to the clean struct student st)*, int inde#! method. The last record is the record that has it inde# e$ual to itemcount subtracted by &. .f the target record stays at the beginning or in the middle of the list, we need to use a loop to allow the previous element to take over the ne#t element. This process

continue until it reaches the end of the list itemcount-&!. Then the clean ! method is called to clean the last element of the list that should not e#it. After the element is cleaned, the itemcount variable decreases by &. This means that the number of elements in the list decreases.
void delete:rec;struct student st46< int= ite&count>2 strin id3 int inde'3 i! ;ite&count C 5> 2 cout??@Enter studentBs -D1@3 cinCCid3 inde' = searc%;st< id<ite&count>3 i! ;inde'D=E1> 2 i! ;inde' == ;ite&countE1>> HHdelete t%e last record 2 clean;st< inde'>3 EEite&count3 cout??@F%e record was deleted.An@3

else HHdelete t%e !irst or &iddle record 2 !or ;int i = inde'3 i ? ite&countE13 iGG> 2 st4i6 = st4i G 163 clean;st< ite&count>3 EEite&count 3 9 9 9 else cout??@F%e record doesnBt e'ist. C%ecL t%e -D and tr, a ain.An@3 9 else cout??@No record to deleteAn@3 9 void clean;struct student st46<int inde'> 2 st4inde'6.stnu&+er =@@3 strcp,;st4inde'6.stna&e<@@>3 st4inde'6.se' =NUKK3 st4inde'6.7ui881 = 53 st4inde'6.7ui882 = 53 st4inde'6.assi &ent = 53 st4inde'6.&idter& = 53 st4inde'6.!inal = 53 st4inde'6.total = 53 9

Step 7: Defining the update struct student st)*, int itemcount! method to update a specified record. The update process starts by asking the user to input the id of the record to be changed.

The id value is check to make sure it really e#ists. .f it e#its the change to the target record can be made after asking the user to input the new value of the field that need change.
void update:rec;struct student st46<int ite&count>2 strin id3 int colu&n:inde'3 cout??@Enter studentBs -D1@3 cinCCid3 cout??@M%ic% !ield ,ou want to update;1E(>N1@3 cinCCcolu&n:inde'3 int inde' = searc%;st< id<ite&count>3 i! ;inde' D= E1> 2 i! ;colu&n:inde' == 1> 2 cout??@Enter studentBs Na&e1@3 cinCCst4inde'6.stna&e3 9 else i! ;colu&n:inde' == 2> 2 cout??@Enter studentBs $e';* or M>1@3 cinCCst4inde'6.se'3 9 else i! ;colu&n:inde' == 3> 2 cout??@Enter studentBs 7ui881 score1@3 cinCCst4inde'6.7ui8813 9 else i! ;colu&n:inde' == 4> 2 cout??@Enter studentBs 7ui882 score1@3 cinCCst4inde'6.7ui8823 9 else i! ;colu&n:inde' == 5> 2 cout??@Enter studentBs assi &ent score1@3 cinCCst4inde'6.assi &ent3 9 else i! ;colu&n:inde' == #> 2 cout??@Enter studentBs &id ter& score1@3 cinCCst4inde'6.&idter&3 9 else i! ;colu&n:inde' == (> 2 cout??@Enter studentBs !inal score1@3 cinCCst4inde'6.!inal3 9 else cout??@-nvalid colu&n inde'@3 st4inde'6.total = st4inde'6.7ui881 G st4inde'6.7ui882 G st4inde'6.assi &ent G st4inde'6.&idter& G st4inde'6.!inal3 9 else cout??@F%e record deosnBt e'its.C%ecL t%e -D and tr, a ain.@3 9

Step 8: Defining the average student)* st, int itemcount! method to calculate the average score of a selected student. The method alo starts by asking the user to input the id of the target student. This id is checked to make sure it really e#ist. The average score can be calculated by dividing the sum of $ui%%& score, $ui%%' score, assignment score, mid-term score, and final score by 0.
void avera e;struct student st46< int ite&count> 2 strin id3 !loat av =53 cout??@Enter studentsB-D1@3 cinCCid3 int inde' = searc%;st< id<ite&count>3 i! ;inde' D= E1 == ite&countC5> 2 st4inde'6.total = st4inde'6.7ui881 G st4inde'6.7ui882 G st4inde'6.assi &ent G st4inde'6.&idter& G st4inde'6.!inal3 av = st4inde'6.total H53 9 cout??@F%e avera e score is @??av 3 9

Step 9: Defining the showma# struct student st)*, int itemcount! and showmin struct student st)*, int itemcount! functions show about the student who gets the ma#imum score and the student who gets the minimum score. To find the highest total core or lowest total core, we need to compare every total score of each element.
void s%ow&a';struct student st46< int ite&count> 2 !loat &a' = st456.total3 int inde'=53 i! ;ite&count C= 2> 2 !or ;int O = 53 O ? ite&countE13 GGO> i! ;&a' ? st4OG16.total> 2 &a' = st4OG16.total3 inde' = OG13 9 9 else i! ;ite&count == 1> 2 inde' = 53 &a' = st456.total3 9 else cout??@Not record !oundDAn@3 i! ;inde' D= E1> cout??@F%e student wit% -D @??st4inde'6.stnu&+er??@ score @??&a'??endl3 9 void s%ow&in;struct student st46< int ite&count> 2!loat &in = st456.total3 int inde' = 53 i! ;ite&count C= 2> 2 !or ;int O = 53 O ? ite&countE13 GGO> ets t%e %i %est

i! ;&in C st4OG16.total> 2 &in = st4OG16.total3 inde' = OG13 9 9 else i! ;ite&count == 1> 2 inde' = 53 &in = st456.total3 9 else cout??@No record !oundDAn@3 i! ;inde' D= E1> cout??@F%e student wit% -D @??st4inde'6.stnu&+er??@ %i %est score @??&in??endl3 9 ets t%e

Step 10: Defining the find struct student st)*, int itemcount! function to find the record in the list. This function asks the user to enter the id of the student record. Then this id is checked to make sure it really e#ists. .f the record is found, the information of the target student will be displayed. .f the record is not found the message 1The record doesn"t e#ist.1 will be displayed.
void !ind;struct student st46< int ite&count> 2 strin id3 cout??@Enter studentBs -D1@3 cinCCid3 int inde'=searc%;st<id<ite&count>3 i! ;inde' D= E1> 2 HHdispla, t%e !ound record cout??le!t??setw;5>??st4inde'6.stnu&+er??setw;25>??st 4inde'6.stna&e??setw;5>??st4inde'6.se'3 cout??setw;5>??st4inde'6.7ui881??setw;5>??st4inde'6.7ui882??setw;5>??st4inde' 6.assi &ent ??setw;5>??st4inde'6.&idter&??setw;5>??st4inde'6.!inal??setw;5> ??st4inde'6.total3 cout??@An@3 9
else cout??@F%e record doesnBt e'its.@3 9

Step 11: Defining the sort struct student st)*, int itemcount! function to sort the records in ascending order by total scores. 2or the sort techni$ue we use bubble sort algorithm.
void +u++lesort;struct student dataset46< int n> 2 int i< O3 !or ;i = 53 i ? n3 iGG> !or ;O = n E 13 O C i3 OEE> i! ;dataset4O6.total ? dataset4O E 16.total > 2 student te&p = dataset4O63 dataset4O6 = dataset4O E 163 dataset4O E 16 = te&p3 9 9

You might also like