Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

First row doesnt work in matrix product using C

I’m a beginner into C and I’ve been stuck on this particular question for the past few hours now. I need to multiply 2 matrices but the code despite being correct as far as I know, just doesn’t work with the first row of the resultant matrix

Please do not mark this as duplicate without going through this entire question atleast once; I genuinely need help.

e.g.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

but my code outputs this instead

enter image description here

code:

// Multiply two matrices

# include <stdio.h>

int main()
{
    // declare itertives and matrices 
    int i, j, k, rows0, cols0, rows1, cols1, matrix0[10][10], matrix1[10][10], matrix2[10][10];
    printf("Number of Rows and columns shouldn't exceed 10\n");
    printf("Enter number of rows and columns for first Matrix\n");
    printf("Enter number of rows: ");
    scanf("%d", &rows0);
    printf("Enter number of columns: ");
    scanf("%d", &cols0);

    printf("Enter number of rows and columns for second Matrix\n");
    printf("Enter number of rows: ");
    scanf("%d", &rows1);
    printf("Enter number of columns: ");
    scanf("%d", &cols1);

    if (cols0!=rows1)
    {
        printf("Number of columns in first matrix should be equal to number of rows in second matrix\n");
        return 0;
    }

    printf("Input values into first Matrix\n");

    for(i = 0; i < rows0; i++)
    {
        printf("Input values in row %d with each value separated with space\n", i);
        for(j=0; j< cols0; j++)
        {
            scanf("%d", &matrix0[i][j]);
        }
    }

    printf("Input values into second Matrix\n");

    for(i = 0; i < rows1; i++)
    {
        printf("Input values in row %d with each value separated with space\n", i);
        for(j=0; j< cols1; j++)
        {
            scanf("%d", &matrix1[i][j]);
        }
    }

    // Matrix multiplication

    for (i=0; i<rows0; i++)
    {
        matrix2[i][j]=0;
        for (j=0; j< cols1; j++)
        {
            for(k=0;k<cols1;k++)
            {
                matrix2[i][j]+=(matrix0[i][k]*matrix1[k][j]);
            }
        }
    }

    printf("Resultant matrix after matrix multiplication is\n");
    for(i=0; i < rows0; i++)
    {
        for(j=0; j<cols1; j++)
        {
            printf("%d ", matrix2[i][j]);
        }
        printf("\n");
    }
}

output

enter image description here

>Solution :

matrix2[i][j]=0; out of place. You owe me 60 seconds of debugging 🙂

See https://ideone.com/NS9vo6

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading