Documente Academic
Documente Profesional
Documente Cultură
ARR[TEMP]
TEMP=TEMP-1
STEP 3:ARR[POSITION]=ELEMENT
STEP4:NUM=NUM+1
STEP5:RETURN(NUM)
ROLLN0.1100514006
ROLLN0.1100514006
case 4:
exit(0);
}
}
getch();
}
void insert(int a[],int n)
{
for(i=1;i<=n;i++)
{
printf("\n enter rhe data\n ");
scanf("%d",&a[i]);
}
}
void display(int a[],int n)
{
for(i=1;i<n;i++)
{
printf("array is data is %d\n ",a[i]);
}
}
int position(int a[],int p,int m)
{
for(i=n;i>p;i--)
{
a[i]=a[i-1];
}
a[p]=m;
n++;
return(n);
}
ROLLN0.1100514006
OUTPUT:
ROLLN0.1100514006
ROLLN0.1100514006
ROLLN0.1100514006
fflush(stdin);
puts(emp[i]);
}
}
int del(char emp[s][s],int n,char a[s])
{
int p,temp,i;
printf("enter the pos\n");
scanf("%d",&p);
temp=p;
a=emp[p];
while(temp<=n-1)
{
for(i=0;i<s;i++)
{
emp[temp][i]=emp[temp+1][i];
}
temp++;
}
n=n-1;
return(n);
}
ROLLN0.1100514006
OUTPUT
ROLLN0.1100514006
ROLLN0.1100514006
10
ROLLN0.1100514006
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
arr[i]=arr[j];
for(k=j;k<n; k++)
{
arr[k]=arr[k+1];
c=c+1;
}
}
}
}
n=n-1;
if(c==0)
printf("\n array is no no of element duplicate\n");
else
printf("\n array is no of element is
duplicate\n",c);
return(n);
}
OUTPUT
ANKIT KUMAR SHARMA
11
ROLLN0.1100514006
12
ROLLN0.1100514006
STEP 1: IF COL1=ROW2
REPEAT FOR I=0,1,2..ROW1- 1
REPEAT FOR J=0,1,2..COL2 - 1
MAT_RES[I][J]=0
REPEAT FOR K=0,1,2COL1 - 1
MAT_RES[I][J]=MAT_RES[I][J] + MAT1[I][K] * MAT2[K][J]
ELSE IF ROW1=COL2
REPEAT FOR I=0,1,2..COL1 - 1
REPEAT FOR J=0,1,2..ROW2 - 1
MAT_RES[I][J]=0
REPEAT FOR K=0,1,2.COL2 1
MAT_RES[I][J]=MAT_RES[I][J] + MAT1[I][K] * MAT2[K][J]
ELSE
OUTPUTMULTIPLICATION IS NOT POSSIBLE AND EXIT
STEP 2: EXIT
W.A.P TO MATRIX
ANKIT KUMAR SHARMA
MULTIPLICATION
13
ROLLN0.1100514006
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int mat1[3][3],mat2[3][3],mat3[3][3],i,j,k,sum;
clrscr();
printf("enter the first matrix\n ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&mat1[i][j]);
}
printf("enter the secand matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&mat2[i][j]);
}
printf("display 1 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
printf("display 2 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mat2[i][j]);
}
14
ROLLN0.1100514006
printf("\n");
}
printf("calculate the matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
sum=sum+mat1[i][k]*mat2[k][j];
mat3[i][j]=sum;
}
}
printf("display 3 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
getch();
}
OUTPUT
ANKIT KUMAR SHARMA
15
ROLLN0.1100514006
16
ROLLN0.1100514006
17
ROLLN0.1100514006
18
ROLLN0.1100514006
OUTPUT
19
ROLLN0.1100514006
20
ROLLN0.1100514006
21
ROLLN0.1100514006
OUTPUT
22
ROLLN0.1100514006
23
ROLLN0.1100514006
24
ROLLN0.1100514006
}
void insert(int q[10],int n)
{
if(rear==9)
printf("que is full");
else if(front==-1)
{
front=0;
q[++rear]=n;
}
else
q[++rear]=n;
}
void display(int q[10])
{
int i;
if(front==-1)
{
printf("queue is empty");
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d",q[i]);
}}}
void del(int q[10])
{
int k;
if(front==-1)
{
printf("que is empty");
}
else
{
k=q[front];
printf("delete element %d",k);
front++;
}}
25
ROLLN0.1100514006
OUTPUT
26
ROLLN0.1100514006
27
ROLLN0.1100514006
28
ROLLN0.1100514006
case 'q':k=1;
}
}while(!k);
}
void push(int a[10],int n)
{
if(top==9)
printf("stck is full");
else
++top;
a[top]=n;
}
void display(int a[10])
{
if(top==-1)
printf("stck is empty\n " );
else
for(i=top;i>0;i--)
{
printf("\n%d\t",a[i]);
}
}
void pop(int a[10])
{
int k;
if(top==-1)
printf("stack is empty");
else
{
k=a[top];
printf("delete iteam =%d",k);
--top;
}
}
29
ROLLN0.1100514006
OUTPUT
30
ROLLN0.1100514006
ALGORITHM OF GCD
Step 1: read two number A&B.
Step 2: function int gcd(int x,int y)
Step 3: while(x!=y)
Step 4: if(x>y)
return gcd(x-y,y);
Step 5: else
Step 6 return gcd(x,y-x);
Step 7: return x;
31
ROLLN0.1100514006
32
ROLLN0.1100514006
OUTPUT
33
ROLLN0.1100514006
34
ROLLN0.1100514006
W.A.P OF
FIBONACCI
SERIES
#include<stdio.h>
#include<conio.h>
long fibo_num(int);
void main()
{
int n;
int i=0;
printf("enter the value");
scanf("%d",&n);
printf("\n fibo num \n");
while(i<n)
{
i++;
printf("\n %d",fibo_num(i));
}
getch();
}
long fibo_num(int n)
{
int r;
if((n==1)||(n==0))
return(0);
else
if(n==2)
return(1);
else
return(fibo_num(n-1)+fibo_num(n-2));
}
35
ROLLN0.1100514006
OUTPUT
36
ROLLN0.1100514006
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
# define size 10
char ch;
int q[size];
int rear=0;
int front=0;
void insert_queue();
void display_queue();
void delet_queue();
void dqinsert_front();
void dqdelet_rear();
void insert_queue()
{
printf("\n input the element:= ");
scanf("%d",&ch);
if(rear<size-1)
{
ANKIT KUMAR SHARMA
37
ROLLN0.1100514006
rear++;
q[rear]=ch;
if (front==0)
front=1;
}
else
printf("\n queue is overflow");
}
void delete_queue()
{
if(front==0)
{
printf("\nunderflow");
return;
}
else
{
ch=q[front];
printf("\n element deleted:= %d",ch);
}
if(front==rear)
{
front=0;
ANKIT KUMAR SHARMA
38
ROLLN0.1100514006
rear=0;
}
else
front=front+1;
}
void dqinsert_front()
{
if(front==1)
printf("\nqueue is full");
else
{
front=front-1;
printf("\n input the element=");
scanf("%d",&ch);
q[front]=ch;
}
}
void dqdelet_rear()
{
if (front==rear)
{
front=0;
rear=0;
ANKIT KUMAR SHARMA
39
ROLLN0.1100514006
printf("\nqueue is empty");
}
else
{
printf("element deleted= %d",q[rear]);
rear=rear-1;
}
}
void display_queue()
{
int i;
if (front==0)
return;
for(i=front;i<=rear;i++)
printf(" %d",q[i]);
printf("\n------------------------------------"
);
}
void main()
{
clrscr();
int k=0;
char choice;
40
ROLLN0.1100514006
do
{
printf("\n insert->i delete->d quit->q
dqinsert->b dqdelet->e");
printf("\n input the choice:= ");
do
{
choice=getchar();
choice=tolower(choice);
}
while(strchr("idqbe",choice)==NULL);
switch(choice)
{
case'i': insert_queue();
printf("\n queue after inserting=");
display_queue();
break;
case'd': delete_queue();
printf("\nqueue content after deletion is=")
display_queue();
break;
case'b':dqinsert_front();
printf("\n again insert=");
41
ROLLN0.1100514006
display_queue();
break;
case'e':
dqdelet_rear();
printf("\n deleted element=");
display_queue();
break;
case'q':
(k=1);
break;
}}
while(!k);
getch();
}
42
ROLLN0.1100514006
OUTPUT
43
ROLLN0.1100514006
INSERTION_SORT(A)
FOR J<-2 TO N
DO KEY<-A[J]
I<-J-1
WHILE I>0 AND A[I]>KEY
DO A[I+1]<-A[I]
I<-I-1
A[I+1]<-KEY
EXIT
44
ROLLN0.1100514006
#include<stdio.h>
#include<conio.h>
void input(int [],int);
void display(int[],int);
void shorts(int [],int);
int a[10],i;
void main()
{
int n;
clrscr();
printf("enter the length of array");
scanf("%d",&n);
input(a,n);
shorts(a,n);
display(a,n);
getch();
}
void input(int a[10],int n)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void display(int a[10],int n)
{
printf("\n after shorting data");
for(i=0;i<n;i++)
{
printf("\ndata is= %d\t",a[i]);
}
}
void shorts(int a[10],int n)
{
int j,temp;
for(j=1;j<n;j++)
{
temp=a[j];
i=j-1;
45
ROLLN0.1100514006
while ((i>=0)&&(a[i]>temp))
{
a[i+1]=a[i];
i=i-1;
}
a[i+1]=temp;
}
}
OUTPUT
46
ROLLN0.1100514006
ALGORITHM
INSERT
Description: Here QUEUE is an array with N
locations. FRONT and REAR points to the front
and rear
elements of the QUEUE. ITEM is the value to be
inserted.
1. If (FRONT == 1 and REAR == N) or (FRONT
== REAR + 1) Then
2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is
empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches
end if QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
10. Print: ITEM inserted
[End of Step 1 If]
47
ROLLN0.1100514006
ALGORITHM
DELETE
Description: Here QUEUE is an array with N
locations. FRONT and REAR points to the front
and rear
elements of the QUEUE.
1. If (FRONT == 0) Then [Check for Underflow]
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only element is
left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches
end if QUEUE]
7. Set FRONT = 1
8. Else
9. Set FRONT = FRONT + 1 [Increment FRONT
by 1]
[End of Step 5 If]
10. Print: ITEM deleted
[End of Step 1 If]
48
ROLLN0.1100514006
49
ROLLN0.1100514006
scanf("%d",&n);
insert(q,n);
break;
case 'd': del(q);
break;
case 's':display(q);
break;
case 'q':k=1;
}
}while(!k);
}
void insert(int q[s],int n)
{
if(count==s)
printf("que is full");
else
rear=(rear+1)%s;
q[rear]=n;
count++;
}
void display(int q[s])
{
int i,j=front;
if(count==0)
{
printf("enpy");
}
else
{
for(i=0;i<count;i++)
{
j=(j+1)%s;
printf("%d",q[j]);
}}}
void del(int q[s])
{
int k;
if(count==0)
{
50
ROLLN0.1100514006
printf("que is empty");
}
else
{
k=q[front];
printf("delete element %d",k);
front=(front+1)%s;
count--;
}}
OUTPUT:
51
ROLLN0.1100514006
52
ROLLN0.1100514006
PROGRAM
#include <stdio.h>
#include<conio.h>
void linear_search();
int a[50], item, n, i;
void main()
{
Clrscr();
printf("\n\nEnter size of an array: ");
scanf("%d", &n);
printf("\n\nEnter elements of an array:\n");
for (i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nEnter item to search: ");
scanf("%d", &item);
linear_search();
getch();
}
void linear_search()
{
for (i=0; i<n; i++)
if (item == a[i])
{
printf("\n\nItem found at location %d", i+1);
return;
}
if (i == n)
ANKIT KUMAR SHARMA
53
ROLLN0.1100514006
OUTPUT
54
ROLLN0.1100514006
Algorithm
Description: Here A is a sorted array having N
elements. ITEM is the value to be searched.
BEG denotes
first element and END denotes last element in
the array. MID denotes the middle value.
1. Set BEG = 1 and END = N
ANKIT KUMAR SHARMA
55
ROLLN0.1100514006
PROGRAM
#include <stdio.h>
#include<conio.h>
void binary_search();
int a[50], n, item, loc, beg, mid, end, i;
void main()
{
Clrscr();
ANKIT KUMAR SHARMA
56
ROLLN0.1100514006
OUTPUT
57
ROLLN0.1100514006
58
ROLLN0.1100514006
Algorithm
Description: Here A is an unsorted array
having N elements.
1. Repeat For J = 1 to N
2. Repeat For K = 1 to N-J
3. If (A[K] > A[K+1]) Then
4. Interchange A[K] and A[K+1]
[End of If]
[End of Step 2 For Loop]
[End of Step 1 For Loop]
5. Exit
59
ROLLN0.1100514006
#include <stdio.h>
#include<conio.h>
void bubble_sort();
int a[50], n;
void main()
{
int i;
clrscr();
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
bubble_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void bubble_sort()
{
int j, k, temp;
for(j=0; j<n; j++)
for(k=0; k<(n-1)-j; k++)
if(a[k] > a[k+1])
{
temp = a[k];
a[k] = a[k+1];
a[k+1] = temp;
}
}
60
ROLLN0.1100514006
OUTPUT
61
ROLLN0.1100514006
Algorithm
Description: Here A is an unsorted array
having N elements.
1. Repeat For J = 2 to N
2. Set TEMP = A[J]
3. Set K = J - 1
4. Repeat While (K >= 1) and (A[K] > TEMP)
5. Set A[K+1] = A[K]
6. Set K = K - 1
[End of While Loop]
7. Set A[K+1] = TEMP
[End of For Loop]
8. Exit
62
ROLLN0.1100514006
PROGRAM:-
#include <stdio.h>
#include<conio.h>
void insertion_sort();
int a[50],n;
void main()
{
int i;
clrscr();
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
insertion_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void insertion_sort()
{
int j, k, temp;
for(j=1; j<n; j++)
{
temp = a[j];
k = j-1;
while (k>=0 && a[k]>temp)
{
a[k+1] = a[k];
k--;
}
a[k+1] = temp;
}
63
ROLLN0.1100514006
OUTPUT
64
ROLLN0.1100514006
65
ROLLN0.1100514006
PROGRAM:
#include <stdio.h>
#include<conio.h>
void selection_sort();
int a[50], n;
void main()
{
int i;
printf("\nEnter size of an array: ");
scanf("%d", &n);
printf("\nEnter elements of an array:\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
selection_sort();
printf("\n\nAfter sorting:\n");
for(i=0; i<n; i++)
printf("\n%d", a[i]);
getch();
}
void selection_sort()
{
int i, j, min, temp;
for (i=0; i<n; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
ANKIT KUMAR SHARMA
66
ROLLN0.1100514006
OUTPUT
67
ROLLN0.1100514006