Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Program 6

Write a program to perform linear search.


Source Code:
#include<iostream.h>
#include<conio.h>

int linearSearch(int* a,int item,int len)


{
for(int i=0;i<len;i++)
{
if(a[i]==item)
{
return i;
}
}
return -1;
}
void main()
{
clrscr();
int a[]={11,2,33,4,5,46,7,8,9,10};
for(int c=0;c<10;c++)
{
cout<<a[c]<<" ";
}

int search=linearSearch(a,5,10);
cout<<"\nSearching 5 using linear search \n";
if(search==-1)
{
cout<<"\n Not Found";
}
else
{
cout<<"\n Found at location " <<search;
}
getch();
}

Output:

Program 7
Write a program to implement binary search.

Source Code:
#include<iostream.h>
#include<conio.h>

int binarySearch(int* a,int len,int item )


{
clrscr();
int low=0,high=len,mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]<item)
{
low=mid+1;
}
else if(a[mid]>item)
{
high=mid-1;
}
else if(a[mid]==item)
{
return mid;
}
}
return -1;

}
void main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int search=binarySearch(a,9,4);
for(int c=0;c<10;c++)
{
cout<<a[c]<<" ";
}
cout<<"\n searching 4 using binary search \n" ;
if(search==-1)
{
cout<<"Item not found \n" ;
}
else
{
cout<<"item found at location "<<search;
}
getch();
}

Output:

Program 8
Write a program to implement Strassens
algorithm for matrix multiplication.
Source Code:
#include<iostream.h>

#include<conio.h>
void main()
{
int a[2][2], b[2][2], c[2][2], i, j;
int p,q,r,s,t,u,v;
clrscr();
cout<<"Enter the four elements of the first matrix row-wise: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<"Enter the four elements of the second matrix row-wise: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>b[i][j];
}

}
p = (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);
q = (a[1][0] + a[1][1]) * b[0][0];
r = a[0][0] * (b[0][1] -b[1][1]);

s = a[1][1] * (b[1][0] - b[0][0]);


t = (a[0][0] + a[0][1]) * b[1][1];
u = (a[1][0] - a[0][0]) * (b[0][0] + b[0][1]);
v = (a[0][1] - a[1][1]) * (b[1][0] + b[1][1]);
c[0][0] = p + s - t + v;
c[0][1] = r + t;
c[1][0] = q + s;
c[1][1] = p - q + r + u;
cout<<" Matrix after multiplication: ";
for(i=0;i<2;i++)
{
cout<<"\n";
for(j=0;j<2;j++)
{
cout<<c[i][j];
cout<<"
}
}
getch();
}

Output:

";

You might also like