0% found this document useful (0 votes)
40 views2 pages

Simple Matrix Programs With Questions

The document contains a question bank for C programming with three questions related to matrix operations. It includes code examples for printing the transpose of a matrix, summing the diagonal elements of a 4x4 matrix, and performing matrix multiplication with a check for compatibility. Each question is accompanied by a complete C program solution.

Uploaded by

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

Simple Matrix Programs With Questions

The document contains a question bank for C programming with three questions related to matrix operations. It includes code examples for printing the transpose of a matrix, summing the diagonal elements of a 4x4 matrix, and performing matrix multiplication with a check for compatibility. Each question is accompanied by a complete C program solution.

Uploaded by

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

C Programming Question Bank with Answers

Q1: Write a program to print transpose of a given Matrix (Take elements from user).

#include <stdio.h>

int main() {
int mat[10][10], trans[10][10];
int i, j, r, c;

printf("Enter rows and columns: ");


scanf("%d %d", &r, &c);

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


for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &mat[i][j]);

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


for(j = 0; j < c; j++)
trans[j][i] = mat[i][j];

printf("Transpose:\n");
for(i = 0; i < c; i++) {
for(j = 0; j < r; j++)
printf("%d ", trans[i][j]);
printf("\n");
}
return 0;
}

Q2: Write a program to sum up all the elements of diagonal of 4x4 Matrix.

#include <stdio.h>

int main() {
int mat[4][4], i, j, sum = 0;

printf("Enter 4x4 matrix elements:\n");


for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
scanf("%d", &mat[i][j]);

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


sum += mat[i][i];

printf("Diagonal sum = %d\n", sum);

return 0;
C Programming Question Bank with Answers

Q3: Write a program Matrix Multiplication. Check if Multiplication is Possible.

#include <stdio.h>

int main() {
int a[10][10], b[10][10], result[10][10];
int i, j, k, r1, c1, r2, c2;

printf("Enter rows and cols of Matrix A: ");


scanf("%d %d", &r1, &c1);

printf("Enter rows and cols of Matrix B: ");


scanf("%d %d", &r2, &c2);

if(c1 != r2) {
printf("Multiplication not possible.\n");
return 0;
}

printf("Enter elements of Matrix A:\n");


for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &a[i][j]);

printf("Enter elements of Matrix B:\n");


for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &b[i][j]);

for(i = 0; i < r1; i++) {


for(j = 0; j < c2; j++) {
result[i][j] = 0;
for(k = 0; k < c1; k++)
result[i][j] += a[i][k] * b[k][j];
}
}

printf("Result Matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++)
printf("%d ", result[i][j]);
printf("\n");
}

return 0;
}

You might also like