fork download
  1. #include <stdio.h>
  2.  
  3. #define MAX_SIZE 10
  4.  
  5. // Function to multiply two matrices
  6. void matrixMultiply(int mat1[MAX_SIZE][MAX_SIZE], int mat2[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows1, int cols1, int cols2) {
  7. int i, j, k;
  8.  
  9. // Perform matrix multiplication
  10. for (i = 0; i < rows1; i++) {
  11. for (j = 0; j < cols2; j++) {
  12. result[i][j] = 0;
  13. for (k = 0; k < cols1; k++) {
  14. result[i][j] += mat1[i][k] * mat2[k][j];
  15. }
  16. }
  17. } // Syntax Error: Missing closing curly brace for matrixMultiply function
  18.  
  19. // Missing semicolon on the next line, causing a syntax error
  20. void displayMatrix(int mat[MAX_SIZE][MAX_SIZE], int rows, int cols) {
  21. int i, j
  22. for (i = 0; i < rows; i++) {
  23. for (j = 0; j < cols; j++) {
  24. printf("%d\t", mat[i][j]);
  25. }
  26. printf("\n");
  27. }
  28. }
  29.  
  30. int main() {
  31. int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
  32. int rows1, cols1, rows2, cols2, i, j;
  33.  
  34. printf("Enter the number of rows and columns of the first matrix: ");
  35. scanf("%d %d", &rows1, &cols1);
  36.  
  37. printf("Enter the elements of the first matrix:\n");
  38. for (i = 0; i < rows1; i++) {
  39. for (j = 0; j < cols1; j++) {
  40. scanf("%d", &mat1[i][j]);
  41. }
  42. }
  43.  
  44. printf("Enter the number of rows and columns of the second matrix: ";
  45. scanf("%d %d", &rows2, &cols2); // Syntax Error: Missing closing parenthesis
  46.  
  47. if (cols1 != rows2) {
  48. printf("Error: Matrix multiplication is not possible. Columns of first matrix must be equal to rows of second matrix.\n");
  49. return 1;
  50. }
  51.  
  52. printf("Enter the elements of the second matrix:\n");
  53. for (i = 0; i < rows2; i++) {
  54. for (j = 0; j < cols2; j++) {
  55. scanf("%d", &mat2[i][j]);
  56. }
  57. }
  58.  
  59. matrixMultiply(mat1, mat2, result, rows1, cols1, cols2);
  60.  
  61. printf("Resultant matrix after multiplication:\n");
  62. displayMatrix(result, rows1, cols2);
  63.  
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0.03s 26060KB
stdin
Standard input is empty
stdout
#include <stdio.h>

#define MAX_SIZE 10

// Function to multiply two matrices
void matrixMultiply(int mat1[MAX_SIZE][MAX_SIZE], int mat2[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows1, int cols1, int cols2) {
    int i, j, k;

    // Perform matrix multiplication
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < cols2; j++) {
            result[i][j] = 0;
            for (k = 0; k < cols1; k++) {
                result[i][j] += mat1[i][k] * mat2[k][j];
            }
        }
    } // Syntax Error: Missing closing curly brace for matrixMultiply function

// Missing semicolon on the next line, causing a syntax error
void displayMatrix(int mat[MAX_SIZE][MAX_SIZE], int rows, int cols) {
    int i, j
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("%d\t", mat[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
    int rows1, cols1, rows2, cols2, i, j;

    printf("Enter the number of rows and columns of the first matrix: ");
    scanf("%d %d", &rows1, &cols1);

    printf("Enter the elements of the first matrix:\n");
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < cols1; j++) {
            scanf("%d", &mat1[i][j]);
        }
    }

    printf("Enter the number of rows and columns of the second matrix: ";
    scanf("%d %d", &rows2, &cols2); // Syntax Error: Missing closing parenthesis

    if (cols1 != rows2) {
        printf("Error: Matrix multiplication is not possible. Columns of first matrix must be equal to rows of second matrix.\n");
        return 1;
    }

    printf("Enter the elements of the second matrix:\n");
    for (i = 0; i < rows2; i++) {
        for (j = 0; j < cols2; j++) {
            scanf("%d", &mat2[i][j]);
        }
    }

    matrixMultiply(mat1, mat2, result, rows1, cols1, cols2);

    printf("Resultant matrix after multiplication:\n");
    displayMatrix(result, rows1, cols2);

    return 0;
}