0% found this document useful (0 votes)
90 views10 pages

55practice Assignment 5

The document contains 10 programming problems related to matrix operations and sorting algorithms in C language. The problems include programs to perform matrix multiplication, addition, subtraction, transpose, check if two matrices are identical, and display the diagonal, lower triangular, and upper triangular elements of a matrix. Sorting algorithms programs include insertion sort and selection sort of an array. For each problem, the full C code solution is provided.

Uploaded by

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

55practice Assignment 5

The document contains 10 programming problems related to matrix operations and sorting algorithms in C language. The problems include programs to perform matrix multiplication, addition, subtraction, transpose, check if two matrices are identical, and display the diagonal, lower triangular, and upper triangular elements of a matrix. Sorting algorithms programs include insertion sort and selection sort of an array. For each problem, the full C code solution is provided.

Uploaded by

Nitesh Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 10

PRACTICE ASSIGNMENT 5

Name: Nitesh Kumar Yadav


Reg. No.: 20195103
Group: B1

1. Write a program in C to perform Matrix Multiplication.

#include<stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

for ( c = 0 ; c < p ; c++ )


for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
PRACTICE ASSIGNMENT 5
}
}

printf("Product of entered matrices:-\n");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

2. Write a program in C to perform the Addition of two matrices.

#include<stdio.h>
#include<conio.h>
int main()
{
int a[2][3],b[2][3],c[2][3],i,j;
clrscr();
printf("\nENTER VALUES FOR MATRIX A:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR MATRIX B:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\nTHE VALUES OF MATRIX C ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("\n");
}
getch();
}
PRACTICE ASSIGNMENT 5
3. Write a program in C to perform and display the Transpose of a matrix.

#include<stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

printf("\nEnter matrix elements:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("\nEntered matrix: \n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

for (i = 0; i < r; ++i)


for (j = 0; j < c; ++j)
{
transpose[j][i] = a[i][j];
}

printf("\nTranspose of the matrix:\n");


for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j)
{
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
PRACTICE ASSIGNMENT 5
4. Write a program in C to perform and display the Subtraction of two matrices.

#include<stdio.h>

int main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n;

printf("\n\nSubtraction of two Matrices :\n");


printf("Input the size of the square matrix (less than 5): ");
scanf("%d", &n);

printf("Input elements in the first matrix :\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}

printf("Input elements in the second matrix :\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",arr1[i][j]);
}

printf("\nThe Second matrix is :\n");


for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",brr1[i][j]);
PRACTICE ASSIGNMENT 5
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
crr1[i][j]=arr1[i][j]-brr1[i][j];
printf("\nThe Subtraction of two matrix is : \n");
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",crr1[i][j]);
}
printf("\n\n");
}

5. Write a program in C to display the diagonal elements of a matrix.

#include<stdio.h>

int main()
{
int array1[10][10],i,j,m,n,sum = 0;

printf("Enter no. of rows :: ");


scanf("%d", &m);
printf("\nEnter no. of cols :: ");
scanf("%d",&n);
printf("\nEnter values to the matrix :: \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("\nEnter a[%d][%d] value :: ",i,j);
scanf("%d", &array1[i][j]);
}
}

printf("\nThe Diagonals elements of a matrix are :: \n\n");

if(m==n)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
PRACTICE ASSIGNMENT 5
if(i==j)
printf("\t%d", array1[i][j]);
else
printf("\t");
}
printf("\n\n");
}
}
else
{
printf("\nMatrix is not a Square Matrix.");
}

return 0;
}

6. Write a program in C to check if two matrices are identical or not.

#include<stdio.h>

int main()
{
int i, j, rows, columns, a[10][10], b[10][10], isEqual;

printf("\n Please Enter Number of rows and columns : ");


scanf("%d %d", &i, &j);

printf("\n Please Enter the First Matrix Elements\n");


for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}

printf("\n Please Enter the Second Matrix Elements\n");


for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &b[rows][columns]);
}
}
PRACTICE ASSIGNMENT 5
isEqual = 1;

for(rows = 0; rows < i; rows++)


{
for(columns = 0;columns < j;columns++)
{
if(a[rows][columns] != b[rows][columns])
{
isEqual = 0;
break;
}
}
}
if(isEqual == 1)
{
printf("\n Matrix a is Equal to Matrix b");
}
else
{
printf("\n Matrix a is Not Equal to Matrix b");
}
return 0;
}

7. Write a program in C to display the lower triangular matrix of an array.

#include<stdio.h>

int main()
{
int arr1[10][10],i,j,n;
float determinant=0;

printf("Input the size of the square matrix : ");


scanf("%d", &n);
printf("Input elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
PRACTICE ASSIGNMENT 5
printf("The matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n ;j++)
printf("% 4d",arr1[i][j]);
printf("\n");
}

printf("\nSetting zero in lower triangular matrix\n");


for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++)
if(i<=j)
printf("% 4d",arr1[i][j]);
else
printf("% 4d",0);
}
printf("\n\n");
}

8. Write a program in C to display the upper triangular matrix of an array.

#include<stdio.h>

int main()
{
int arr1[10][10],i,j,n;
float determinant=0;

printf("Input the size of the square matrix : ");


scanf("%d", &n);
printf("Input elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("The matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n ;j++)
printf("% 4d",arr1[i][j]);
PRACTICE ASSIGNMENT 5
printf("\n");
}

printf("\nSetting zero in upper triangular matrix\n");


for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
if(i>=j)
printf("% 4d",arr1[i][j]);
else
printf("% 4d",0);
}
printf("\n\n");
}

9. Write a program in C to sort an array using insertion sort.

#include<stdio.h>

int main()
{
int n, array[1000], c, d, t, flag = 0;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 1 ; c <= n - 1; c++) {


t = array[c];

for (d = c - 1 ; d >= 0; d--) {


if (array[d] > t) {
array[d+1] = array[d];
flag = 1;
}
else
break;
}
if (flag)
array[d+1] = t;
PRACTICE ASSIGNMENT 5
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0;
}

10. Write a program in C to sort an array using selection sort.

#include<stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy