#include<stdio.h>
#include<conio.h>
#define max 20
struct data
{
int rno;
int marks;
}dt[max];
struct data input_data();
void disp_data(struct data[],int);
void disp_sdata(struct data);
int disp_sbm(int);
void add_ele(struct data[],int&,int);
void del_ele(struct data[],int&,int);
void Ssort(struct data[],int);
void Bsort(struct data[],int);
void Lsearch(struct data[],int,int);
void Bsearch(struct data[],int,int);
void trav(struct data[],int,int);
void main()
{
clrscr();
int i,num,cho,sbch,item,ssbch;
do
{
printf("Enter the no. of students :- ");
scanf("%d",&num);
}while( num <= 0 || num >= max );
for(i=0;i<num;i++)
{
printf("\n\n Enter the data of the student #%d: \n",i+1);
dt[i] = input_data();
}
clrscr();
disp_data(dt,num);
getch();
do
{
clrscr();
printf("\n\n\t\t\t Menu Selection\n");
printf("\n 1. Insertion of an Element ");
printf("\n 2. Deletion of an Element ");
printf("\n 3. Sorting of an Array ");
printf("\n 4. Searching of an Element ");
printf("\n 5. Traversing of all Elements ");
printf("\n\t\t Hit Other Keys To Exit .....");
printf("\n\n Enter your choice :- ");
scanf("%d",&cho);
switch(cho)
{
case 1: sbch = disp_sbm(1);
switch(sbch)
{
case 1:
case 2:
case 3: add_ele(dt,num,sbch);
break;
default : break;
}
break;
case 2: sbch = disp_sbm(2);
switch(sbch)
{
case 1:
case 2:
case 3: del_ele(dt,num,sbch);
break;
default : break;
}
break;
case 3: sbch = disp_sbm(3);
switch(sbch)
{
case 1: Ssort(dt,num);
break;
case 2: Bsort(dt,num);
break;
default : break;
}
break;
case 4: sbch = disp_sbm(4);
printf("Enter the roll no. to be searched : ");
scanf("%d",&item);
switch(sbch)
{
case 1: Lsearch(dt,num,item);
break;
case 2: Bsearch(dt,num,item);
break;
default: break;
}
break;
case 5: sbch = disp_sbm(5);
switch(sbch)
{
case 1:
case 2: trav(dt,num,sbch);
break;
default: break;
}
break;
}
if(cho>=1 && cho<=5)
disp_data(dt,num);
}while(cho>=1 && cho<=5);
printf("\n\n\t\t !!!! Thanks Fro Using The Program !!!!");
getch();
}
struct data input_data()
{
struct data s;
printf("\n\t Enter the Roll no. of the student :- ");
scanf("%d",&s.rno);
printf("\n\t Enter the marks of the student :- ");
scanf("%d",&s.marks);
return s;
}
void disp_data(struct data s[],int noe)
{
int i;
clrscr();
printf("\n\n\t The current status of the current data \n");
for(i=0;i<noe;i++)
{
printf("\n\n Data of the Student #%d: \n",i+1);
printf("\n\t Roll no.:- %d",s[i].rno);
printf("\n\t Marks :- %d",s[i].marks);
}
getch();
}
void disp_sdata(struct data s)
{
printf("\n\n Data of the Student \n");
printf("\n\t Roll no.:- %d",s.rno);
printf("\n\t Marks :- %d",s.marks);
}
int disp_sbm(int choice)
{
int sb_choice;
if(choice == 1 || choice ==2)
{
if(choice == 1)
printf(" \n\n\t Select the position to insert an element ");
else
printf(" \n\n\t Select the position to delete an element ");
printf("\n 1. At the beginning ");
printf("\n 2. At the end ");
printf("\n 3. At a desired position ");
}
else if(choice==3)
{
printf(" \n\n\t Select the option of sorting the array ");
printf("\n 1. Selection Sort ");
printf("\n 2. Bubble Sort ");
}
else if(choice==4)
{
printf(" \n\n\t Select the option of searching an element ");
printf("\n 1. Linear Search ");
printf("\n 2. Binary Search ");
}
else
{
printf(" \n\n\t Select the option of traversing of all element ");
printf("\n 1. Addition of every Element ");
printf("\n 2. Subtraction of every Element ");
}
printf("\n\n Choose your option : ");
scanf("%d",&sb_choice);
return sb_choice;
}
void add_ele(struct data x[],int &noe,int choice)
{
int i,loc=0;
struct data temp;
if(noe == max)
printf("\n Warning : OVERFLOW !!! ");
else
{
if(choice == 3)
{
do
{
printf("\n Enter the location you want insert the element");
scanf("%d",&loc);
}while( loc>noe || loc<=0 );
loc -= 1;
}
printf("Enter the details of the element");
temp = input_data();
if(choice == 1 || choice == 3)
{
for(i=noe;i>loc;i--)
x[i] = x[i-1];
x[loc] = temp;
}
else
{
x[noe] = temp;
}
noe += 1;
printf("\n\n\t\t The element has been inserted ");
getch();
}
}
void del_ele(struct data x[],int &noe,int choice)
{
int i,loc=0;
if(noe == 0)
printf("\n Warning : UNDERFLOW !!! ");
else
{
if(choice == 3)
{
do
{
printf("\n Enter the location you want delete the element");
scanf("%d",&loc);
}while( loc>noe || loc<=0 );
loc -= 1;
}
if(choice == 1 || choice == 3)
{
for(i=loc;i<noe-1;i++)
x[i] = x[i+1];
}
noe -= 1;
printf("\n\n\t\t The element has been deleted ");
getch();
}
}
void Ssort(struct data x[],int noe)
{
int i,j,flag,small,posn;
struct data temp;
for(i=0;i<noe;i++)
{
flag = 0;
small = x[i].rno;
for(j=i+1;j<noe;j++)
{
if( x[j].rno < small )
{ small = x[j].rno; flag = 1; posn = j; }
}
if(flag==1)
{
temp = x[i];
x[i] = x[posn];
x[posn] = temp;
}
}
printf("\n\n\t\t Sorting has been done ... \n\n");
}
void Bsort(struct data x[],int noe)
{
int i,j;
struct data temp;
for(i=0;i<noe;i++)
{
for(j=0;j<noe-1-i;j++)
{
if( x[j].rno > x[j+1].rno )
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
printf("\n\n\t\t Sorting has been done ... \n\n");
}
void Lsearch(struct data x[],int noe,int ele)
{
int flag=0,i,noc=0,posn;
for(i=0;i<noe;i++)
{
noc++;
if(ele == x[i].rno)
{ flag = 1; posn = i; break; }
}
if(flag==0)
printf("\n\nThe data not found ");
else
{
printf("\n\nThe data has been found at position #%d",posn+1);
printf("\n& The no. of comparisions were made : %d",noc);
disp_sdata(x[posn]);
}
getch();
}
void Bsearch(struct data x[],int noe,int ele)
{
int flag=0,i,noc=0,posn,beg,last,mid;
beg = 0; last = noe - 1;
Ssort(x,noe);
while(beg<=last)
{
mid = (beg+last)/2;
noc++;
if(ele == x[mid].rno)
{
flag = 1;
posn = mid;
break;
}
else if(ele > x[mid].rno)
beg = mid + 1;
else
last = mid - 1;
}
if(flag==0)
printf("\n\nThe data not found ");
else
{
printf("\n\nThe data has been found at position #%d",posn+1);
printf("\n& The no. of comparisions were made : %d",noc);
disp_sdata(x[posn]);
}
getch();
}
void trav(struct data x[],int noe,int choice)
{
int i,ele;
if(choice == 1)
printf("\n Enter any number to be added to the data : ");
else
printf("\n Enter any number to be subtracted from the data : ");
scanf("%d",&ele);
for(i=0;i<noe;i++)
{
if(choice == 1)
x[i].marks += ele;
else
x[i].marks -= ele;
}
printf("\n\n\t Traversing of all the elements of the data has been done \n\n");
getch();
}
No comments:
Post a Comment