0% found this document useful (0 votes)
76 views

C Programs Final

This document contains 13 programming problems related to C and C++ programming. The problems cover topics like matrix operations, string manipulation, searching algorithms, data structures like linked lists and trees, file handling, and sorting techniques. The document provides sample code and output for the first problem of finding the inverse of a matrix.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

C Programs Final

This document contains 13 programming problems related to C and C++ programming. The problems cover topics like matrix operations, string manipulation, searching algorithms, data structures like linked lists and trees, file handling, and sorting techniques. The document provides sample code and output for the first problem of finding the inverse of a matrix.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

EC-309

Programming in C and C++

Submitted By:-

Manoj Kumar
62/EC/06
Contents
1) Write a program for finding the inverse of a matrix A=Adj(A)/|A|
2) Write a program for finding any possible largest string in the text when the string
is taken as an input
3) Write a program for generating n*n matrix which have sum of rows, sum of
Columns and sum of diagonals equal
4) Write a program for finding English equivalent of a given input number(<9999)
5) To write a program for searching a value in the list using linear search and
binary search method and display the time taken in each case
6) Write a program to find the age of a person by taking input the present date and
the birth date keeping track of leap years
7) Write a program to store information related to a manager using array of structure
8) To write a program for storing information of a student using linked list
9) Write a program for generating a single link list in which insertion, deletion and
modification of a node is possible
10) Write a program for traversing a tree(Preorder, Post order, In order)
11) Write a program for traversing a graph
12) Write a program for storing, appending and retrieving information from a file
13) Write a program for sorting list of numbers using quick, selection, merge and
insertion sort techniques
14) Write a program to determine the day of a given date
EXP-1
AIM:- Write a program for finding the inverse of a matrix A=Adj(A)/|A|

#include<stdio.h>
#include<conio.h>
#include<math.h>

//Gives the minor of the row+1,column+1 element


float minor(float p[][10],int order,int row,int column);
//Gives the determinant of matrix p of any order
float determinant(float p[][10],int order);

void main()
{
int order,i,j;
float matrix[10][10],det;
clrscr();
printf("Enter the order of the matrix:");
scanf("%d",&order);
printf("Enter the elements of matrix\n");
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
scanf("%f",&matrix[i][j]);
}
}
printf("\nyour entered matrix is\n");
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
printf("%4.1f ",matrix[i][j]);
}
printf("\n\n");
}
det = determinant(matrix,order);
if(det==0)
{
printf("Matrix is singular\nInverse not possible");
goto end;
}
printf("\nDeterminant is %f \n",det);
printf("\nThe inverse of the matrix is\n");
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
printf("%f ",pow(-1,i+j)*minor(matrix,order,j,i)/det);
}
printf("\n\n");
}
end:
getch();
}

float minor(float p[][10],int order,int row,int column)


{
int q,qa,r,ra;
float sub[10][10];
if(order==1)
return 1;//minor of 1*1 matrix is 1
else if(order==2)
return p[!row][!column];
else
{
for(q=0,qa=0;q<order;q++,qa++)
{
if(q==row) //skip coresponding element
q++;
if(q==order) //beyond last row
continue;
for(r=0,ra=0;r<order;r++,ra++)
{
if(r==column)//skip corresponding element
r++;
if(r==order) //beyond last column
continue;
sub[qa][ra]=p[q][r];
}
}
return determinant(sub,order-1);
}
}

float determinant(float p[][10],int order)


{
float z=0;
int t;
if(order==1)
return p[0][0];
else
{
for(t=0;t<order;t++)
{
z+=pow(-1,0+t+2)*p[0][t]*minor(p,order,0,t);
}
return z;
}
}

OUTPUT
Enter the order of the matrix:5
Enter the elements of matrix
12896
26478
23698
14523
17895

your entered matrix is


1.0 2.0 8.0 9.0 6.0

2.0 6.0 4.0 7.0 8.0

2.0 3.0 6.0 9.0 8.0

1.0 4.0 5.0 2.0 3.0

