Practicalsolution
Practicalsolution
Slip 1_1 : Sort a random array of n integers (accept the value of n from user) in ascending order
#include<stdio.h>
int i,j,temp;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
int i;
for(i=0;i<n;i++)
a[i]=rand()%1000;
{ int i;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
void main()
int a[20],i,j,n;
scanf("%d",&n);
generate(a,n);
display(a,n);
bubble(a,n);
display(a,n);
********************************************************
#include <stdio.h>
#define MAXSIZE 10
void main()
int array[MAXSIZE];
scanf("%d", &num);
scanf("%d", &array[i]);
printf("%d\n", array[i]);
temp = array[j];
array[j + 1] = temp;
printf("%d\n", array[i]);
********************************************************
2. Slip 2_1 : Sort a random array of n integers (accept the value of n from user) in ascending order by
using insertion sort algorithm.*
#include<stdio.h>
int i,j,key;
for(i=1;i<n;i++)
{
key=a[i];
for(j=i-1;j>=0 ;j--)
if(key<a[j])
a[j+1]=a[j]; //shifting
else break;
a[j+1]=key;
int i;
for(i=0;i<n;i++)
a[i]=rand()%2000;
{ int i;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
main()
int a[20],i,j,n;
scanf("%d",&n);
generate(a,n);
display(a,n);
insertion(a,n);
display(a,n);
********************************************************
3. Slip3_1 : Read the data from the file “employee.txt” and sort on age using insertion sort. Reading &
Display the file Insertion sort Function
#include<stdio.h>
int age;
char name[10];
}record;
record employee[100];
int readfile(record *a)
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
while(!feof(fp))
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
return (i-1);
int i=0;
FILE *fp;
if((fp=fopen("sorted_on_age_emp.txt","w"))!=NULL)
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
}}
{
int i,j;
record t;
for(i=1;i<n;i++)
t=a[i];
a[j+1]=a[j];
a[j]=t;
main()
int n;
n=readfile(employee);
insertion(employee,n);
writefile(employee,n);
Email This
BlogThis!
Share to Twitter
Share to Facebook
Share to Pinterest
**************************************
4. Slip 4_1: Read the data from the file “employee.txt” and sort on age using Bubble sort.
#include<stdio.h>
char name[10];
int age;
}record;
record employee[100];
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{while(!feof(fp))
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}
return(i-1);
int i=0;
FILE *fp;
if((fp=fopen("sorted_emp_on_age.txt","w"))!=NULL)
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j].age>a[j+1].age)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
main()
int n;
n=readfile(employee);
bubble_sort(employee,n);
writefile(employee,n);
Email This
BlogThis!
Share to Twitter
Share to Facebook
Share to Pinterest
**************************************
5. Slip 5_1 :Read the data from the file “employee.txt” and sort on names in alphabetical order (use
strcmp)
using bubble sort
#include<stdio.h>
char name[10];
int age;
}record;
record employee[100];
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{while(!feof(fp))
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}}
return(i-1);
}
void writefile(record *a,int n)
int i=0;
FILE *fp;
if((fp=fopen("sorted_emp_on_name_bubble.txt","w+"))!=NULL)
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(strcmp(a[j].name,a[j+1].name)>=0)
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
main()
int n;
n=readfile(employee);
bubble_sort(employee,n);
writefile(employee,n);
Email This
BlogThis!
Share to Twitter
Share to Facebook
Share to Pinterest
******************************************
6. Slip6_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use
using Insertion sort
#include<stdio.h>
{
int age; char name[10];
}record;
record employee[50];
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
while(!feof(fp))
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}return (i-1);
int i;
FILE *fp;
if((fp=fopen("sorted_on_name_insertion.txt","w"))!=NULL)
{
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
int i,j;
record t;
for(i=1;i<n;i++)
t=a[i];
a[j+1]=a[j];
a[j]=t;
main()
int n;
n=readfile(employee);
insertion(employee,n);
writefile(employee,n);
Email This
BlogThis!
Share to Twitter
Share to Facebook
Share to Pinterest
***********************
7. Slip 7_1 : Sort a random array of n integers (accept the value of n from user) in ascending order by
using a
#include<stdio.h>
int c[10],i,j,k;
i=l;
j=m+1;
k=0;
if(a[i]<a[j])
c[k]=a[i];
k++;i++;
else
c[k]=a[j];
k++;j++;
while(i<=m)
c[k]=a[i];
i++;k++;
while(j<=u)
c[k]=a[j];
k++;j++;
for(i=l,j=0;i<=u;i++,j++)
a[i]=c[j];
}
void generate(int a[10],int n)
int i;
for(i=0;i<n;i++)
a[i]=rand()%10;
int k=0;
if(i<j)
k=(i+j)/2;
merge_sort(a,i,k);
merge_sort(a,k+1,j);
merge(a,i,k,j);
main()
int i,n,a[10];
scanf("%d",&n);
generate(a,n);
printf("elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
merge_sort(a,0,n-1);
printf("\nafter sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
*******************************
8. Slip 8_1 : Sort a random array of n integers (accept the value of n from user) in ascending order by
using
#include<stdio.h>
int i;
for(i=l;i<=u;i++)
printf("%d\t",a[i]);
}
void quick(int a[],int l,int u)
int temp,piv,left,right;
left=l;
right=u;
piv=l;
if(l>=u)
return;
printf("\nsublist:\n");
disp(a,l,u);
while(pivot_places==false)
right--;
if(piv==right)
pivot_places=true;
if(a[piv]>a[right])
temp=a[piv];
a[piv]=a[right];
a[right]=temp;
piv=right;
if(piv==left)
pivot_places=true;
if(a[piv]<a[left])
temp=a[piv];
a[piv]=a[left];
a[left]=temp;
piv=left;
disp(a,l,u);
quick(a,l,piv-1);
quick(a,piv+1,u);
int i;
for(i=0;i<n;i++)
a[i]=rand()%20;
main()
{
int a[10],n,i;
scanf("%d",&n);
generate(a,n);
printf("\nelements are:");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
quick(a,0,n-1);
printf("\nafter sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
*******************************************
9. Slip 9_1 :. Read the data from the „employee.txt‟ file and sort on age using Merge sort and write the
sorted
include<stdio.h>
char name[10];
int age;
}record;
record employee[100];
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{while(!feof(fp))
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}}
return(i-1);
int i=0;
FILE *fp;
if((fp=fopen("sorted_emp_on_age_merge.txt","w"))!=NULL)
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
}}
merge(record *a,int l,int m,int u)
i=l;
j=m+1;
k=0;
if(a[i].age<a[j].age)
c[k]=a[i];
k++;i++;
else
c[k]=a[j];
k++;j++;
while(i<=m)
c[k]=a[i];
i++;k++;
while(j<=u)
{
c[k]=a[j];
k++;j++;
for(i=l,j=0;i<=u;i++,j++)
a[i]=c[j];
int k=0;
if(i<j)
k=(i+j)/2;
merge_sort(a,i,k);
merge_sort(a,k+1,j);
merge(a,i,k,j);
main()
int n;
n=readfile(employee);
merge_sort(employee,0,n-1);
writefile(employee,n);
*************************************
10.Slip10_1 : Read the data from the "employee.txt‟ file and sort on age using Quick sort and write the
sorted
#include<stdio.h>
}record;
record employee[50];
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{
while(!feof(fp))
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}return (i-1);
int i;
FILE *fp;
if((fp=fopen("sorted_on_age_quick.txt","w"))!=NULL)
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
right=u;
piv=l;
if(l>=u)
return;
/*
printf("\nsublist:\n");
disp(a,l,u);*/
while(pivot_places==false)
right--;
if(piv==right)
pivot_places=true;
if(a[piv].age>a[right].age)
temp=a[piv];
a[piv]=a[right];
a[right]=temp;
piv=right;
left++;
if(piv==left)
pivot_places=true;
if(a[piv].age<a[left].age)
temp=a[piv];
a[piv]=a[left];
a[left]=temp;
piv=left;
//disp(a,l,u);
quick(a,l,piv-1);
quick(a,piv+1,u);
main()
int n;
n=readfile(employee);
quick(employee,0,n-1);
writefile(employee,n);
*****************************************
11. Slip11_1 : Create a random array of n integers. Accept a value x from user and use linear search
algorithm to
check whether the number is present in the array or not and output the position if the number is
present.
#include<stdio.h>
int i;
for(i=0;i<n;i++)
if(a[i]==sr)
return i;
return -1;
int i;
for(i=0;i<n;i++)
a[i]=rand()%20;
{ int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
main()
int a[20],i,j,n,x,ans;
scanf("%d",&n);
generate(a,n);
display(a,n);
scanf("%d",&x);
ans=linear_search(a,n,x);
if(ans==-1)
else
***************************
12. Slip12_1 : Read the data from file 'cities.txt' containing names of 10 cities and their STD codes.
Accept a name of the city from user and use linear search algorithm to check whether the name is
present in the file and output the STD code, otherwise output “city not in the list”.
#include<string.h>
char name[20];
int code;
}record;
record city[100];
int i=0;
FILE *fp;
if((fp=fopen("cities.txt","r"))!=NULL)
while(!feof(fp))
fscanf(fp,"%s%d",a[i].name,&a[i].code);
i++;
}}
return (i-1);
int i;
for(i=0;i<n;i++)
if(strcmp(a[i].name,x)==0)
printf("\n%s=%d\n",a[i].name,a[i].code);
break;
if(i==n)
main()
char x[20];
int n;
n=read_file(city);
gets(x);
l_search(city,n,x);
**********************************
13. Slip13_1 : Read the data from file "sortedcities.txt" containing names of 10 cities and their STD
codes. Accept a name of the city from user and use binary search algorithm to check whether the name
is present in the file and output the STD code, otherwise output “city not in the list”.
#include<stdio.h>
#include<string.h>
char name[30];
int code;
}record;
record city[100];
int i=0;
FILE *fp;
if((fp=fopen("sortedcities.txt","r"))!=NULL)
while(!feof(fp))
fscanf(fp,"%s%d",a[i].name,&a[i].code);
i++;
return (i-1);
}
int i=0;
FILE *fp;
if((fp=fopen("sorted_cities.txt","w"))!=NULL)
for(i=0;i<n;i++)
fprintf(fp,"\n%s\t%d",a[i].name,a[i].code);
int i,j;
record t;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(strcmp(a[j].name,a[j+1].name)>0)
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
int i=0;
FILE *fp;
if((fp=fopen("sorted_cities.txt","r"))!=NULL)
while(!feof(fp))
fscanf(fp,"%s%d",a[i].name,&a[i].code);
i++;
return (i-1);
int l,h,mid;
l=0;
h=n-1;
while(h>=l)
mid=(l+h)/2;
if(strcmp(key,a[mid].name)==0)
break;
}
else if(strcmp(key,a[mid].name)<0)
h=mid-1;
else
l=mid+1;
if(h<l)
main()
char key[20];
int n,m;
n=read_file(city);
sort(city,n);
write_file(city,n);
scanf("%s",key);
b_search(city,n,key);
*****************************
14. Slip14_1 : Implement a stack library (stack.h) of integers using a static implementation of the stack
and implementing the Init(),Push(),Pop(),IsEmpty()& IsFull() these operations. Write a Menu driven
program that includes stack library and calls different stack operations Push & Pop Function each carries
Init Function IsEmpty Function & IsFull Function each carries 2 marks.
#include<stdio.h>
#include"stack.h"
main()
int n,i=0,ch;
init();
do
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
push(n);
break;
case 2:
break;
case 3: i=isempty();
if(i==1)
printf("\nstack is empty");
else
printf("\nstack is not empty");
break;
case 4: i=isfull();
if(i==1)
printf("\nstack is full");
else
break;
break;
case 6: display();
break;
case 7:exit(0);
}while(ch!=7);
***************************************************
16. Slip16_1 : Implement a list library (singlylist.h) for a singly linked list with the operations create,
Delete,display. Write a menu driven program to call these operations.
Create & Delete at front, Display Operation Carries 5 each Marks. [15]
#include<stdio.h>
#include<stdlib.h>
#include "singlylist.h"
main()
do
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
h=create(n);
break;
scanf("%d",&d);
h=delete(h,d);
break;
case 3: display(h);
}while(ch!=4);
}
******************************
Slip18_1 : Implement a list library (doublylis.h) for a doubly linked list with the operations create,
Insert,display. Write a menu driven program to call these operations.
#include<stdio.h>
#include<stdlib.h>
#include "doublylist.h"
void main()
int n,ch,sr,d;
do
printf("\nMENU\n");
printf("\n1.Create");
printf("\n2.Insert");
printf("\n3.Display");
printf("\n4.Exit\n");
printf("\nEnter Ur Choice\n");
scanf("%d",&ch);
switch(ch)
case 1:
printf("\nHow many nodes you want to \n");
scanf("%d",&n);
H=create(n);
break;
scanf("%d",&d);
H=insert(H,d);
break;
display(H);
break;
case 4: exit(0);
break;
}while(ch!=4);
***************************
25. Slip25_1 : Implement a list library (singlylist.h) for a singly linked list with the operations create,
Delete,display. Write a menu driven program to call these operations.
Create & Delete at front, Display Operation Carries 5 each Marks. [15]
#include<stdio.h>
#include<stdlib.h>
#include "singlylist.h"
main()
do
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
h=create(n);
break;
scanf("%d",&d);
h=delete(h,d);
break;
case 3: display(h);
break;
case 4: exit(0);
}while(ch!=4);
************************