Example(a): Program to Add Two Matrices
#include
int main(){
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf(" Enter elements of 1st matrix: ");
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("Enter elements of 2nd matrix: ");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &b[i][j]);
}
// Adding Two matrices
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the result
printf(" Sum of two matrices: ");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
printf(" ");
}
}
return 0;
}
Output
Enter number of rows (between 1 and 100): 2 Enter number of columns (between 1 and 100): 3 Enter elements of 1st matrix: Enter element a11: 5 Enter element a12: 3 Enter element a13: 4 Enter element a21: 1 Enter element a22: 2 Enter element a23: 3 Enter elements of 2nd matrix: Enter element a11: -4 Enter element a12: 6 Enter element a13: 3 Enter element a21: 5 Enter element a22: 6 Enter element a23: 3 Sum of two matrices: 1 9 7 6 8 6
Example{b}: Program to Multiply Two Matrices
#include
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second. ");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf(" Enter elements of matrix 1: ");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Storing elements of second matrix.
printf(" Enter elements of matrix 2: ");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
// Initializing all elements of result matrix to 0
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}
// Displaying the result
printf(" Output Matrix: ");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf(" ");
}
return 0;
}
Output
Enter rows and column for first matrix: 3 2 Enter rows and column for second matrix: 3 2 Error! column of first matrix not equal to row of second. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a11:2 Enter elements a12:1 Enter elements a13:2 Enter elements a21:3 Enter elements a22:1 Enter elements a23:4 Enter elements of matrix 2: Enter elements b11:2 Enter elements b12:-1 Enter elements b21:2 Enter elements b22:3 Enter elements b31:1 Enter elements b32:1 Output Matrix: 8 3 12 4Example{c} Program to Find Transpose of a Matrix
This program takes a matrix of order r*c from the user and computes the transpose of that matrix.To understand this example, you should have the knowledge of following C programming topics:
In this program, user is asked to entered the number of rows r and columns c. The value of r and cshould be less than 10 in this program.
The user is asked to enter elements of the matrix (of order r*c).
Then, the program computes the transpose of the matrix and displays it on the screen.
Example[d]: Program to Find Transpose of a Matrix
#include
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf(" Enter elements of matrix: ");
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]);
}
// Displaying the matrix a[][] */
printf(" Entered Matrix: ");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf(" ");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf(" Transpose of Matrix: ");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf(" ");
}
return 0;
}
Output
Enter rows and columns of matrix: 2 3 Enter element of matrix: Enter element a11: 5 Enter element a12: 6 Enter element a13: 8 Enter element a21: 3 Enter element a22: 9 Enter element a23: 1 Entered Matrix: 5 6 8 3 9 1 Transpose of Matrix: 5 3 6 9 8 1Example[E] Inverse Matrix of 3x3 in C
C code for inverse of 3×3matrix
- #include
- int main(){
- int a[3][3],i,j;
- float determinant=0;
- printf("Enter the 9 elements of matrix: ");
- for(i=0;i<3;i++)
- for(j=0;j<3;j++)
- scanf("%d",&a[i][j]);
- printf(" The matrix is ");
- for(i=0;i<3;i++){
- printf(" ");
- for(j=0;j<3;j++)
- printf("%d ",a[i][j]);
- }
- for(i=0;i<3;i++)
- determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
- printf(" Inverse of matrix is: ");
- for(i=0;i<3;i++){
- for(j=0;j<3;j++)
- printf("%.2f ",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
- printf(" ");
- }
- return 0;
- }
- Type this code on your computer and execute input and output. Best of luck
Program to Find the Trace & Normal of a given Matrix
This is a program to find the trace & normal of a given matrix.
Problem DescriptionThis C Program find the trace & normal of a given matrix. Here trace of the matrix is the sum of the elements of the main diagonal i.e the diagonal from the upper left to the lower right of a matrix. Normal of the matrix is the square root of the sum of all the elements.
Problem Solution1. Create a matrix and define all its elements.
2. To evaluate normal of the matrix, take sum of all the elements of the array and calculate the square root of it.
3. To evaluate trace of the matrix, take sum of the main diagonal elements.Program/Source CodeHere is source code of the C program to find the trace & normal of a given matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C program to find the trace and normal of a matrix
*
* Trace is defined as the sum of main diagonal elements and
* Normal is defined as square root of the sum of all the elements
*/
#include
#include
void main (){
static int array[10][10]; int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal; printf("Enter the order of the matrix "); scanf("%d %d", &m, &n); printf("Enter the n coefficients of the matrix "); for (i = 0; i < m; ++i){
for (j = 0; j < n; ++j){
scanf("%d", &array[i][j]); a = array[i][j] * array[i][j]; sum1 = sum1 + a;}
}
normal = sqrt(sum1); printf("The normal of the given matrix is = %d ", normal); for (i = 0; i < m; ++i){
sum = sum + array[i][i];}
printf("Trace of the matrix is = %d ", sum);}
Program Explanation1. Declare a matrix and define all its elements.
2. Declare variables for storing the normal and trace of the matrix.
3. Find the sum of all the elements of the matrix using nested for loop.
4. Evaluate normal by passing the above calculated sum to sqrt() function.
5. Take sum of all the main diagonal elements of the array to calculate trace.
6. Print trace and normmalRuntime Test Cases
Enter the order of the matrix 3 3 Enter the coefficients of the matrix 3 7 9 2 6 10 8 5 9 The normal of the given matrix is = 21 Trace of the matrix is = 18
good
ReplyDelete🤟🤟🙏🙏
ReplyDelete🙏🙏
Delete😍😍😍
ReplyDeletethrilling
ReplyDeleteNishant sir namaste
ReplyDelete