You are on page 1of 22

Matrix Questions and Important Algorithms

1. Write a program in C to find the sum of two matrices


2. Write a program in C to find the difference of two
matrices.
3. Write a program in C to fund the multiplication of two
matrices.
4. Searching Algorithms : -
 Linear Search
 Binary Search
5. Sorting Algorithms :-
 Bubble Sorting
 Selection Sorting
 Insertion Sorting
 Quick Sort
 Merge Sort

Output :
Enter the number of rows and column 2 2
Matrix 1
11
22

Matrix 2
33
44

4 4
6 6

Program ended with exit code : 0

// Q1. Write a program in C to find the sum of two matrices.


// Date – 16/10/2018
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int main()
{
int arr1[20][20],arr2[20][20],m,n,j,i;
printf("\nEnter the rows and columns\n");
scanf("%d %d",&m,&n) ;
printf("\nMatrix 1\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{scanf("%d",&arr1[i][j]) ;}
}
printf("\nMatrix 2\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{scanf("%d",&arr2[i][j]) ;}
}
printf("\n") ;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",(arr1[i][j]+arr2[i][j]));
}
printf("\n") ;
}
}

Output :
Enter the number of rows and column 2 2
Matrix 1
11
22

Matrix 2
33
44

-2 -2
-2 -2

Program ended with exit code : 0

// Q2. Write a program in C to find the difference of two matrices.


// Date – 16/10/2018
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int main()
{
int arr1[20][20],arr2[20][20],m,n,j,i;
printf("\nEnter the rows and columns\n");
scanf("%d %d",&m,&n) ;
printf("\nMatrix 1\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{scanf("%d",&arr1[i][j]) ;}
}
printf("\nMatrix 2\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{scanf("%d",&arr2[i][j]) ;}
}
printf("\n") ;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",(arr1[i][j]-arr2[i][j]));
}
printf("\n") ;
}
}

Output :
Enter the number of rows of matrix 1 2

Enter the no. of columns of matrix 1 same as no. of rows of second matrix 2

Enter the no. of columns of matrix 2 2

Matrix 1
12
34

Matrix 2
12
34

7 10
15 22
Program ended with exit code : 0

// Q3. Write a program in C to find the multiplication of two matrices.


// Date – 16/10/2018
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int main()
{
int arr1[20][20],arr2[20][20],arr3[20][20],i,j,k,a,b,c,s=0;
printf("\nEnter the no. of rows of first matrix ");
scanf("%d",&a) ;
printf("\nEnter the no. of columns of matrix 1 same as no. of rows of second
matrix ");
scanf("%d",&b) ;
printf("\nEnter the no. of columns of matrix 2 ");
scanf("%d",&c) ;
printf("\nMatrix 1\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{scanf("%d",&arr1[i][j]) ;}
}
printf("\nMatrix 2\n");
for(i=0;i<b;i++)
{
for(j=0;j<c;j++)
{scanf("%d",&arr2[i][j]) ;}
}
printf("\n") ;
for(i=0;i<a;i++)
{
for(j=0;j<c;j++)
{
s=0;
for(k=0;k<b;k++)
{
s+=arr1[i][k]*arr2[k][j];
}
arr3[i][j]=s;
}
}
for(i=0;i<a;i++)
{
for(j=0;j<c;j++)
{
printf("%d ", arr3[i][j]);
}
printf("\n") ;
}
}

Output :
Enter the size of the array 5

Enter the array


10 20 30 40 50

Enter the element to be searched 30

Element does exist


Program ended with exit code : 0

//Q4. Searching Algorithms : Linear Search


// Date – 16/10/2018
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int search(int[],int,int,int);
int main()
{
int arr[50],n,el,pos;
printf("\nEnter the size of the array ");
scanf("%d",&n);
printf("\nEnter the array\n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\nEnter the element to be searched ");
scanf("%d",&el);
pos=search(arr,n,el,0);
if(pos==-1)
{
printf("\nElement does not exist");
}
else
{
printf("\nElement does exist");
}
}
int search(int arr[],int n,int el,int i)
{
if(i==n)
{return -1;}
if(arr[i]==el)
{return 1;}
return search(arr,n,el,i+1);
}

Output :
Enter the size of the array 5

Enter the array


10 20 30 40 50

Enter the element to be searched 20

Element does exist


Program ended with exit code : 0
//Q4. Searching Algorithms : Binary Search
// Date – 16/10/2018

#include<string.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int arr[50],n;
int search(int,int,int);
int main()
{
int el,pos,flag;
int i ;
printf("\nEnter the size of the array ");
scanf("%d",&n);
printf("\nEnter the array\n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\nEnter the element to be searched ");
scanf("%d",&el);
for(i=0;i<n-1;i++)
{
flag=0;
for(int j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
flag=1;
pos=arr[j];
arr[j]=arr[j+1];
arr[j+1]=pos;
}
}
if(flag==0)
{ break;}
}
pos=search(0,n-1,el);
if(pos==-1)
{printf("\nElement does not exist");}
else
{printf("\nElement does exist");}
}
int search(int low,int high,int el)
{
int mid=(low+high)/2;
if(high<low)
{return -1;}
if(arr[mid]>el)
{return search(low,mid-1,el);}
else if(arr[mid]<el)
{return search(mid+1,high,el);}
else
{return mid;}
}

Output :
Enter the size of the array 5

Enter the array


10 40 30 50 20

1.BUBBLE_SORT
2.INSERTION_SORT
3.SELECTION_SORT
Enter your choice 3

10 20 30 40 50
Program ended with exit code : 0

//Q5. Sorting Algorithms : Bubble Sort, Selection Sort, Insertion Sort


// Date – 16/10/2018
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
void bubble_sort(int arr[],int n)
{
int flag,i,j;
for(i=0;i<n-1;i++)
{
flag=0;
for(j=0;j<n-1-i;j++)
{
if(arr[j]>arr[j+1])
{
flag=arr[j];
arr[j]=arr[j+1];
arr[j+1]=flag;
flag=1;
}
}
if(flag==0)
{ break;}
}
}
void insertion_sort(int arr[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=arr[i];
for(j=i-1;j>=0;j--)
{
if(arr[j]>temp)
{arr[j+1]=arr[j];}
else
{break;}
}
arr[++j]=temp;
}
}
void selection_sort(int arr[],int n)
{
int pos,small,i,j;
for(i=0;i<n-1;i++)
{
small=arr[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(small>arr[j])
{
pos=j;
small=arr[j];
}
}
arr[pos]=arr[i];
arr[i]=small;
}
}
int main()
{
int arr[50],n,opt;
printf("Enter the size of the array ");
scanf("%d",&n) ;
printf("\nEnter the array \n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]) ;
}
printf("1.BUBBLE_SORT\n2.INSERTION_SORT\n3.SELECTION_SORT\nEnter your choice
");
again:;
scanf("%d",&opt) ;
switch(opt)
{
case 1:bubble_sort(arr,n);
break;
case 2:insertion_sort(arr,n);
break;
case 3:selection_sort(arr,n);
break;
default :printf("\nNo such option,try giving the correct option");
goto again;
}
printf("\n") ;
int i ;
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);}}

Output :
Enter the size of the array 5

Enter the array


10 40 30 50 20

10 20 30 40 50
Program ended with exit code : 0

//Q5. Sorting Algorithms : Quick Sort


// Date – 16/10/2018
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int arr[50];
void quicksort(int,int);
int main()
{
int n;
printf("\nEnter the size of the array ");
scanf("%d",&n);
printf("\nEnter the array \n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]) ;
}
quicksort(0,n-1);
printf("\n") ;
int i ;
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
void quicksort(int lo,int hi)
{
if ( lo >= hi )
{
return ;
}

int left = lo ;
int right = hi ;
int mid = ( lo + hi ) / 2 ;
int pivot = arr[mid] ;

while ( left <= right )


{

while ( arr[left] < pivot )


{
left++ ;
}

while ( arr[right] > pivot )


{
right-- ;
}
if ( left <= right )
{
int t = arr[left] ;
arr[left] = arr[right] ;
arr[right] = t ;
left++ ;
right-- ;

quicksort(lo,right);
quicksort(left,hi);
}

Output :
Enter the size of the array 5

Enter the array


20 50 40 10 30

10 20 30 40 50
Program ended with exit code : 0

//Q5. Sorting Algorithms : Merge Sort


// Date – 16/10/2018
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

int arr[50];
void sort(int,int);
void merge(int,int,int);
int main()
{
int n;
printf("\nEnter the size of the array ");
scanf("%d",&n);
printf("\nEnter the array \n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]) ;
}
sort(0,n-1);
printf("\n");
int i ;
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
}
void sort(int low,int high)
{
int mid;
if(high>low)
{
mid=(high+low)/2;
sort(low,mid);
sort(mid+1,high);
merge(low,high,mid);
}
}
void merge(int low,int high,int mid)
{
int temp[50],i=low,j=mid+1,k=0;
while(i<=mid&&j<=high)
{
if(arr[i]<arr[j])
{
temp[k++]=arr[i++];
}
if(arr[i]>arr[j])
{
temp[k++]=arr[j++];
}
}
if(i<=mid)
{
while(i<=mid)
{temp[k++]=arr[i++];}
}
if(j<=high)
{
while(j<=high)
{temp[k++]=arr[j++];}
}
k=0;
for(i=low;i<=high;i++)
{
arr[i]=temp[k++];
}
}

You might also like