The array is a versatile data structure. can be used to represent multi dimensio
ID: 3732677 • Letter: T
Question
The array is a versatile data structure. can be used to represent multi dimensional data such as matrices. A matrix is a rectangular structure with M rows and N columns. For this exercise, you may assume that M 50 and N 50. Given specific values of M and N, the C declaration "int myMatrix[MIIN] will create the matrix with M x N elements of type int. You can, of course declare matrices to hold float, double or char data types in a similar manner. To access the element in the h row and fh column of myMatrix, you need to acces:s the variable as myMatrix[)0]. Not all matrices you will be given will be square The first two integers your programs read in will provide you the values for M and N, in this order 1. Write a C program that will accept M x N elements of a matrix of type int. The first two integers will specify M and N. The rest of the input will provide values for the M x N elements, row by row. Print out the matrix row by row. Make sure to include a next line character only after the entire row is printed. This is called row major ordering of the matrix. 2. Write a C program that will accept M x N elements of a matrix of type int. The first two integers will specify Mand N. The rest of the input will provide values for the M x N elements, row by row. Print out the matrix column by column. Make sure to include a next line character only after the entire column is printed. This is called column major ordering of the matrix 3. Write a C program that will accept M x N elements of a matrix of type int. The first two integers will specify M and N. The rest of the input will provide values for the M x N elements in row major ordering. Let us call this matrix that you just accepted as matrix A. Compute the resulting matrix B A, where AT the transpose of matrix A. Print the matrix B in row major ordering 4. Write a C program that will accept two M x N matrices A and B, both in row major ordering as in the previous problems. For each matrix, the input will provide the values for M and N followed by the elements. Now, your program must accept two integer scalar values, p and q. Compute the matrix C A pA qB. Print out the matrix C in row major ordering 5. Write a C program that will accept two M x N matrices A and B, both in row major ordering as in the previous problems. For each matrix, the input wil provide the values for M and N followed by the elements. Now, your program must check if the two input matrices are compatible for multiplication. If they are compatible, then compute CAB - BA. Print out the matrix C in row major orderingExplanation / Answer
Part1 :-
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf(" ---- matrix program ----");
int m=0,n=0;
printf(" Enter the Value of M :- ");
scanf("%d",&m);
printf(" Enter the Value of N :-");
scanf("%d",&n);
printf("Enter the Matrix value ");
int i,j;
int matrix[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&matrix[i][j]);
}
}
//print matrix row by row
printf("Matrix row by row is:- ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",matrix[i][j]);
}
printf(" ");
}
return 0;
}
------
output sample:-
---- matrix program ----
Enter the Value of M :- 3
Enter the Value of N :-4
Enter the Matrix value
3
4
5
6
7
8
1
2
5
3
5
6
Matrix row by row is:-
3 4 5 6
7 8 1 2
5 3 5 6
-------------------------------------------------------------------------------
part 2:-
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf(" ---- matrix program ----");
int m=0,n=0;
printf(" Enter the Value of M :- ");
scanf("%d",&m);
printf(" Enter the Value of N :-");
scanf("%d",&n);
printf("Enter the Matrix value ");
int i,j;
int matrix[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&matrix[i][j]);
}
}
//print matrix col by col
printf("Matrix col by col is:- ");
for(i=0;i < n ;i++)
{
for(j=0 ; j < m ; j++)
{
printf("%d ",matrix[j][i]);
}
printf(" ");
}
return 0;
}
----------------
output sample :-
---- matrix program ----
Enter the Value of M :- 2
Enter the Value of N :-4
Enter the Matrix value
1
4
5
6
8
9
3
4
Matrix col by col is:-
1 8
4 9
5 3
6 4
--------------------------------------------------------------
part 3:-
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf(" ---- matrix program ----");
int m=0,n=0;
printf(" Enter the Value of M :- ");
scanf("%d",&m);
printf(" Enter the Value of N :-");
scanf("%d",&n);
printf("Enter the Matrix value ");
int i,j;
int matrix[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&matrix[i][j]);
}
}
//print matrix row by row
printf("Given matrix is :- ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",matrix[i][j]);
}
printf(" ");
}
printf("Transpose of matrix is ");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
printf(" %d", matrix[i][j]);
}
printf(" ");
}
return 0;
}
-----------
output sample:-
---- matrix program ----
Enter the Value of M :- 3
Enter the Value of N :-4
Enter the Matrix value
3
4
6
7
9
7
4
3
2
4
6
7
Given matrix is :-
3 4 6 7
9 7 4 3
2 4 6 7
Transpose of matrix is
3 9 2
4 7 4
6 4 6
7 3 7
------------------------------------------------------------------------------------------------
part 4:-
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf(" ---- matrix program ----");
int m=0,n=0;
printf(" Enter the Value of M :- ");
scanf("%d",&m);
printf(" Enter the Value of N :-");
scanf("%d",&n);
int i,j;
int matrix_A[m][n];
int matrix_B[m][n];
int matrix_C[m][n];
printf("Enter Matrix A ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&matrix_A[i][j]);
}
}
printf("Enter Matrix B ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&matrix_B[i][j]);
}
}
int p,q;
printf(" Enter value of p:- ");
scanf("%d",&p);
printf(" Enter value of q:- ");
scanf("%d",&q);
printf(" Matrix C = A - pA + qB ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
matrix_C[i][j] = matrix_A[i][j] - p*matrix_A[i][j] + q*matrix_B[i][j];
}
}
printf("Matrix C is :- ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ", matrix_C[i][j]);
}
printf(" ");
}
return 0;
}
----
output sample :-
---- matrix program ----
Enter the Value of M :- 2
Enter the Value of N :-4
Enter Matrix A 3
4
5
6
7
8
1
2
Enter Matrix B 4
6
5
4
6
4
6
7
Enter value of p:- 2
Enter value of q:- 4
Matrix C = A - pA + qB
Matrix C is :-
13 20 15 10
17 8 23 26
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.