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

Program 6: Write A Program To Perform Linear Search

The document describes 3 programs: 1) A program to implement linear search on an array containing integers. It defines a linearSearch function that searches for an item in the array and returns its index if found, or -1 if not found. 2) A program to implement binary search on a sorted array of integers. It defines a binarySearch function that searches for an item and returns its index similarly to linear search. 3) A program to multiply two 2x2 matrices using Strassen's matrix multiplication algorithm. It defines variables to store the intermediate values calculated using Strassen's approach and outputs the multiplied matrix.

Uploaded by

lakshya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Program 6: Write A Program To Perform Linear Search

The document describes 3 programs: 1) A program to implement linear search on an array containing integers. It defines a linearSearch function that searches for an item in the array and returns its index if found, or -1 if not found. 2) A program to implement binary search on a sorted array of integers. It defines a binarySearch function that searches for an item and returns its index similarly to linear search. 3) A program to multiply two 2x2 matrices using Strassen's matrix multiplication algorithm. It defines variables to store the intermediate values calculated using Strassen's approach and outputs the multiplied matrix.

Uploaded by

lakshya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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