0 votes
336 views
in C programming by (98.9k points)
edited
Write a menu driven program and perform following operations on matrices such as transpose of matrix, addition of matrix, subtraction of matrix using function.

1 Answer

0 votes
by (98.9k points)
selected by
 
Best answer
#include <stdio.h>

#define MAX_SIZE 10

void read_matrix(int matrix[][MAX_SIZE], int rows, int cols) {
    printf("Enter matrix elements:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

void print_matrix(int matrix[][MAX_SIZE], int rows, int cols) {
    printf("Matrix:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

void transpose_matrix(int matrix[][MAX_SIZE], int rows, int cols) {
    int transposed[MAX_SIZE][MAX_SIZE];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            transposed[j][i] = matrix[i][j];
        }
    }
    printf("Transposed matrix:\n");
    print_matrix(transposed, cols, rows);
}

void add_matrices(int matrix1[][MAX_SIZE], int matrix2[][MAX_SIZE], int rows, int cols) {
    int result[MAX_SIZE][MAX_SIZE];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    printf("Sum of matrices:\n");
    print_matrix(result, rows, cols);
}

void subtract_matrices(int matrix1[][MAX_SIZE], int matrix2[][MAX_SIZE], int rows, int cols) {
    int result[MAX_SIZE][MAX_SIZE];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }
    printf("Difference of matrices:\n");
    print_matrix(result, rows, cols);
}

int main() {
    int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE];
    int rows1, cols1, rows2, cols2;
    int choice;

    printf("Enter dimensions of first matrix (rows columns): ");
    scanf("%d %d", &rows1, &cols1);
    read_matrix(matrix1, rows1, cols1);

    printf("Enter dimensions of second matrix (rows columns): ");
    scanf("%d %d", &rows2, &cols2);
    read_matrix(matrix2, rows2, cols2);

    printf("Select an operation:\n");
    printf("1. Transpose of matrix\n");
    printf("2. Addition of matrices\n");
    printf("3. Subtraction of matrices\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            transpose_matrix(matrix1, rows1, cols1);
            break;
        case 2:
            if (rows1 != rows2 || cols1 != cols2) {
                printf("Error: matrices have different dimensions\n");
                return 1;
            }
            add_matrices(matrix1, matrix2, rows1, cols1);
            break;
        case 3:
            if (rows1 != rows2 || cols1 != cols2) {
printf("Error: matrices have different dimensions\n");
return 1;
}
subtract_matrices(matrix1, matrix2, rows1, cols1);
break;
default:
printf("Invalid choice\n");
return 1;
}





return 0; 


}

Related questions

0 votes
1 answer 124 views
0 votes
1 answer 99 views
asked Jun 10, 2022 in C programming by Doubtly (98.9k points)
0 votes
1 answer 191 views
asked Apr 23, 2022 in C programming by Doubtly (98.9k points)
0 votes
1 answer 418 views
0 votes
1 answer 184 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

554 users

...