/* Programmer: Greg Perkins Program: matmul.c Class: CS 73 - C/UNIX Due: April 9, 1998 This program accepts the sizes of two matrices from the user, then dynamically allocates space for the matrices. It then checks the matrices for compatibility and multiplies them (if compatible) and displays the result in a third, dynamically allocated matrix. After declaring the sizes of the two initial matrices, the user must also enter the contents of each matrix. The current implementation covers only integer matrices. */ #include /********* FUNCTION PROTOTYPES **********/ void fill_mat(int **mat, int rows, int cols); void display_mat(int **mat, int rows, int cols); void matmul(int **mata, int **matb, int **matc, int x, int y, int z); main() { int counter; int p, q; /* intended (row, col) of first matrix */ int r, s; /* intended (row, col) of second matrix */ char response; /* user response to prompts - n or y */ char dummy; int is_valid = 0; /* used as loop control to ensure correct entries */ int compat; /* used as loop control to ensure matrix compatibility */ int **mat1; int **mat2; int **ans; printf("I will multiply two matrices that you enter and return the result.\n"); printf("First, I will need the intended sizes of the matrices. "); printf("You must\nspecify them in the correct order to ensure compatibility for multiplication.\n"); printf("The col size of the first matrix must equal the row size of the second matrix.\n\n"); do { printf("\nPlease enter the dimensions for:\n"); printf("MATRIX #1:\n"); printf(" # of rows : "); scanf("%d", &p); printf(" # of cols : "); scanf("%d", &q); printf("\nMATRIX #2:\n"); printf(" # of rows : "); scanf("%d", &r); printf(" # of cols : "); scanf("%d", &s); printf("MATRIX #1 is (%d X %d)\n", p, q); printf("MATRIX #2 is (%d X %d)\n", r, s); compat = (q == r); if (!compat) printf("Matrices are incompatible sizes - PLEASE RE-ENTER\n"); else { do { scanf("%c", &dummy); /* clear input buffer */ printf("Is this correct? (y or n)"); scanf("%c", &response); } while ((response != 'y') && (response != 'n')); is_valid = (response == 'y'); } } while (!(is_valid && compat)); mat1 = (int **) malloc(p * sizeof(int *)); for (counter = 0; counter < p; counter++) mat1[counter] = (int *) malloc(q * sizeof(int)); fill_mat(mat1, p, q); printf("\nMATRIX #1:\n"); display_mat(mat1, p, q); mat2 = (int **) malloc(r * sizeof(int *)); for (counter = 0; counter < r; counter++) mat2[counter] = (int *) malloc(s * sizeof(int)); fill_mat(mat2, r, s); printf("\nMATRIX #2:\n"); display_mat(mat2, r, s); ans = (int **) malloc(p * sizeof(int *)); for (counter = 0; counter < p; counter++) ans[counter] = (int *) malloc(s * sizeof(int)); matmul(mat1, mat2, ans, p, r, s); printf("\nPRODUCT MATRIX:\n"); display_mat(ans, p, s); } /********************* fill_mat *********************/ void fill_mat(int **mat, int rows, int cols) { int i, j; char dummy; printf("Enter matrix position values : (i.e. mat[1][1] = mat(row1,col1))\n"); for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) { scanf("%c", &dummy); printf("mat[%d][%d] : ", i+1, j+1); scanf("%d", &mat[i][j]); } } /******************** display_mat *****************************/ void display_mat(int **mat, int rows, int cols) { int i, j; for (i = 0; i < rows; i++) { printf("|"); for (j = 0; j < cols; j++) { printf(" %10d ", mat[i][j]); } printf(" |\n"); } printf("\n"); } /************************** matmul ******************************/ void matmul(int **mata, int **matb, int **matc, int x, int y, int z) { int i, j, k; int sum = 0; for (i = 0; i < x; i++) for (j = 0; j < z; j++) { for (k = 0; k < y; k++) sum += (mata[i][k] * matb[k][j]); matc[i][j] = sum; sum = 0; } }