Ошибка в расчете результирующей матрицы на языке программирования C - PullRequest
0 голосов
/ 04 апреля 2020

Я пытаюсь написать программу, в которой у меня есть две матрицы, и я умножаю две матрицы и сохраняю ее в результирующей матрице с именем «carr». По какой-то странной причине умножение матриц не выполняется должным образом. Пытался найти проблему довольно долго, но не смог найти ошибку. Кто-нибудь может помочь? TIA для вашего времени! Вот сс проблемы: https://snipboard.io/s9ifP4.jpg

#include <stdio.h>
int main()
{

int row1, column1, row2, column2,i,j,k, sum=0;

//START OF THE 1ST ARRAY//
printf("How many rows do you want for the first matrix? Ans: ");
scanf("%d", &row1);

printf("How many columns do you want for the first matrix? Ans: ");
scanf("%d", &column1);
int arr[row1][column1];

printf("Enter the elements of the first array:\n");
for(i = 0; i <row1; i++){
  for(j=0; j < column1; j++){

    scanf("%d", &arr[i][j]);
  }
}
printf("\n----------------------------------------\n");
printf("The elements of the first array are:\n");
for(i = 0; i <row1; i++){
  printf("[ ");
  for(j=0; j < column1; j++){
    printf("%d, ", arr[i][j]);
  }
  printf("]\n");
}
//END OF THE FIRST ARRAY//
printf("----------------------------------------\n");


//START OF THE 2ND ARRAY//
printf("\n**How many rows do you want for the second matrix?\n\nAlert: For matrix multiplication, the COLUMN of the 1st matrix MUST equal to the ROW of the 2nd matrix.\nAns: ");
scanf("%d", &row2);

printf("How many columns do you want for the second matrix? Ans: ");
scanf("%d", &column2);
int barr[row2][column2];

printf("Enter the elements of the second array:\n");
for(i = 0; i <row2; i++){
  for(j=0; j < column2; j++){

    scanf("%d", &arr[i][j]);
  }
}
printf("\n----------------------------------------\n");
printf("The elements of the second array are:\n");
for(i = 0; i <row2; i++){
  printf("[ ");
  for(j=0; j < column2; j++){
    printf("%d, ", arr[i][j]);
  }
  printf("]\n");
}
printf("----------------------------------------\n");
//END OF THE 2ND ARRAY//



//Everything above this part is okay. The problem starts from the Matrix multiplication part//

//MATRIX MULTIPLICATION//
//The resultant matrix where the values of the multiplied matrix is being held has row = ROW1 and column = COLUMN2.//
int carr[row1][column2];

if(column1 == row2)
{
for(i = 0; i < row1; i++){
  for(j=0; j < column2; j++){
    for(k=0; k < row2; k++){
      sum = sum + arr[i][k] * barr[k][j];
    }
    carr[i][j] = sum;
    sum=0;
  }
}
}

else
{
printf("Matrix multiplication is not possible");
}


printf("\n----------------------------------------\n");
printf("The elements of the resultant array are:\n");
for(i = 0; i <row1; i++){
  printf("[ ");
  for(j=0; j < column2; j++){
    printf("%d, ", carr[i][j]);
  }
  printf("]\n");
}
printf("----------------------------------------\n");

  return 0;
}

1 Ответ

0 голосов
/ 04 апреля 2020

Изменение arr на barr устраняет проблему. Спасибо @M Oehm за указание на ошибку.

...