Tuesday 1 November 2011

5. Student Database (Bubble sort and linear search)


  /*Title : Student Database data searching and sorting */



/* header files */
#include<stdio.h>
#include<conio.h>

typedef struct stud
{
int roll_no;
char name[20];
float per;
}stud;

/* MAIN FUNCTION DEFINITION */
int main(void)
{
stud s[20];

/* VARIABLES DECLARATIONS */
int n=0,choice=0,rollno=0;

/* FUNCTION PROTOTYPES */
int accept(int,struct stud s[]);
int display(int,struct stud s[]);
int bsrt(int,struct stud s[]);
int linsearch(int,struct stud s[],int);
clrscr();
printf("\n\t WELCOME TO THE PROGRAM OF STUDENT DATABASE....!! ");
printf("\n\n ENTER NO OF RECORDS:");/* ACCEPT NO OF RECORDS */
scanf("%d",&n);

do
{
/* PRINTING MENU */
printf("\n\n\t\t\t MENU ");
printf("\n\n  1 -> CREATE STUDENT DATABASE ");
printf("\n\n  2 -> DISPLAY STUDENT DATABASE ");
printf("\n\n  3 -> BUBBLE SORT (DESCENDING ORDER OF PERCENTAGE)");
printf("\n\n  4 -> LINEAR SEARCH BY ROLL NO AND THE CASES");
printf("\n\n ENTER CHOICE GREATER THAN 4 TO EXIT");
printf("\n\n ENTER YOUR CHOICE:");
scanf("%d",&choice);

switch(choice)
{
case 1: printf("\n\t\t ACCEPTING STUDENTS DETAILS:");
accept(n,s);/* ACCEPT FUNCTION CALL */
break;

case 2: printf("\n\t\t DISPLAYING STUDENT DETAILS:");
display(n,s);/* DISPLAY FUNCTION CALL */
break;

case 3: printf("\n\t\t SORTED LIST IS:");
bsrt(n,s);/* BINARY SORT FUNCTION CALL */
display(n,s);/* DISPLAY FUNCTION CALL */
break;

case 4: printf("\n\n ENTER ROLL NO TO BE SEARCHED:");
scanf("%d",&rollno);
linsearch(n,s,rollno);/* LINEAR SEARCH FUNCTION CALL */
break;

}

}
while(choice<=4);/* END OF do-while LOOP */
getch();
return 0;
}


/* ACCEPT FUNCTION DEFINITION */
int accept(int n,struct stud s[20])
{
int i=0;
float d=0.0;
for(i=0;i<n;i++)
{
printf("\n\nENTER ROLL NO:");
flushall();
scanf("%d",&s[i].roll_no);
flushall();
printf("\nENTER NAME OF THE STUDENT:");
flushall();
gets(s[i].name);
printf("\nENTER PERCENTAGE:");
flushall();
scanf("%f",&d);
s[i].per=d;

}
return 0;
}


/* DISPLAY FUNCTION DEFINITION */
int display(int n,struct stud s[20])
{
int i=0;
printf("\n\n ROLL NO\t NAME\t\t PERCENTAGE\n");
for(i=0;i<n;i++)
{
printf("\n %d",s[i].roll_no);
printf("\t\t %s",s[i].name);
printf("\t\t %f",s[i].per);
printf("\n");

}
return 0;

}


/* BUBBLE SORT FUNCTION DEFINITION */
int bsrt(int n,struct stud s[20])
{
int i=0,j=0;
struct stud temp;

for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(s[j].per<s[j+1].per)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;

}
}
}
return 0;
}


/* LINEAR SEARCH FUNCTION DEFINITION */
int linsearch(int n,struct stud s[20],int rollno)
{
int i=0,flag=0,cnt=1;
for(i=0;i<n;i++)
{
if(s[i].roll_no==rollno)
{
flag=1;
printf("\n ROLL NO IS FOUND AT POSITION %d",i+1);
printf("\n\t\t DISPLAYING STUDENT DETAILS:");
printf("\n\n ROLL NO\t NAME\t\t PERCENTAGE\n");
printf("\n %d",s[i].roll_no);
printf("\t\t %s",s[i].name);
printf("\t\t %f",s[i].per);
break;
}
cnt++;
}
printf("\n\n NO OF PASSES: %d",cnt);
if(flag==1)
{
if(cnt==1)
{
printf("\n\n\t BEST CASE");
}
else
{
if(cnt==n)
{
printf("\n\n\t WORST CASE");
}
else
{
printf("\n\n AVERAGE CASE");
}
}
}
else
{
printf("\n\n OOPS..!! ROLL NO IS NOT FOUND IN THE RECORDS");
}
return 0;
}


1 comment:

  1. Databases are all about collections of interrelated data. Research work on databases at the undergraduate level for students in their final year may include projects or theses.apdm

    ReplyDelete