为什么我的矩阵乘法代码会产生错误的答案?
Why is my matrix multiplying code producing wrong answer?
我写的这个矩阵乘法程序有时会写错答案。它正确地使用大小为 2x3 3x3 的矩阵或至少使用数字我试过它是正确的但是如果我放入大小为 2x3 3x2 的矩阵并用数字 [-1 2 1][2 0 4] 和 [2 -3] 填充它们[1 4][-2 3] 程序输出[0 11][4 -6],正确的结果应该是[-2 14][-4 6]。代码或指针中是否存在错误?我只是忽略了公式中的一些错误吗?我不知道。
程序的完整代码:
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
void Scanner(int *row1, int *col1, int *row2, int *col2){
printf("\nNumber of rows for first matrix : ");
scanf("%d", row1);
printf("\nNumber of columns for first matrix : ");
scanf("%d", col1);
printf("\nNumber of rows for second matrix : ");
scanf("%d", row2);
printf("\nNumber of columns for second matrix. : ");
scanf("%d", col2);
if(*col1 != *row2)
{
printf("\nMatrixes cant be multiplyed.");
Scanner( &row1, &col1, &row2, &col2 );
}
if(*col1 <= 0 || *row1 <= 0 || *col2 <= 0 || *row2 <= 0){
printf("\nEnter positive numbers");
Scanner( &row1, &col1, &row2, &col2 );
}
return;
}
/* Ask for elements of matrix.*/
void AddValues(int row, int col,float **ptr, int matrixNumber){
printf("\nEnter elements of %d. matrix :\n", matrixNumber);
for(int i=0; i< row; i++)
{
for(int j=0; j< col; j++)
{
printf("\tA[%d][%d] = ",i, j);
scanf("%f", &ptr[i][j]);
}
}
}
void Multiply(int row1, int col1, int col2, float ** ptr1, float ** ptr2, float ** ptr3){
/* Matrix multiplycation. */
for(int i=0; i < row1; i++)
{
for(int j=0; j < col1; j++)
{
ptr3[i][j] = 0;
for(int k=0; k<col2; k++)
ptr3[i][j] = ptr3[i][j] + ptr1[i][k] * ptr2[k][j];
}
}
}
void WriteOut(int row1, int col2, float** ptr3){
/* Print final matrix. */
printf("\n\nFinal matrix :");
for(int i=0; i< row1; i++)
{
printf("\n\t\t\t");
for(int j=0; j < col2; j++)
printf("%f\t", ptr3[i][j]);
}
}
void HighestSum(int row1, int col2, float **ptr3){
printf("\n\nHighest sum of elements in row :");
float max = 0;
float sum;
for (int i = 0; i < row1; ++i) {
for (int j = 0; j < col2; ++j){
sum = sum + ptr3[i][j];
if (j == col2 - 1)
printf("\n");
}
if (max == 0) max = sum;
if (max < sum)
max = sum;
sum = 0;
}
printf("Highest sum of elements in row: %f ", max);
}
int main()
{
clock_t start = clock();
float **ptr1, **ptr2, **ptr3;
int row1, col1, row2, col2;
int i;
Scanner( &row1, &col1, &row2, &col2 );
/* Allocation */
ptr1 = (float **) malloc(sizeof(float *) * row1);
ptr2 = (float **) malloc(sizeof(float *) * row2);
ptr3 = (float **) malloc(sizeof(float *) * row1);
for(i=0; i<row1; i++)
ptr1[i] = (float *)malloc(sizeof(float) * col1);
for(i=0; i<row2; i++)
ptr2[i] = (float *)malloc(sizeof(float) * col2);
for(i=0; i<row1; i++)
ptr3[i] = (float *)malloc(sizeof(float) * col2);
AddValues(row1,col1,ptr1,1);
AddValues(row2,col2,ptr2,2);
Multiply(row1,col1,col2,ptr1,ptr2,ptr3);
WriteOut(row1,col2,ptr3);
HighestSum(row1,col2,ptr3);
clock_t stop = clock();
double elapsed = (double) (stop - start) / CLOCKS_PER_SEC;
printf("\nTime elapsed: %.5f\n", elapsed);
return 0;
}
单独的乘法函数:
void Multiply(int row1, int col1, int col2, float ** ptr1, float ** ptr2, float ** ptr3){
/* Matrix multiplycation. */
for(int i=0; i < row1; i++)
{
for(int j=0; j < col1; j++)
{
ptr3[i][j] = 0;
for(int k=0; k<col2; k++)
ptr3[i][j] = ptr3[i][j] + ptr1[i][k] * ptr2[k][j];
}
}
}
2 个错误:
for(int j=0; j < col2; j++) // final matrix has col2 columns
{
ptr3[i][j] = 0;
for(int k=0; k<col1; k++)// loop on all row = loop on col1
ptr3[i][j] = ptr3[i][j] + ptr1[i][k] * ptr2[k][j];
}
我写的这个矩阵乘法程序有时会写错答案。它正确地使用大小为 2x3 3x3 的矩阵或至少使用数字我试过它是正确的但是如果我放入大小为 2x3 3x2 的矩阵并用数字 [-1 2 1][2 0 4] 和 [2 -3] 填充它们[1 4][-2 3] 程序输出[0 11][4 -6],正确的结果应该是[-2 14][-4 6]。代码或指针中是否存在错误?我只是忽略了公式中的一些错误吗?我不知道。
程序的完整代码:
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
void Scanner(int *row1, int *col1, int *row2, int *col2){
printf("\nNumber of rows for first matrix : ");
scanf("%d", row1);
printf("\nNumber of columns for first matrix : ");
scanf("%d", col1);
printf("\nNumber of rows for second matrix : ");
scanf("%d", row2);
printf("\nNumber of columns for second matrix. : ");
scanf("%d", col2);
if(*col1 != *row2)
{
printf("\nMatrixes cant be multiplyed.");
Scanner( &row1, &col1, &row2, &col2 );
}
if(*col1 <= 0 || *row1 <= 0 || *col2 <= 0 || *row2 <= 0){
printf("\nEnter positive numbers");
Scanner( &row1, &col1, &row2, &col2 );
}
return;
}
/* Ask for elements of matrix.*/
void AddValues(int row, int col,float **ptr, int matrixNumber){
printf("\nEnter elements of %d. matrix :\n", matrixNumber);
for(int i=0; i< row; i++)
{
for(int j=0; j< col; j++)
{
printf("\tA[%d][%d] = ",i, j);
scanf("%f", &ptr[i][j]);
}
}
}
void Multiply(int row1, int col1, int col2, float ** ptr1, float ** ptr2, float ** ptr3){
/* Matrix multiplycation. */
for(int i=0; i < row1; i++)
{
for(int j=0; j < col1; j++)
{
ptr3[i][j] = 0;
for(int k=0; k<col2; k++)
ptr3[i][j] = ptr3[i][j] + ptr1[i][k] * ptr2[k][j];
}
}
}
void WriteOut(int row1, int col2, float** ptr3){
/* Print final matrix. */
printf("\n\nFinal matrix :");
for(int i=0; i< row1; i++)
{
printf("\n\t\t\t");
for(int j=0; j < col2; j++)
printf("%f\t", ptr3[i][j]);
}
}
void HighestSum(int row1, int col2, float **ptr3){
printf("\n\nHighest sum of elements in row :");
float max = 0;
float sum;
for (int i = 0; i < row1; ++i) {
for (int j = 0; j < col2; ++j){
sum = sum + ptr3[i][j];
if (j == col2 - 1)
printf("\n");
}
if (max == 0) max = sum;
if (max < sum)
max = sum;
sum = 0;
}
printf("Highest sum of elements in row: %f ", max);
}
int main()
{
clock_t start = clock();
float **ptr1, **ptr2, **ptr3;
int row1, col1, row2, col2;
int i;
Scanner( &row1, &col1, &row2, &col2 );
/* Allocation */
ptr1 = (float **) malloc(sizeof(float *) * row1);
ptr2 = (float **) malloc(sizeof(float *) * row2);
ptr3 = (float **) malloc(sizeof(float *) * row1);
for(i=0; i<row1; i++)
ptr1[i] = (float *)malloc(sizeof(float) * col1);
for(i=0; i<row2; i++)
ptr2[i] = (float *)malloc(sizeof(float) * col2);
for(i=0; i<row1; i++)
ptr3[i] = (float *)malloc(sizeof(float) * col2);
AddValues(row1,col1,ptr1,1);
AddValues(row2,col2,ptr2,2);
Multiply(row1,col1,col2,ptr1,ptr2,ptr3);
WriteOut(row1,col2,ptr3);
HighestSum(row1,col2,ptr3);
clock_t stop = clock();
double elapsed = (double) (stop - start) / CLOCKS_PER_SEC;
printf("\nTime elapsed: %.5f\n", elapsed);
return 0;
}
单独的乘法函数:
void Multiply(int row1, int col1, int col2, float ** ptr1, float ** ptr2, float ** ptr3){
/* Matrix multiplycation. */
for(int i=0; i < row1; i++)
{
for(int j=0; j < col1; j++)
{
ptr3[i][j] = 0;
for(int k=0; k<col2; k++)
ptr3[i][j] = ptr3[i][j] + ptr1[i][k] * ptr2[k][j];
}
}
}
2 个错误:
for(int j=0; j < col2; j++) // final matrix has col2 columns
{
ptr3[i][j] = 0;
for(int k=0; k<col1; k++)// loop on all row = loop on col1
ptr3[i][j] = ptr3[i][j] + ptr1[i][k] * ptr2[k][j];
}