Sei sulla pagina 1di 22

Algorithm and Programming for

LINEAR SEARCH USING C PROGRAMMING


Algorithm :

LINEAR(DATA,N,ITEM,LOC)

1. Set DATA [N+1] = ITEM
2. Set LOC = 1
3. while DATA[LOC] != ITEM
Set LOC = LOC + 1
4. If LOC = N + 1
Set LOC = 0
5. Exit

Program :

include<stdio.h>
include<conio.h>
void main()

{
int a[10],i,n,m,c=0;
printf("Enter the size of an array");
scanf("%d",&n);
printf("\nEnter the elements of the array");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("\nThe elements of an array are");
for(i=0;i<=n-1;i++)
{
printf(" %d",a[i]);
}
printf("\nEnter the number to be search");
scanf("%d",&m);
for(i=0;i<=n-1;i++)
{
if(a[i]==m)
{
c=1;

break;
}
}
if(c==0)
printf("\nThe number is not in the list");
else
printf("\nThe number is found");
getch();
}
































Output:

Enter the size of an array


8
Enter the elements of the array
1 2 3 4 5 6 7 8
Enter the number to be search : 6
Result: The number is found
Enter the number to be search : 9
Result: The number is not in the list

Algorithm and Programming for


BINARY SEARCH USING C PROGRAMMING

Algorithm:

BINARY(DATA,LB,UB,ITEM,LOC)

Set BEG = LB,END = UB and MID = INT(BEG+END/2)
Repeat Steps 3 and 4,while BEG<=ND and DATA[MID] !=
ITEM
If ITEM < DATA[MID]
Then
Set END = MID - 1
Else
Set BEG = MID + 1
Set MID = INT (BEG + END)/2
If DATA[MID] = ITEM
Then
Set LOC = MID
Else
Set LOC = NULL
Exit

PROGRAM :

include<stdio.h>
include<conio.h>
Void main()
{
int a[10],i,n,m,c=0,l,u,mid;
clrscr();
printf("Enter the size of an array->");
scanf("%d",&n);
printf("\nEnter the elements of the array->");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nThe elements of an array are->");
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
printf("\nEnter the number to be search->");
scanf("%d",&m);

l=0,u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
break;
}
else if(m<a[mid])
{
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("\nThe number is not in the list");
else
printf("\nThe number is found");
getch();
}

OUTPUT:

Enter the size of an array - 7



Enter the elements of the array
41
52
63
84
95
46
67

The elements of an array are
41
52
63
84
95
46
67

Enter the number to be search - 53

Result : The number is not in the list

Enter the number to be search - 84

Result : The number is found

Algorithm and Programming for


BUBBLE SORT USING C PROGRAMMING


ALGORITHM :

BUBBLE(A,N)

. Repeat 2 & 3 for K=1 to N-1
. Set PTR = 1
3. hile PTR <= N-K
(a)If A[PTR]>a[PTR+1]
Then INTERCHANGE
(b)PTR = PTR + 1
EXIT

PROGRAM:

include<stdio.h>
include<conio.h>
void main()
{
int s,temp,i,j,a[20];
clrscr();
printf("\nEnter size of the array: ");
scanf("%d",&s);
printf("\nEnter %d elements in to the array:",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s-1;i++)
{
for(j=0;j<s-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe array after sorting is: ");
for(i=0;i<s;i++);
printf(" %d",a[i]);
getch();
}

OUTPUT:

Enter size of the array: 6



Enter elements in to the array:
56
24
63
12
8
69
The array after sorting is:8 12 24 56 63 69





































Algorithm and Programming for


SELECTION SORT USING C PROGRAMMING

ALGORITHM :

for i = 1:n,
k = i
for j = i+1:n, if a[j] < a[k], k = j
invariant: a[k] smallest of a[i..n]
swap a[i,k]
invariant: a[l..i] in final position
end

PROGRAM :

include<stdio.h>
include<conio.h>
void main()
{
int s,i,j,temp,a[20];
clrscr();
printf("\nEnter size of the array :");
scanf("%d",&s);
printf("\nEnter %d elements in to the array:");
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe array after sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
getch();
}


OUTPUT :

Enter size of the array : 7



Enter elements in to the array :
14
25
7
65
43
21
89

The array after sorting is:
7 14 21 25 43 65 89

Algorithm and Programming for


MATRIX ADDITION USING C PROGRAMMING

ALGORITHM :

int A{n){n), 8{n){n), C{n){n) //--> declaring 3 matrices in a


common 1ormat.
int i=]= //---> declaring variables.i and ] values changes 1rom
to 3 in the pgm.
1or{i= i<= 3 i++}
(
1or{]= ]<= 3 ]++}
(
C{i){])=A{i){])+8{i){])
]
]

PROGRAM :

include<stdio.h>
include<conio.h>
void main()

{

int a[2][2],b[2][2],c[2][2],i,j;

printf("Enter the First matrix->");

for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);

printf("\nEnter the Second matrix->");

for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);

printf("\nThe First matrix is\n");
for(i=0;i<2;i++)
{
printf("\n");

for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}

printf("\nThe Second matrix is\n");
for(i=0;i<2;i++)
{
printf("\n");

for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nThe Addition of two matrix is\n");

for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
getch();
}

OUTPUT :

Enter the First matrix - 2 3


5 7
Enter the Second matrix - 4 6
5 4
The Addition of two matrix is 6 9
10 11

Algorithm and Programming for


MATRIX SUBTRACTION USING C PROGRAMMING

ALGORITHM :

int A{n){n), 8{n){n), C{n){n) //--> declaring 3 matrices in a


common 1ormat.
int i=]= //---> declaring variables.i and ] values changes 1rom
to 3 in the pgm.
1or{i= i<= 3 i++}
(
1or{]= ]<= 3 ]++}
(
C{i){])=A{i){])-8{i){])
]
]

PROGRAM :

include<stdio.h>
include<conio.h>
void main()

{

int a[2][2],b[2][2],c[2][2],i,j;

printf("Enter the First matrix->");

for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);

printf("\nEnter the Second matrix->");

for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);

printf("\nThe First matrix is\n");
for(i=0;i<2;i++)
{
printf("\n");

for(j=0;j<2;j++)

printf("%d\t",a[i][j]);
}

printf("\nThe Second matrix is\n");
for(i=0;i<2;i++)
{
printf("\n");

for(j=0;j<2;j++)
printf("%d\t",b[i][j]);
}
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]-b[i][j];
printf("\nThe Subtraction of two matrix is\n");

for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
getch();
}


OUTPUT :

Enter the First matrix - 8 9


5 7
Enter the Second matrix - 4 6
5 4
The Subtraction of two matrix is 4 3
0 3

Algorithm and Programming for


TRANSPOSE OF A MATRIX USING C PROGRAMMING

ALGORITHM :

1.for (int i = 0; i < 4; i++) {


2.for (int j = i + 1; j < 4; j++) {
3.int save = matrix[i][j];
4.matrix[i][j] = matrix[j][i];
5.matrix[j][i] = save;
6.}
7.}

PROGRAM :

include<stdio.h>
include<conio.h>
void main()
{
int a[10][10],b[10][10],i,j,k=0,m,n;
printf("\nEnter the row and column of matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
b[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[j][i];

printf("\n%d",b[i][j]);
}
}
printf("\n\nTraspose of a matrix is -> ");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
{
printf("%d\t",b[i][j]);
}
}
getch();
}

OUTPUT :

Enter the row and column of matrix 2 2



Enter the First matrix
5 7
9 8

Traspose of a matrix is
5 9
7 8

Algorithm and Programming for


MATRIX MULTIPLICATION USING C PROGRAMMING

ALGORITHM :

void mm_mul (int A[N][N], int B[N][N], int C[N][N])


{
int i, j, k;
int sum;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++) {
sum = 0;
for (k = 0; k < N; k++) {
sum += A[i][k] * B[k][j];
}
C[i][j] = sum;
}
}
}

PROGRAM :

include<stdio.h>
include<conio.h>
void main()
{
int a[3][3],i,j,k,b[3][3],c[3][3];
printf("Enter matrix A:");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("First Matrix A is :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf(Enter matrix B:)
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
printf("Second Matrix B is:\n");
for(i=0;i<3;i++)

{
for(j=0;j<3;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
printf("Multiplication of the Matrices:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
getch();
}

OUTPUT:

Enter matrix A :
1 1 1
1 1 1
1 1 1
Enter matrix B :
2 2 2
2 2 2
2 2 2
Multiplication of the Matrices :
6 6 6
6 6 6
6 6 6

Potrebbero piacerti anche