-3

I have a written a c program on the product of two matrices.When i compile i find no error but when i run it i find the aforementioned error.I have tried multiple solutions as suggested by youtube videos.But nothing worked.I have pirated windows 10 copy installed on my pc.I run the program on codeblocks 20.03 Here is the program:

#include<stdio.h>
int main()
{
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
    printf("Enter the rows and columns Of matrix a and b(r1,c1,r2,c2):");
    scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
    printf("Input matrices elements");
    int i,j;
    printf("Input elements of matrix a:");
    for(i=0;i<r1;i++){
        for(j=0;j<c1;j++)
        scanf("%d",&a[i][j]);
    }
    printf("Input elements of matrix b:");
    for(i=0;i<r2;i++){
        for(j=0;j<c2;j++)
        scanf("%d",b[i][j]);
    }
    printf("Matrix a is:\n");
    for(i=0;i<r1;i++){
        for(j=0;j<c1;j++)
        printf("%d ",a[i][j]);
    }
    printf("Matrix b is:\n");
    for(i=0;i<r2;i++){
        for(j=0;j<c2;j++)
        printf("%d ",b[i][j]);
    }
    if(c1==r2)
    printf("\nMatrix multiplication is possible.....\n");
    else
    return 0;

    for(i=0;i<r2;i++){
        sum=0;
        for(j=0;j<c2;j++)
        sum+=a[i][j]*b[j][i];
        c[i][j]=sum;
    }
    printf("\nProduct of matrices is:\n");
    for(i=0;i<r1;i++){
        for(j=0;j<c2;j++)
        printf("%d ",c[i][j]);
        printf("\n");
    }

    return 0;
}

Note: My program may not be correct,all i want to do is run the program and letter on i will debug it.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
Shubham
  • 25
  • 4
  • This can't be right: the variable arrays in the first line of `main()` have dimensions of `r1`, `c1` (etc) that are not initialized, so nobody knows how much memory is actually allocated. – Steve Friedl Sep 03 '20 at 13:46
  • 1
    Why don't you debug it *now*? Obviously you have a problem *now* so why would you say that you won't debug it until later? – user253751 Sep 03 '20 at 14:16
  • Don't "fix" the code in your question, it invalidates large parts of the posted answers. – Blastfurnace Sep 03 '20 at 16:04
  • @user253751 a person can debug the problem only after he is able to execute it.In the present scenario program wouldn't execute after first two inputs. – Shubham Sep 06 '20 at 16:23
  • @Blastfurnace thanks for your suggestion,next time i will keep this thing in mind. – Shubham Sep 06 '20 at 16:24

2 Answers2

1

Try compiling with clang -Weverything -Wno-vla:

c.c:17:20: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
        scanf("%d",b[i][j]);
               ~~  ^~~~~~~
c.c:4:27: warning: variable 'c1' is uninitialized when used here [-Wuninitialized]
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
                          ^~
c.c:4:17: note: initialize the variable 'c1' to silence this warning
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
                ^
                 = 0
c.c:4:23: warning: variable 'r1' is uninitialized when used here [-Wuninitialized]
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
                      ^~
c.c:4:11: note: initialize the variable 'r1' to silence this warning
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
          ^
           = 0
c.c:4:37: warning: variable 'c2' is uninitialized when used here [-Wuninitialized]
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
                                    ^~
c.c:4:20: note: initialize the variable 'c2' to silence this warning
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
                   ^
                    = 0
c.c:4:33: warning: variable 'r2' is uninitialized when used here [-Wuninitialized]
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
                                ^~
c.c:4:14: note: initialize the variable 'r2' to silence this warning
    int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;
             ^
              = 0

You initialize all your matrizes with a garbage value size, as you 1. didn't initialize the variables and 2. declared the VLAs on the same line==> Leading to garbage sizes.

First read the values from the user, then declare an VLA. (Although using malloc and Co. would be better).

JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
  • @JCWasmax86 I don't know how to compile with clang -Weverything -Wno-vla .I am just started learning my first programming language. – Shubham Sep 03 '20 at 14:01
  • How do you compile with an IDE or from the commandline? – JCWasmx86 Sep 03 '20 at 14:04
  • Does this (https://stackoverflow.com/a/40962079/13912132) help you? BUT: You can't use `-Weverything` with codeblocks. Use `-Wall -Wextra -pedantic`, as gcc doesn't support `-Weverything`. If you additionally use `-Werror`, your compiler will make all warnings to errors. – JCWasmx86 Sep 03 '20 at 14:06
0

You have the primary problem in

  int r1,r2,c1,c2,a[r1][c1],b[r2][c2],c[r1][c2],sum=0;

where you use the values of r1, r2, c1, c2 when they are indeterminate (i.e., not initialized or assigned). You should see some compiler warnings along the lines of

warning: ‘r1’ is used uninitialized in this function [-Wuninitialized]
warning: ‘c2’ is used uninitialized in this function [-Wuninitialized]
warning: ‘r2’ is used uninitialized in this function [-Wuninitialized]

If you don't see them, use your compiler flags to enable warnings.

To solve this: You would need to scan the values first, and then use them as array dimensions.

Then, the next problem: you have a typo at

 scanf("%d",b[i][j]);

it should be

 scanf("%d", &b[i][j]);
            ^^

That said, always check for the return value of scanf() to ensure scanning success before you use the scanned values.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261