C exercises and solutions: Student Records complete C code


C structure: Student Records complete C code

Step 12: Now combine the C code together and test your program.



#include <stdio.h>
#include <stdlib.h>

//declaring student structure
typedef struct
{

char stnumber[10];
char stname[20];
char sex;
float quizz1;
float quizz2;
float assigment;
float midterm;
float final;
float total;

}student;
//function prototypes
int search(student st[],char id[], int itemcount);
void clean(student st[],int deleteitem);
void displayheading();

//function to display a menu of choices
void displaymenu(){
printf("==================================================\n");
printf(" MENU \n");
printf("==================================================\n");
printf(" 1.Add student records\n");
printf(" 2.Delete student records\n");
printf(" 3.Update student records\n");
printf(" 4.View all student records\n");
printf(" 5.Calculate average score of a student\n");
printf(" 6.Show student who gets the max total score\n");
printf(" 7.Show student who gets the max total score\n");
printf(" 8.Find a student by ID\n");
printf(" 9.Sort records by TOTAL\n");
}
//function to append a new record
void add_rec(student st[],int *itemcount){

again:
printf("\nEnter student's ID:");
scanf("%s",&st[*itemcount].stnumber);
if(search(st,st[*itemcount].stnumber,*itemcount)!=-1){
printf("This ID already exists\n");goto again;
}

printf("Enter student's Name:");
scanf("%s",&st[*itemcount].stname);
printf("Enter student's Sex(F or M):");scanf("%s",&st[*itemcount].sex);
printf("Enter student's quizz1 score:");scanf("%f",&st[*itemcount].quizz1);
printf("Enter student's quizz2 score:");scanf("%f",&st[*itemcount].quizz2);
printf("Enter student's assigment score:");scanf("%f",&st[*itemcount].assigment);
printf("Enter student's mid term score:");scanf("%f",&st[*itemcount].midterm);
printf("Enter student's final score:");scanf("%f",&st[*itemcount].final);
st[*itemcount].total=st[*itemcount].quizz1+st[*itemcount].quizz2+
st[*itemcount].assigment+st[*itemcount].midterm+st[*itemcount].final;

++(*itemcount);


}

//function to find the location of the record
int search(student st[], char id[],int itemcount){
int found =-1,i;

for (i = 0; i < itemcount && found==-1; i++)
{

if (strcmp(st[i].stnumber,id)==0) found=i;

else found=-1 ;
}

return found;
}



//function to view all records
void viewall(student st[], int itemcount){
int i=0;
displayheading();
while(i<itemcount){
if(st[i].stnumber!=""){
printf("%-5s",st[i].stnumber);
printf("%-17s",st[i].stname);
printf("%-5c",st[i].sex);
printf("%-6.1f",st[i].quizz1);
printf("%-6.1f",st[i].quizz2);
printf("%-6.1f",st[i].assigment);
printf("%-6.1f",st[i].midterm);
printf("%-7.1f",st[i]. final);
printf("%-4.1f",st[i].total);
printf("\n");
}
i=i+1;

}


}

void displayheading(){
printf("ID NAME SEX Q1 Q2 As Mi Fi TOTAL \n");
printf("==================================================\n");
}

//function to delete record
void delete_rec(student st[], int *itemcount){
char id[10];
int index,i;
if (*itemcount > 0)
{
printf("Enter student's ID:");
scanf("%s",id);
index = search(st, id,*itemcount);

if ((index!=-1) && (*itemcount != 0))
{
if (index == (*itemcount-1)) //delete the last record
{

clean(st, index);
--(*itemcount);

printf("The record was deleted.\n");
}
else //delete the first or middle record
{
for (i = index; i < *itemcount-1; i++)
{
st[i] = st[i + 1];
clean(st, *itemcount);
--(*itemcount) ;
}

}

}
else printf("The record doesn't exist.Check the ID and try again.\n");


}
else printf("No record to delete\n");
}

//function to clean deleted record
void clean(student st[],int index)
{
strcpy(st[index].stnumber,"");
strcpy(st[index].stname,"");
st[index].sex =NULL;
st[index].quizz1 = 0;
st[index].quizz2 = 0;
st[index].assigment = 0;
st[index].midterm = 0;
st[index].final = 0;
st[index].total = 0;

}


//function to update record
void update_rec(student st[],int itemcount){
char id[10];
int column_index;
printf("Enter student's ID:");
scanf("%s",&id);
printf("Which field you want to update(1-7)?:");
scanf("%d",&column_index);

int index = search(st, id,itemcount);

if (index != -1)
{
if (column_index == 1)
{
printf("Enter student's Name:");

scanf("%s",&st[index].stname);
}

else if (column_index == 2)
{
printf("Enter student's Sex(F or M):");
scanf("%c",&st[index].sex);
}
else if (column_index == 3)
{
printf("Enter student's quizz1 score:");
scanf("%f",st[index].quizz1);
}
else if (column_index == 4)
{
printf("Enter student's quizz2 score:");
scanf("%f",st[index].quizz2);
}
else if (column_index == 5)
{
printf("Enter student's assigment score:");
scanf("%f",st[index].assigment);
}
else if (column_index == 6)
{
printf("Enter student's mid term score:");
scanf("%f",&st[index].midterm);
}
else if (column_index == 7)
{
printf("Enter student's final score:");
scanf("%f",&st[index].final);
}
else printf("Invalid column index");

st[index].total = st[index].quizz1 + st[index].quizz2 + st[index].assigment

 + st[index].midterm + st[index].final;


}
else printf("The record deosn't exits.Check the ID and try again.");


}