1.0 7.0 8.0 9.0 5.0

Determinant is -323.000000

The inverse of the matrix is


-2.046440 -1.650155 2.631579 0.495356 0.588235

-0.061920 0.133127 -0.157895 -0.006192 0.117647

0.136223 -0.092879 -0.052632 0.213622 -0.058824

-0.229102 -0.207430 0.315789 -0.222910 0.235294

0.690402 0.665635 -0.789474 -0.030960 -0.411765


EXP-2
AIM:- Write a program for finding any possible largest string in the text when the string
is taken as an input

#include<conio.h>
#include<stdio.h>
//arr to store string
//arr1 to store position of spaces
//arr2 to store length of words b/w spaces
//max to store length of longest word or substring
//maxn to store position of longest word
//IMP user must give a space after end of input string before entering it
void main()
{ char arr[50];
int arr1[50],arr2[50];
int i,j,k,max,maxn;
clrscr();
printf("Enter String\n\n");
for(i=0;i<50;i++)
{
scanf("%c",&arr[i]);
if(arr[i]=='\n')
break;
}

k=1; arr1[0]=0;
for(i=0;;i++)
{ if((arr[i]==' '))
{ arr1[k]=i;
k++;
// printf("%d ",i);
}
if(arr[i]=='\n')
break;
}

for(i=0;i<k;i++)
{ arr2[i]=arr1[i+1]-arr1[i]; }

maxn=0;max=0;
for(i=0;i<k;i++)
{ if(arr2[i]>max)
{ max=arr2[i];
maxn=i;}
}
// printf("\n");
// printf("%d",maxn);
printf("\nLongrst word entered is : ");
for(i=arr1[maxn];i<arr1[maxn+1];i++)
printf("%c",arr[i]);

getch();
}

OUTPUT

Enter String

Hello lets go to market

Longrst word entered is : market


EXP-3
AIM: - Write a program for generating n*n matrix which have sum of rows, sum of
columns and sum of diagonals equal.

#include<iostream.h>
#include<conio.h>

int arr[9][9],n;
void display(void)
{ cout<<"\n\nMagic Square\n\n";
for(int i=0;i<n;i++)
{for(int j=0;j<n;j++)
cout<<arr[i][j]<<" ";
cout<<"\n\n";
}
}

void main()
{
clrscr();
int a,b,c;
cout<<"Enter order of matrix(Odd integer only)\n";
cin>>n;
arr[0][n/2]=1;
a=0;b=n/2;
//cout<<"\n"<<a<<" \n"<<b<<"\n";
for(int m=2;m<=n*n;m++)
{ a=a-1;
b=b+1;
if(a<0)
a=a+n;
if(b>n-1)
b=b-(n);
if(arr[a][b]==0)
arr[a][b]=m;
else
{ a=a+2;
b=b-1;
if(a>(n-1))
a=a-(n);
if(b<0)
b=b+n;
arr[a][b]=m;
}
}
display();
getch();
}

OUTPUT

Enter order of matrix(Odd integer only)


7

Magic Square

30 39 48 1 10 19 28

38 47 7 9 18 27 29

46 6 8 17 26 35 37

5 14 16 25 34 36 45

13 15 24 33 42 44 4

21 23 32 41 43 3 12

22 31 40 49 2 11 20
EXP-4
AIM: - Write a program for finding English equivalent of a given input number(<9999)

# include<stdio.h>
# include<conio.h>
void main()
{ int p,n;
char a[30][10]={"one ","two ","three ","four ","five ","six ","seven ","eight ","nine
","ten ","eleven ","twelv e","thirteen ","fourteen ","fifteen ","sixteen ","seventeen
","eighteen ","nineteen ","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty
","ninety "};
clrscr();
while(1)
{
printf("enter a no.(0 to 9999):");
scanf("%d",&n);
if(n>=0 && n<=9999)
break;
}
if(n==0)
{
printf("zero");
getch();
exit(0);
}
p=n/1000;
if(p!=0)
{
printf("%s",a[p-1]);
printf("thousand ");
}
n=n-(p*1000);
p=n/100;
if(p!=0)
{
printf("%s",a[p-1]);
printf("hundred ");
}
n=n-(p*100);
if(n>=1 && n<=20)
{
printf("%s",a[n-1]);
getch();
exit(0);
}
p=n/10;
if(p!=0)
{
printf("%s",a[p+17]);
}
n=n-(p*10);
if(n!=0)
{
printf("%s",a[n-1]);
}
getch();
}

OUTPUT

enter a no.(0 to 9999):7358


seven thousand three hundred fifty eight
EXP-5
AIM: - To write a program for searching a value in the list using linear search and
binary search method and display the time taken in each case

#include<stdio.h>
#include<conio.h>
#include<dos.h>

void linearsearch(int*a,int b,int c);


/* a is the array of size b in which value c is to be searched. */
void binarysearch(int*a,int b,int c);
/*For binarysearch data must be in sorted order*/

void main()
{
struct dostime_t t1,t2,t3;
int a[10],i,d;
clrscr();
printf("Enter the elements of A:");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nArray A:");
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n\nEnter the value to be searched:");
scanf("%d",&d);
_dos_gettime(&t1);
printf("\nBinary Searching");
binarysearch(a,10,d);
_dos_gettime(&t2);
printf("\nIn binarysearching Execution time=0.%02d second",(t2.hsecond-
t1.hsecond)/100);
printf("\n\nLinearsearching");
linearsearch(a,10,d);
_dos_gettime(&t3);
printf("\nIn linearsearching Execution time=0.%02d second",(t3.hsecond-
t2.hsecond)/100);
getch();
}

void linearsearch(int *a,int b,int c)


{
int i;
for(i=0;i<b;i++)
{
if(a[i]==c)
{
printf("\nElement %d is found at location %d\n",c,i+1);
return;
}
}
printf("\n%d is not found in the list.\n",c);
}

void binarysearch(int *a,int b,int c)


{
int m=0,u=0,l=0;
u=b-1;
for(m=(u+l)/2;u>=l;m=(u+l)/2)
{
if(c==a[m])
{
printf("\nElement %d is found at location %d\n",c,m+1);
return;
}
else if(c>a[m])
l=m+1;
else
u=m-1;
}
printf("\n%d is not found in the list.\n",c);
}

OUTPUT

Enter the elements of A:5 9 32 35 39 45 49 87 89 99

Array A:5 9 32 35 39 45 49 87 89 99

Enter the value to be searched:45

Binary Searching
Element 45 is found at location 6

In binarysearching Execution time=0.00 second

Linearsearching
Element 45 is found at location 6

In linearsearching Execution time=0.00 second


EXP -6
AIM:- Write a program to find the age of a person by taking input the present date and
the birth date keeping track of leap years

#include<stdio.h>
#include<conio.h>
void main()
{
int d1,d2,m1,m2,y1,y2;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
clrscr();
printf("Enter the today's date?\n");
scanf("%d%d%d",&d1,&m1,&y1);

printf("\nEnter the date of birth?\n");


scanf("%d%d%d",&d2,&m2,&y2);
if(d1>=d2)
{
if(m1>=m2)
{
printf(" %d years",y1-y2);
printf(" %d months",m1-m2);
printf(" %d days",d1-d2);
}
else
{
printf(" %d years",y1-y2-1);
printf(" %d months",m1-m2+12);
printf(" %d days",d1-d2);
}
getch();
exit(0);
}
else
{
if(m1>m2)
{
printf(" %d years",y1-y2);
printf(" %d months",m1-m2-1);
}
else
{
printf(" %d years",y1-y2-1);
printf(" %d months",m1-m2+11);
}
if (y1%400==0 || (y1%4==0 && y1%100!=0))
printf(" %d days",d1-d2+b[m2-1]);
else
if (y2%400==0 || (y2%4==0 && y2%100!=0))
printf(" %d days",d1-d2+b[m2-1]);
else
printf(" %d days",d1-d2+a[m2-1]);
}
getch();
}

OUTPUT

Enter the today's date?


4 11 2008

Enter the date of birth?


16 11 1987
20 years 11 months 18 days
EXP -7
AIM:- Write a program to store information related to a manager using array of structure.

#include<stdio.h>
#include<conio.h>

struct man{
char name[25];
int age;
int id;
long int salary;

}ar[3];
void main()
{
int i,j;
char name[25],x,y;
clrscr();
for(i=0;i<3;i++)
{
printf("\nEnter the data of %d manager\n",i+1);
printf("\nEnter name : ");
gets(ar[i].name);
printf("\nEnter age : ");
scanf("%d",&ar[i].age);
printf("\nEnter ID : ");
scanf("%d",&ar[i].id);
printf("\nEnter Salary : ");
scanf("%ld",&ar[i].salary);
scanf("%c",&y);
clrscr();

}
clrscr();
for(i=0;i<3;i++)
{
printf("\nName : %s\nID : %d\nAge : %d\nSalary :
%ld\n",ar[i].name,ar[i].id,ar[i].age,ar[i].salary);
}
getch();
}
OUTPUT

Enter the details of manager 1


Enter name:Raj
Enter age:45
Enter salary:362145

Enter the details of manager 2


Enter name:Ravi
Enter age:42
Enter salary:563214

Enter the details of manager 3


Enter name:Aryan
Enter age:56
Enter salary:563296

Details of managers are:

Manager 1
Name:Raj
Age:45
Salary:Rs.362145

Manager 2
Name:Ravi
Age:42
Salary:Rs.563214

Manager 3
Name:Aryan
Age:56
Salary:Rs.563296
EXP-8
AIM :- To write a program for storing information of a student using linked list.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

struct node{
int id;
char name[15];
node *next;
}*start,*rear,*save,*ptr,*nwptr;
int no;

node *new_node(int n,char na[])


{ no++; ptr=start;
ptr=new node;
ptr->id=n;
strcpy(ptr->name,na);
ptr->next=NULL;
return ptr;
}

void insert(node* np)


{

if(start==NULL)
{start=np;
rear=np;
}
else
{rear->next=np;
rear=np;
}
}

void display(node* np)


{ int n=1;
while(np!=NULL)
{ cout<<"\n"<<n<<" "<<np->id<<" "<<np->name<<"\n\n";
np=np->next;
n++;
}
cout<<"\n";
}

void main()
{ clrscr();
start=NULL;
int id;
char ch,nam[15];
no=0;
cout<<"Insert information\n\n";

do{
cout<<"Enter ID No.of student\n";
cin>>id;
cout<<"Enter name of student\n";
gets(nam);
nwptr=new_node(id,nam);
insert(nwptr);
display(start);
cout<<"Continue to enter information\n(Press y for yes)";
cin>>ch;
}while((ch=='y')||(ch=='Y'));

getch();
}

OUTPUT

Insert information

Enter ID No.of student


987
Enter name of student
Ram Nath

1 987 Ram Nath

Continue to enter information


(Press y for yes)y
Enter ID No.of student
988
Enter name of student
Raj
1 987 Ram Nath

2 988 Raj

Continue to enter information


(Press y for yes)
EXP-9
AIM :- Write a program for generating a single link list in which insertion, deletion and
modification of a node is possible.

#include<iostream.h>
#include<conio.h>

struct node{
int info;
node *next;
}*start,*rear,*save,*ptr,*nwptr;
int no;

node *new_node(int n)
{ no++; ptr=start;
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}

void insert(node* np,int pos)


{int p=0; ptr=start;

if(pos<=1)
{
cout<<"Inserting node in begining\n";
if(start==NULL)
{
start=np;
rear=np;
}
else
{save=start;
start=np;
np->next=save;
}
}
else if(pos>=no)
{
cout<<"Inserting node in the end\n";
{if(start==NULL)
{start=np;
rear=np;
}
else
{rear->next=np;
rear=np;
}
}
}
else
{while(!(p==pos-2))
{ ptr=ptr->next;
p++;
}
save=ptr->next;
ptr->next=np;
np->next=save;
}
}

void del(int pos)


{ int p=0;
ptr=start;

if(pos<=1)
{ save=start;
start=start->next;
no--;
delete save;
}

else if(pos>=no)
{ while(!(p==no-2))
{ptr=ptr->next;
p++;
}
save=ptr->next;
ptr->next=NULL;
rear=ptr;
no--;

delete save;
}
else
{ while(!(p==pos-2))
{ptr=ptr->next;
p++;
}
save=ptr->next;
ptr->next=save->next;
no--;
delete save;
}
}
void modify(int pos,int data)
{ if(no==0)
{cout<<"There is no node to modify\n";
return;
}
int p=0;
ptr=start;

if(pos<=1)
{ start->info=data;
}

else if(pos>=no)
{ rear->info=data;
}
else
{ while(!(p==pos-1))
{ ptr=ptr->next;
p++;
}
ptr->info=data;
}
}

void display(node* np)


{ while(np!=NULL)
{ cout<<np->info<<"->";
np=np->next;
}
cout<<"\n";
}

void main()
{ clrscr();
start=NULL;
int inf,c,pos;
char ch;
no=0;
do{
cout<<"MENU\n\n";
cout<<"Enter your choice\n";
cout<<"1.Insert node\n2.Delete node\n3.Modify content of node\n";
cin>>c;
switch(c)
{
case 1: cout<<"Enter information for node\n";
cin>>inf;
nwptr=new_node(inf);
cout<<"Position for node\n";
cin>>pos;
insert(nwptr,pos);
break;
case 2: if(no==0)
{ cout<<"First create and insert some nodes\n";
break;}
cout<<"Enter position of node\n";
cin>>pos;
del(pos);
break;
case 3: if(no==0)
{ cout<<"First create and insert some nodes\n";
break;}

cout<<"Enter position of node to be modified\n";


cin>>pos;
cout<<"Enter new information\n";
cin>>inf;
modify(pos,inf);
break;
default: cout<<"Enter correct choice\n";
break;
}

display(start);
cout<<"Continue\n";
cin>>ch;
}while((ch=='y')||(ch=='Y'));

getch();
}

OUTPUT
MENU

Enter your choice


1.Insert node
2.Delete node
3.Modify content of node
1
Enter information for node
111
Position for node
1
111->
Continue
y
MENU

Enter your choice


1.Insert node
2.Delete node
3.Modify content of node
1
Enter information for node
222
Position for node
2
111->222->
Continue
y
MENU

Enter your choice


1.Insert node
2.Delete node
3.Modify content of node
1
Enter information for node
333
Position for node
2
111->333->222->
Continue
y
MENU

Enter your choice


1.Insert node
2.Delete node
3.Modify content of node
3
Enter position of node to be modified
1
Enter new information
555
555->333->222->
Continue
y
MENU

Enter your choice


1.Insert node
2.Delete node
3.Modify content of node
2
Enter position of node
3
555->333->
Continue
EXP-10
AIM:- Write a program for traversing a tree

#include<stdio.h>
#include<conio.h>
#include<alloc.h>

struct node
{
struct node *right;
struct node *left;
int data;
};

/*For building a tree according to the data in array A,l & r.*/
struct node* buildtree(int n);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);

void main()
{
struct node* tree;
clrscr();
tree=buildtree(0);
printf("\nInorder traversal: ");
inorder(tree);
printf("\n\nPreorder traversal: ");
preorder(tree);
printf("\n\nPostorder traversal: ");
postorder(tree);
getch();
}

struct node* buildtree(int n)


{
struct node *tree=NULL;

/*Values of nodes*/
int A[]={20,17,6,8,10,7,18,13,12,5};
/*Address of left node pointed by corresponding node in A*/
int l[]={1,2,9,5,-1,-1,-1,8,-1,-1};
/*Address of right node pointed by corresponding node in A*/
int r[]={-1,6,3,4,7,-1,-1,-1,-1,-1};
/* -1 indicates that the node is pointing to NULL*/

if(n!=(-1))
{
tree=(struct node*)malloc(sizeof(struct node));
tree->left=buildtree(l[n]);
tree->data=A[n];
tree->right=buildtree(r[n]);
}
return(tree);
}

void inorder(struct node *tree)


{
if(tree!=NULL)
{
inorder(tree->left);
printf("%d ",tree->data);
inorder(tree->right);
}
}

void preorder(struct node *tree)


{
if(tree!=NULL)
{
printf("%d ",tree->data);
preorder(tree->left);
preorder(tree->right);
}
}

void postorder(struct node *tree)


{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
printf("%d ",tree->data);
}
}

OUTPUT

Inorder traversal: 5 6 7 8 10 12 13 17 18 20
Preorder traversal: 20 17 6 5 8 7 10 13 12 18
Postorder traversal: 5 7 12 13 10 8 6 18 17 20
EXP-12
AIM: - Write a program for storing, appending and retrieving information from a file.

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f;
char str[50],s[1],c;
int i,ch;
clrscr();
do{
printf("1.Storing\n2.Appending\n3.Retrieving\n");
printf("Enter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
f=fopen("abc.txt","w");
if(f==NULL)
{
printf("Cannot open file ");
exit(1);
}
printf("Enter string to be written to file ");
gets(str);
gets(str);
fputs(str,f);
fclose(f);
break;
case 2:
f=fopen("abc.txt","a");
if(f==NULL)
{
printf("Cannot open file ");
exit(1);
}
printf("Enter the string to be appended to file ");
gets(str);
gets(str);
fputs(str,f);
fclose(f);
break;
case 3:
f=fopen("abc.txt","r");
if(f==NULL)
{
printf("Cannot open file ");
exit(1);
}
fgets(str,50,f);
printf("%s",str);
fclose(f);
break;
default: break;
}
printf("\nContinue(Press'y') ");
scanf("%c",&c);
}while((c=='y')||(c=='Y'));

getch();
}

OUTPUT

1.Storing
2.Appending
3.Retrieving
Enter your choice 1
Enter string to be written to file Hello i am Manoj

Continue(Press'y') y
1.Storing
2.Appending
3.Retrieving
Enter your choice 2
Enter the string to be appended to file Who are you?

Continue(Press'y') y
1.Storing
2.Appending
3.Retrieving
Enter your choice 3
Hello i am Manoj Who are you?
Continue(Press'y')
EXP-13
AIM: - Write a program for sorting list of numbers using quick, selection, merge and
insertion sort techniques.

#include<stdio.h>
#include<conio.h>

void creation(int*,int);
void print(int*,int);
void selectionsort(int*,int);
void quicksort(int*,int,int);
int split(int*,int,int);
void mergesort(int*,int*,int*,int,int);
void insertionsort(int*,int);

void main()
{
int a[10],b[5],c[15],i;
//clrscr();
printf("\nEnter the elements of array A having 10 elements:");
creation(a,10);
printf("\nBefore sorting Array A:");
print(a,10);
printf("\nFor mergesorting press 1.\nFor quicksorting press 2.\n");
printf("For insertionsorting press 3.\nFor selectionsorting press 4.\n");
printf("Enter your choice:");
again:
scanf("%d",&i);
switch(i)
{
case 1:printf("\nEnter the elements of array B having 5 elements:");
creation(b,5);
printf("\nBefore sorting Array B:");
print(b,5);
mergesort(a,b,c,10,5);
break;

case 2:quicksort(a,0,9);
printf("After quicksorting array is:");
print(a,10);
break;

case 3:insertionsort(a,10);
break;

case 4:selectionsort(a,10);
break;
default:printf("Enter the correct choice:");
goto again;
}
getch();
}

void creation(int *a,int n)


{
int i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}

void print(int *a,int n)


{
int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}

void selectionsort(int *a,int n)


{
int i,j,t;
for(i=0;i<(n-1);i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}
printf("After selectionsorting array is:");
print(a,n);
}

void insertionsort(int *a,int n)


{
int i,j,k,t;
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[i]<a[j])
{
t=a[i];
for(k=i;k!=j;k--)
a[k]=a[k-1];
a[j]=t;
}
}
}
printf("After insertionsorting array is:");
print(a,n);
}

void quicksort(int *a,int l,int u)


{
int i;
if(u>l)
{
i=split(a,l,u);
quicksort(a,l,i-1);
quicksort(a,i+1,u);
}
}

int split(int *a,int l,int u)


{
int i,p,q,t;
p=l+1;
q=u;
i=a[l];
while(q>=p)
{
while(a[p]<=i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[l];
a[l]=a[q];
a[q]=t;
return(q);
}

void mergesort(int *a,int*b,int *c,int n,int m)


{
int i,j=0,k=0,t;
quicksort(a,0,n-1);
quicksort(b,0,m-1);
for(i=0;i<(n+m);i++)
{
if(a[j]<b[k])
{
c[i]=a[j];
j++;
if(j==n)
break;
}
else
{
c[i]=b[k];
k++;
if(k==m)
break;
}
}
if(j==n)
for(t=k;t<m;t++,i++)
c[i+1]=b[t];
else
for(t=j;t<n;t++,i++)
c[i+1]=a[t];
printf("\nAfter quicksorting array is:");
print(a,10);
printf("\nAfter quicksorting array is:");
print(b,5);
printf("\nHence After mergesorting array is:");
print(c,n+m);
}

OUTPUT

(i) For the purpose of mergesorting

Enter the elements of array A having 10 elements:1 5 6 3 8 0 4 9 2 0

Before sorting Array A:1 5 6 3 8 0 4 9 2 0

For mergesorting press 1.


For quicksorting press 2.
For insertionsorting press 3.
For selectionsorting press 4.
Enter your choice:1

Enter the elements of array B having 5 elements:5 3 9 8 7

Before sorting Array B:5 3 9 8 7

After quicksorting array is:0 0 1 2 3 4 5 6 8 9

After quicksorting array is:3 5 7 8 9

Hence After mergesorting array is:0 0 1 2 3 3 4 5 5 6 7 8 8 9 9

(b) For other sorting techniques:

Enter the elements of array A having 10 elements:1 5 6 3 8 0 4 9 2 0

Before sorting Array A:1 5 6 3 8 0 4 9 2 0

For mergesorting press 1.


For quicksorting press 2.
For insertionsorting press 3.
For selectionsorting press 4.
Enter your choice:3
After insertion sorting array is: 0 0 1 2 3 3 4 5 5 6 7 8 8 9 9
EXP-14
AIM: - Write a program to determine the day of a given date

#include<stdio.h>
#include<conio.h>

void main()
{
long int a,b,d,i,e,c;
int s[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int s1[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
char
day[][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday
"};
d=0;
clrscr();
printf("Enter the date : ");
scanf("%ld%ld%ld",&a,&b,&c);
//printf("Enter the month : ");
//scanf("%ld",&b);
//printf("Enter the year : ");
//scanf("%ld",&c);
for(i=1;i<c;i++)
{
if((i%4)!=0)
d+=365;
else if((i%4)==0)
{

if((i%100)!=0)
d+=366;
else if((i%100)==0)
{
if((i%400)==0)
d+=366;
else
d+=365;
}
}
}
for(i=1;i<b;i++)
{ if((c%4)!=0)
d+=s[i];
else if((c%4)==0)
{
if((c%100)!=0)
d+=s1[i];
else if((c%100)==0)
{
if((c%400)==0)
d+=s1[i];
else
d+=s[i];
}
}
}
d+=a;
e=d%7;
// printf("%ld",d);
// printf("\n%ld",e);
// printf("\n");
puts(day[e]);
getch();
}

OUTPUT
Enter the date : 3 11 2008
Monday

You might also like