Write C Program To Read Two R*C Matrices A And B From User (R, C Are User Inputs)

Statement: Write C program to read two r*c matrices A and B from user (r, c are user inputs) and print the matrix 5A+7B+9 (add 9 with each element of the matrix 5A+7B to get the resultant matrix).

Solution: 

Code Part:

#include<stdio.h>

int main() {
  int r, c, i, j;

  // taking user input for r and c
  printf("Enter no. of rows = ");
  scanf("%d", & r);
  printf("\nEnter no. of cols = ");
  scanf("%d", & c);

  int matrixA[r][c], matrixB[r][c], matrixsum[r][c];

  // taking user input for matrixA
  printf("\nEnter values to the matrix A = \n");
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      scanf("%d", & matrixA[i][j]);
    }
  }

  // taking user input for matrixB
  printf("\nEnter values to the matrix B = \n");
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      scanf("%d", & matrixB[i][j]);
    }
  }

  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      matrixsum[i][j] = (5 * matrixA[i][j]) + (7 * matrixB[i][j]) + 9;
    }
  }

  printf("\nValues of the matrix 5A+7B+9 = \n");
  for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
      printf("%d\t", matrixsum[i][j]);
    }
    printf("\n");
  }

  return 0;
}

Output: 



إرسال تعليق

أحدث أقدم