//function to show max total score
void showmax(student st[], int itemcount)
{
float max = st[0].total;
int index=0,j;
if (itemcount >= 2)
{

for (j = 0; j < itemcount-1; ++j)
if (max < st[j+1].total) {
max = st[j+1].total;
index = j+1;

}


}

else if (itemcount == 1)
{
index = 0;
max = st[0].total;
}


else printf("Not record found!\n");

if (index != -1) printf("The student with ID %s gets the highest score %f",st[index].stnumber,max);


}

//function to show min total score
void showmin(student st[], int itemcount)
{

float min = st[0].total;
int index = 0,j;
if (itemcount >= 2)
{
for (j = 0; j < itemcount-1; ++j)
if (min > st[j+1].total)
{
min = st[j+1].total;
index = j+1;

}

}

else if (itemcount == 1)
{
index = 0;
min = st[0].total;
}
else printf("No record found!\n");

if (index != -1) printf("The student with ID %s gets the highest score %f",st[index].stnumber,min);

}

//function to find record
void find(student st[], int itemcount)
{
char id[10];
printf("Enter student's ID:");
scanf("%s",&id);

int index=search(st,id,itemcount);
if (index != -1)
{ //display the found record
displayheading();
printf("%-5s",st[index].stnumber);
printf("%-17s",st[index].stname);
printf("%-5c",st[index].sex);
printf("%-6.1f",st[index].quizz1);
printf("%-6.1f",st[index].quizz2);
printf("%-6.1f",st[index].assigment);
printf("%-6.1f",st[index].midterm);
printf("%-7.1f",st[index]. final);
printf("%-4.1f",st[index].total);
printf("\n");

}
else printf("The record doesn't exits.\n");

}


//function to sort records by total score
void bubblesort(student dataset[], int n)
{
int i, j;
for (i = 0; i < n; i++)
for (j = n - 1; j > i; j--)
if (dataset[j].total < dataset[j - 1].total )
{
student temp = dataset[j];
dataset[j] = dataset[j - 1];
dataset[j - 1] = temp;
}

}

//function to calculate the average score of a student
void average(student st[], int itemcount)
{
char id[10];
float avg=0;
printf("Enter students'ID:");
scanf("%s",&id);
int index = search(st, id,itemcount);
if (index != -1 && itemcount>0)
{
st[index].total = st[index].quizz1 + st[index].quizz2 + st[index].assigment + st[index].midterm + st[index].final;
avg = st[index].total /5;
}

printf("The average score is %f",avg);
}

//the main function

int main(int argc, char *argv[])
{


student st[20];
int itemcount=0;

//show menu
displaymenu();
int yourchoice;
char confirm;
do
{
printf("Enter your choice(1-9):");
scanf("%d",&yourchoice);

switch(yourchoice){
case 1:add_rec(st, &itemcount);break;
case 2:delete_rec(st, &itemcount);break;
case 3:update_rec(st, itemcount);break;
case 4:viewall(st, itemcount);break;
case 5:average(st, itemcount);break;
case 6:showmax(st, itemcount);break;
case 7:showmin(st, itemcount);break;
case 8:find(st, itemcount);break;
case 9:bubblesort(st, itemcount);break;
default:printf("invalid\n");
}

printf("Press y or Y to continue:");
scanf("%s",&confirm);
}while(confirm=='y'||confirm=='Y');
system("PAUSE");
return EXIT_SUCCESS;
}





Comments

wtf is this comment

 wtf is this

shit program


2017-09-11
Abdullah Ibn Younus comment

 Abdullah Ibn Younus

sir, i have to need
just 6 operations that is
1.Add
2.Delete
3.Update
4.Search
5.Shorting &
6.Display
Help me sir please


2017-08-04
vicheamam comment

 vicheamam

thank alot for this program.It helps me alot :D


2014-06-15
rogerson001 comment

 rogerson001

else //delete the first or middle record
{
for (i = index; i < *itemcount-1; i++)
{
st[i] = st[i + 1];
clean(st, *itemcount);
--(*itemcount) ;
}

why is it that when I delete the first or the middle record there is an error. . .and when i display records again it shows the same records in all data. . .

But somehow this is an awesome! Thanks to this bro. .


2014-02-15
sneaky5439 comment

 sneaky5439

The //delete first and middle record won't work.


2014-02-15



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.