Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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:
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:
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:
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 :
ALGORITHM :
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 :
ALGORITHM :
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 :
ALGORITHM :
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 :
ALGORITHM :
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