Here I am uploading some of the C programming codes, i have faced in my college days. If anyone is helped with my effort, i will consider myself very lucky........
Prob 1:) Write a C programme to evaluate the power set of a given set
include stdio.h
include stdlib.h
include string.h
int cnt=1;
char exp[10][10];
int n;
//char exp[11][10];
void dec_bin(int x);
int main()
{
//char exp[100];
int i,row=1,j,k,x;
printf("\n\t");
printf("\n...Enter The Number of elements of the Set ->.....->>>>>");
//gets(exp);
scanf("%d",&n);
//len=strlen(exp);
for(x=0;x lt n;x++)
{
scanf("%s",&exp[x]);
}
for(i=0;i lt x;i++)
{
row=row*2;
}
//printf("Line No is=%d",row);
printf("\n");
/*------------Starting The Power Set Evaluation--------------------------*/
int cnt=1;
char exp[10][10];
int n;
//char exp[11][10];
void dec_bin(int x);
int main()
{
//char exp[100];
int i,row=1,j,k,x;
printf("\n\t");
printf("\n...Enter The Number of elements of the Set ->.....->>>>>");
//gets(exp);
scanf("%d",&n);
//len=strlen(exp);
for(x=0;x
scanf("%s",&exp[x]);
}
for(i=0;i
row=row*2;
}
//printf("Line No is=%d",row);
printf("\n");
/*------------Starting The Power Set Evaluation--------------------------*/
printf("\n.....The Power Set is......\n");
for(j=0;j
dec_bin(j);
}
printf("\n");
return 0;
}
void dec_bin(int x)
{
int count,temp,c,p=0;
char num[1000];
for(count=0;count
temp=(x%2);
num[count]=temp;
x=(x/2);
}
//printf("\NNNNNNNN====%d",n);
printf("<%d>==[",cnt++);
for(c=0;c
if(num[c]==1)
{
if(c==n)
{
printf(" %s ",exp[c]);
p==1;
}
else
{
printf(" %s ",exp[c]);
p==1;
}
}
}
printf("]\n");
}
include stdio.h
include stdlib.h
void insertion();
void deletion();
void display();
struct dll
{
int data;
struct dll *left;
struct dll *right;
};
struct dll *front=NULL;
int main()
{
int option;
while(!0)
{
printf("\n----------Enlist The Options--------------\n");
printf("\n\tPress 1 for insert an item");
printf("\n\tPress 2 for delete from Priority queue");
printf("\n\tPress 3 for List the output");
printf("\n\tPress 4 for exit");
printf("\n\tPress the key as per choice=");
scanf("%d",&option);
switch(option)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("\nRe Enter Once More Please\n");
break;
}
}
getch();
return 0;
}
void insertion()
{
struct dll *p;
int item;
p=(struct dll *)malloc(sizeof(struct dll));
if(p==NULL)
{
printf("\n Not Enough Space To Create Node");
exit(1);
}
printf("\n\tEnter The item to be inserted=");
scanf("%d",&item);
p->data=item;
if(front==NULL)
{
p->right=NULL;
front=p;
}
else
{
struct dll *q;
q=front;
while(q->right!=NULL)
{
q=q->right;
}
q->right=p;
p->right=NULL;
p->left=q;
}
}
void deletion()
{
struct dll *p;
int min;
p=(struct dll *)malloc(sizeof(struct dll));
if(p==NULL)
{
printf("\n Not Enough Space To Create Node");
exit(1);
}
if(front==NULL)
{
printf("\n---------------Nothing To Delete----------------\n\n");
}
else
{
struct dll *temp,*prv,*next;
p=front;
min=p->data;
while(p->right!=NULL)
{
p=p->right;
if(p->data<=min)
{
min=p->data;
}
}
printf("\n~~~~~~~~~~~~The Deleted Item is %d~~~~~~~~~~~~~~~~~~~~",min);
temp=front;
while(temp!=NULL)
{
temp=temp->right;
if(temp!=NULL)
{
if(min==temp->data)
{
if(temp->right==NULL)
{
prv=temp->left;
prv->right=NULL;
free(temp);
}
else
{
prv=temp->left;
next=temp->right;
next->left=prv;
prv->right=next;
free(temp);
}
break;
}
}
else if(min==front->data)
{
front=front->right;
break;
}
}
}
}
void display()
{
struct dll *trav,*shift;
int swap;
trav=front;
if(trav==NULL)
{
printf("\n\tTHE DOUBLE LINK LIST IS");
printf("\n--------------------------------------------");
printf("\n\n");
printf("\n---------------------------------------------");
}
else
{
printf("\n\tTHE DOUBLE LINK LIST IS");
printf("\n--------------------------------------------");
printf("\n");
while(trav!=NULL)
{
shift=trav->right;
while(shift!=NULL)
{
if(trav->data>=shift->data)
{
swap=trav->data;
trav->data=shift->data;
shift->data=swap;
}
shift=shift->right;
}
printf("\t%d",trav->data);
trav=trav->right;
}
printf("\n");
printf("\n---------------------------------------------");
}
}
Prob 3:) Programme to find the kth Smallest element from a list using median
#include stdio.h
#include conio.h
#include stdlib.h
#include string.h
#include time.h
void recurse(int ub, int lb);
void insertion(int arr[], int x);
int final_median();
void partition(int x[], int c, int d, int f);
int find_index(int m[], int b, int j);
int *arr,*splt;
int kth_element,k;
int *median,med;
int count=0;
int main()
{
srand(time(0));
int i,no;
printf("\n\t...Enter No Of Elements....=");
scanf("%d",&no);
arr=(int *)calloc(no,sizeof(int));
for(i=0;i lt no;i++)
{
arr[i]=rand()%no;
}
printf("\n\n\tTHE RANDOM INPUT IS........\n\n");
for(i=0;i lt no;i++)
{
printf(" %d ",arr[i]);
}
printf("\n\n\t...ENTER THE kth Smallest Element(k scanf("%d",&kth_element);
med=0;
recurse(no-1,0);
if(count==0)
{
printf("\n...DESIRED ELEMENT NOT FOUND.....\n");
}
getch();
return 0;
}
void recurse(int ub, int lb)
{
int i,j,init,s,par,total,m,up_bnd,dn_bnd;
up_bnd=ub;
dn_bnd=lb;
total=(up_bnd-dn_bnd+1);
init=lb;
if(ub gt lb)
{
for(i=total;i gt =1;i--)
{
if(total%i==0)
{
par=i;
break;
}
}
median=(int *)malloc(par*sizeof(int));
splt=(int *)malloc((total/par)*sizeof(int));
k=0;
for(i=0;i {
for(j=init;j lt (init+(total/par));j++)
{
splt[k++]=arr[j];
}
init=j;
/*---------Showing The splliting arrays---------*/
/*printf("\n\n.........THE %d th SPLITTED ARRAY IS........",i+1);
for(s=0;s lt k;s++)
{
printf(" %d ",splt[s]);
}*/
if(k>0)
insertion(splt,k);
k=0;
}
/*printf("\n\n....THE LIST OF MEDIANS ARE.......\n\n");
for(i=0;i lt med;i++)
{
printf("\t%d",median[i]);
}*/
printf("\n\n");
if(med gt 0)
{
m=final_median();
med=0;
//printf("\n\n\t....THE ULTIMATE MEDIAN IS........=%d",m);
partition(arr,m,dn_bnd,up_bnd);
}
/*else
{
exit(1);
}*/
}
}
void insertion(int arr[], int x)
{
int i,j,temp;
for(i=0;i< le x;i++)
{
temp=arr[i];
for(j=i;j gt 0;j--)
{
if(arr[j-1] gt temp)
{
arr[j]=arr[j-1];
arr[j-1]=temp;
}
else
{
break;
}
}
}
/*printf("\n\tTHE sorted array is\n\n");
for(i=0;i {
printf(" %d ",arr[i]);
}*/
/*---------------FINDING THE MEDIAN FROM SORTED ARRAY----------------*/
if(x%2==0)
{
temp=(x/2)+1;
}
else if(x%2!=0)
{
temp=(x+1)/2;
}
median[med++]=arr[temp-1];
}
int final_median()
{
int i,j,key,index;
int fn_med;
for(i=0;i lt med;i++)
{
key=median[i];
for(j=i;j gt 0;j--)
{
if(median[j-1] gt key)
{
median[j]=median[j-1];
median[j-1]=key;
}
else
{
break;
}
}
}
/*printf("\n\n....THE SORTED FINAL MEDIAN ARRAY IS......\n\n");
for(int f=0;f {
printf(" %d ",median[f]);
}*/
if(med%2!=0)
{
index=(med+1)/2;
}
else
{
index=(med/2)+1;
}
//printf("\n...INDEX=%d",index);
fn_med=median[index-1];
return(fn_med);
}
void partition(int x[], int m, int lb, int ub)
{
int up,down,pivot,swap,ind,high,low,calc;
int total;
pivot=m;
up=ub;
down=lb;
if(ub gt lb)
{
while(up gt down)
{
while(x[down] le pivot && down lt up)
{
down++;
}
while(x[up] gt pivot)
{
up--;
}
if(up gt down)
{
swap=x[down];
x[down]=x[up];
x[up]=swap;
}
}
calc=ub-lb+1;
total=calc;
if(total>0)
{
ind=find_index(x,calc,pivot);
swap=x[down-1];
x[down-1]=pivot;
x[ind]=swap;
}
calc=down-1;
printf("\n....The Index of Pivot is...=%d",calc+1);
//printf("\nCOUNT IS=%d----------------%d-------------%d",count,ub,lb);
//printf("\n\n....THE PARTITION IS......\n\n");
/*for(int i=0;i lt total;i++)
{
printf(" %d ",x[i]);
}*/
if(kth_element==(calc))
{
printf("\n\n------------------------------");
printf("\n\t\t..THE %d th SMALLEST ELEMENTS IS %d",kth_element,x[calc]);
printf("\n--------------------------------\n\n");
count++;
}
else if(kth_element lt (calc))
{
//kth_element=(calc-kth_element);
high=calc-1;
low=lb;
//if(low recurse(high,low);
/*else
{
printf("\n\n...NOT FOUND.....");
exit(1);
}*/
}
else
{
//kth_element=(calc+kth_element);
high=ub;
low=calc+1;
//if(low lt high)
recurse(high,low);
/*else
{
printf("\n\n....NOT FOUND....");
exit(1);
}*/
}
}
/*else
{
printf("\n\n...NOT FOUND.....\n\n");
}*/
}
int find_index(int m[], int bound, int y)
{
int i,ret;
for(i=bound-1;i gt 0;i--)
{
if(m[i]==y)
{
return i;
}
}
}
Prog 4:) Programme to sort data using Radix Sorting method
#include conio.h
#include stdlib.h
#include string.h
#include time.h
void recurse(int ub, int lb);
void insertion(int arr[], int x);
int final_median();
void partition(int x[], int c, int d, int f);
int find_index(int m[], int b, int j);
int *arr,*splt;
int kth_element,k;
int *median,med;
int count=0;
int main()
{
srand(time(0));
int i,no;
printf("\n\t...Enter No Of Elements....=");
scanf("%d",&no);
arr=(int *)calloc(no,sizeof(int));
for(i=0;i lt no;i++)
{
arr[i]=rand()%no;
}
printf("\n\n\tTHE RANDOM INPUT IS........\n\n");
for(i=0;i lt no;i++)
{
printf(" %d ",arr[i]);
}
printf("\n\n\t...ENTER THE kth Smallest Element(k
med=0;
recurse(no-1,0);
if(count==0)
{
printf("\n...DESIRED ELEMENT NOT FOUND.....\n");
}
getch();
return 0;
}
void recurse(int ub, int lb)
{
int i,j,init,s,par,total,m,up_bnd,dn_bnd;
up_bnd=ub;
dn_bnd=lb;
total=(up_bnd-dn_bnd+1);
init=lb;
if(ub gt lb)
{
for(i=total;i gt =1;i--)
{
if(total%i==0)
{
par=i;
break;
}
}
median=(int *)malloc(par*sizeof(int));
splt=(int *)malloc((total/par)*sizeof(int));
k=0;
for(i=0;i
for(j=init;j lt (init+(total/par));j++)
{
splt[k++]=arr[j];
}
init=j;
/*---------Showing The splliting arrays---------*/
/*printf("\n\n.........THE %d th SPLITTED ARRAY IS........",i+1);
for(s=0;s lt k;s++)
{
printf(" %d ",splt[s]);
}*/
if(k>0)
insertion(splt,k);
k=0;
}
/*printf("\n\n....THE LIST OF MEDIANS ARE.......\n\n");
for(i=0;i lt med;i++)
{
printf("\t%d",median[i]);
}*/
printf("\n\n");
if(med gt 0)
{
m=final_median();
med=0;
//printf("\n\n\t....THE ULTIMATE MEDIAN IS........=%d",m);
partition(arr,m,dn_bnd,up_bnd);
}
/*else
{
exit(1);
}*/
}
}
void insertion(int arr[], int x)
{
int i,j,temp;
for(i=0;i< le x;i++)
{
temp=arr[i];
for(j=i;j gt 0;j--)
{
if(arr[j-1] gt temp)
{
arr[j]=arr[j-1];
arr[j-1]=temp;
}
else
{
break;
}
}
}
/*printf("\n\tTHE sorted array is\n\n");
for(i=0;i
printf(" %d ",arr[i]);
}*/
/*---------------FINDING THE MEDIAN FROM SORTED ARRAY----------------*/
if(x%2==0)
{
temp=(x/2)+1;
}
else if(x%2!=0)
{
temp=(x+1)/2;
}
median[med++]=arr[temp-1];
}
int final_median()
{
int i,j,key,index;
int fn_med;
for(i=0;i lt med;i++)
{
key=median[i];
for(j=i;j gt 0;j--)
{
if(median[j-1] gt key)
{
median[j]=median[j-1];
median[j-1]=key;
}
else
{
break;
}
}
}
/*printf("\n\n....THE SORTED FINAL MEDIAN ARRAY IS......\n\n");
for(int f=0;f
printf(" %d ",median[f]);
}*/
if(med%2!=0)
{
index=(med+1)/2;
}
else
{
index=(med/2)+1;
}
//printf("\n...INDEX=%d",index);
fn_med=median[index-1];
return(fn_med);
}
void partition(int x[], int m, int lb, int ub)
{
int up,down,pivot,swap,ind,high,low,calc;
int total;
pivot=m;
up=ub;
down=lb;
if(ub gt lb)
{
while(up gt down)
{
while(x[down] le pivot && down lt up)
{
down++;
}
while(x[up] gt pivot)
{
up--;
}
if(up gt down)
{
swap=x[down];
x[down]=x[up];
x[up]=swap;
}
}
calc=ub-lb+1;
total=calc;
if(total>0)
{
ind=find_index(x,calc,pivot);
swap=x[down-1];
x[down-1]=pivot;
x[ind]=swap;
}
calc=down-1;
printf("\n....The Index of Pivot is...=%d",calc+1);
//printf("\nCOUNT IS=%d----------------%d-------------%d",count,ub,lb);
//printf("\n\n....THE PARTITION IS......\n\n");
/*for(int i=0;i lt total;i++)
{
printf(" %d ",x[i]);
}*/
if(kth_element==(calc))
{
printf("\n\n------------------------------");
printf("\n\t\t..THE %d th SMALLEST ELEMENTS IS %d",kth_element,x[calc]);
printf("\n--------------------------------\n\n");
count++;
}
else if(kth_element lt (calc))
{
//kth_element=(calc-kth_element);
high=calc-1;
low=lb;
//if(low
/*else
{
printf("\n\n...NOT FOUND.....");
exit(1);
}*/
}
else
{
//kth_element=(calc+kth_element);
high=ub;
low=calc+1;
//if(low lt high)
recurse(high,low);
/*else
{
printf("\n\n....NOT FOUND....");
exit(1);
}*/
}
}
/*else
{
printf("\n\n...NOT FOUND.....\n\n");
}*/
}
int find_index(int m[], int bound, int y)
{
int i,ret;
for(i=bound-1;i gt 0;i--)
{
if(m[i]==y)
{
return i;
}
}
}
Prog 4:) Programme to sort data using Radix Sorting method
#include
#include
#include
#include
Prog 6:)
#include
#include
#include
Prog 6:)
No comments:
Post a Comment