행렬 더하기, 빼기 및 곱하기를 수행하는 프로그램을 만들었습니다. 더하기와 빼기 부분을 처리했지만 곱셈에 도달하면 올바른 값을 출력하는 데 문제가 있습니다. 아래의 곱하기 함수를 사용하여 코드를 배치했습니다. 곱셈 기능에 행렬 곱셈 - C
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *elements;
int rows;
int columns;
} matrix;
void main() {
matrix a, b, c;
void read_matrix(matrix *);
void deallocate(matrix *);
void print(matrix);
matrix add(matrix, matrix);
matrix subtract(matrix, matrix);
matrix multiply(matrix, matrix);
read_matrix(&a);
read_matrix(&b);
/*
c = add(a, b);
printf("The answer of Matrix (a + b) is \n\n");
print(a);
printf("\n +\n\n");
print(b);
printf("\n =\n\n");
print(c);
printf("\n");
deallocate(&c);
c = subtract(a, b);
printf("The answer of Matrix (a - b) is \n\n");
print(a);
printf("\n -\n\n");
print(b);
printf("\n =\n\n");
print(c);
printf("\n");
deallocate(&c);
*/
c = multiply(a, b);
printf("The answer of Matrix (a * b) is \n\n");
print(a);
printf("\n *\n\n");
print(b);
printf("\n =\n\n");
print(c);
printf("\n");
}
void read_matrix(matrix *z) {
int d1, d2, allc, i, x, y, j, val;
int res;
printf("\nWhat is the first dimension of the array? ");
res = scanf("%d", &d1);
if (res != 1) {
fprintf(stderr, "Something went wrong with your first dimension!");
return;
}
printf("What is the second dimension of the array? ");
res = scanf("%d", &d2);
if (res != 1) {
fprintf(stderr, "Something went wrong with your second dimension!");
return;
}
printf("Matrix Dimension is %dx%d\n", d1, d2);
allc = d1*d2;
(*z).elements = (int *)calloc(allc, sizeof(int));
(*z).rows = d1;
(*z).columns = d2;
x = 0;
j = 0;
printf("\n");
for (i = 0; i < d1; i++) {
x++;
for (y = 0; y < d2; y++) {
printf("Enter the value for row %d column %d: ", x, y + 1);
res = scanf("%d", &val);
if (res != 1) {
fprintf(stderr, "Something went wrong while reading value %d\n", x);
return;
}
(*z).elements[j++] = val;
}
}
}
void deallocate(matrix *c) {
free((*c).elements);
(*c).elements = NULL;
(*c).rows = 0;
(*c).columns = 0;
}
void print(matrix z) {
int i, j, x;
x = 0;
for (i = 0; i < z.rows; i++) {
printf("[ ");
for (j = 0; j < z.columns; j++) {
printf("%-4d", z.elements[x++]);
}
printf("]\n");
}
}
matrix multiply(matrix a, matrix b) {
matrix c;
int a1, a2, b1, b2, allc, i, j, x, y, z, alc, addval, val;
i = 0;
j = 0;
alc = 0;
val = 0;
addval = 0;
a1 = a.rows;
a2 = a.columns;
b1 = b.rows;
b2 = b.columns;
allc = (a1 * b2);
c.elements = (int *)calloc(allc, sizeof(int));
c.columns = a1;
c.rows = b2;
if (a2 != b1) {
printf("\n\nThe inner dimensions of your matrices do not match! Multiplication cannot be done!\n\n");
exit(1);
}
for (x = 0; x < c.rows; x++) {
for (y = 0; y < c.rows; y++) {
for (z = 0; z < c.rows; z++) {
i = (i * c.rows);
addval = (a.elements[j]) * (b.elements[i]);
val += addval;
j++;
i++;
}
c.elements[alc] = val;
printf("VAL IS: %d\n\n", val);
val = 0;
i = 0;
alc++;
}
}
printf("\n\n");
return c;
}
은, 루프 트리플 중첩은 새로운 배열의 차원에 대한 항목의 정확한 숫자를 인쇄하기에 충분한 시간을 통해 갈 예정이다. 행렬 곱셈을 수행하는 방법을 알고 있지만 올바르게 표현했는지 확신 할 수 없습니다.
예제의 출력은 다음과 같습니다
What is the first dimension of the array? 3
What is the second dimension of the array? 3
Matrix Dimension is 3x3
Enter the value for row 1 column 1: 1
Enter the value for row 1 column 2: 2
Enter the value for row 1 column 3: 3
Enter the value for row 2 column 1: 4
Enter the value for row 2 column 2: 5
Enter the value for row 2 column 3: 6
Enter the value for row 3 column 1: 7
Enter the value for row 3 column 2: 8
Enter the value for row 3 column 3: 9
What is the first dimension of the array? 3
What is the second dimension of the array? 3
Matrix Dimension is 3x3
Enter the value for row 1 column 1: 1
Enter the value for row 1 column 2: 2
Enter the value for row 1 column 3: 3
Enter the value for row 2 column 1: 4
Enter the value for row 2 column 2: 5
Enter the value for row 2 column 3: 6
Enter the value for row 3 column 1: 7
Enter the value for row 3 column 2: 8
Enter the value for row 3 column 3: 9
The answer of Matrix (a * b) is
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
*
[ 1 2 3 ]
[ 4 5 6 ]
[ 7 8 9 ]
=
[ 216847534336951265054271]
[ 1641572693-138635672036124672]
[ 1352368309-50514195286739134]
Press any key to continue . . .
x, y 및 z는 모두 카운팅 행입니다. 적어도 그 중 하나는 열을 계산해야합니다. 그리고'i'는'c.rows'를 곱한 다음 루프를 통과 할 때마다 증가합니다. 'printf'를 사용하여'i'의 값을 출력하면 원하는 것이 전혀 없음을 알 수 있습니다. – user3386109
주제 끄기 :'print' 함수에서''% -4d "'를''% -3d"'로 바꿀 것입니다. 그것은 어떤 사람들이 3 자리 이상일 때 함께 묶어 버리는 것을 막을 것입니다. – user3386109