Matrix Calculator

Lab C Advanced 30 points

Read two 3×3 matrices A and B, then commands: add, mul, det (determinant of A). Print each result (matrices row by row).

Input

A's 3 rows, B's 3 rows, k, then k commands.

Output

The results.

Example

Input

1 2 3
4 5 6
7 8 10
1 0 0
0 1 0
0 0 1
3
add
mul
det

Output

2 2 3
4 6 6
7 8 11
1 2 3
4 5 6
7 8 10
-3
Sample input available Sample output available
Editor
C

Hints

Global matrices avoid passing 2D arrays to functions.

The 3x3 determinant expands along the first row.

Solution explanation

A small command loop turns individual operations into a calculator.

Show solution
#include <stdio.h>
#include <string.h>

int A[3][3], B[3][3];

void print_cell_row(int *row) {
    printf("%d %d %d\n", row[0], row[1], row[2]);
}

int main() {
    int k, r[3];
    char cmd[10];
    for (int i = 0; i < 3; i++) scanf("%d %d %d", &A[i][0], &A[i][1], &A[i][2]);
    for (int i = 0; i < 3; i++) scanf("%d %d %d", &B[i][0], &B[i][1], &B[i][2]);
    scanf("%d", &k);
    for (int t = 0; t < k; t++) {
        scanf("%s", cmd);
        if (strcmp(cmd, "det") == 0) {
            int d = A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1])
                  - A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0])
                  + A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]);
            printf("%d\n", d);
            continue;
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (strcmp(cmd, "add") == 0) {
                    r[j] = A[i][j] + B[i][j];
                } else {
                    r[j] = 0;
                    for (int m = 0; m < 3; m++) r[j] += A[i][m] * B[m][j];
                }
            }
            print_cell_row(r);
        }
    }
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD