Home > Software design >  Why is my matrix multiplying code producing wrong answer?
Why is my matrix multiplying code producing wrong answer?

Time:05-11

i have this matrix multipication program written that sometimes writes wrong answers. It worked with matrixes of size 2x3 3x3 correctly or atleast with numbers i tried it was correct but if i put in matrixes of size 2x3 3x2 and fill them with numbers [-1 2 1][2 0 4] and [2 -3][1 4][-2 3] the program outputs [0 11][4 -6] and the correct results should be [-2 14][-4 6]. Is there and error in the code or the pointers ? Am i just overlooking some error in formula ? I have no idea.

Full code of program:

#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;
}

The multiplycation function alone:

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];
        }
    }
}

CodePudding user response:

2 errors :

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];
    }
  • Related