-1

It successfully compiles. But at running on getting values of matrix it crashes stops working.

#include <stdio.h>

void getmat(int mat[100][100],int m,int n);
void matmul(int mat1[100][100],int mat2[100][100],int m1,int n1,int m2,int 
n2,int matmul[100][100]);
void printmat(int matmul[100][100],int m,int n);

int main(void)
{
    int m1,n1,m2,n2;
    printf("Enter the dimensions of matrix1: ");
    scanf("%d %d",&m1,&n1);
    printf("Enter the dimensions of matrix2: ");
    scanf("%d %d",&m2,&n2);
    int mat1[m1][n1];
    int mat2[m2][n2];
    int matmul1[m1][n2];
    int matmul2[m2][n1];
    printf("For the values of matrix 1\n");
    getmat(mat1,m1,n1);
    printf("For the values of matrix 2\n");
    getmat(mat2,m2,n2);
    if(n1==m2)
    {
        printf("Mat1 x Mat2 is possible.");
        matmul(mat1,mat2,m1,n1,m2,n2,matmul1);
        printf("Mat1 x Mat2 :\n");
        printmat(matmul1,m1,n2);
    }
    else
        printf("Mat1 x Mat2 is not possible.\n");
    if(n2==m1)
    {
        printf("Mat2 x Mat1 is possible.");
        matmul(mat2,mat1,m2,n2,m1,n1,matmul2);
        printf("Mat2 x Mat1 :\n");
        printmat(matmul2,m2,n1);
    }
    else
        printf("Mat2 x Mat1 is not possible.\n");
    return 0;
}   


void printmat(int matmul[100][100],int m,int n)
{
    int i,j;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%3d ",matmul[i][j]);
        }
        printf("\n");
    }
}




void getmat(int mat[100][100],int m,int n)
{
    int i,j;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("Enter element of %dx%d: ",i+1,j+1);
            scanf("%d",&mat[i][j]);
        }
    }
}
void matmul(int mat1[100][100],int mat2[100][100],int m1,int n1,int m2,int 
n2,int matmul[100][100])
{   
    int i,j,k;
    for(i=0;i<m1;i++)
    {
        for(j=0;j<n2;j++)
        {   
            matmul[i][j]=0;
            for(k=0;k<m2;k++)
            {
                matmul[i][j]+=mat1[i][k]*mat2[k][j];
            }
        }
    }
}

Help making any changes or optimizing this code. Also another way to this. This error shows up at running a half

abelenky
  • 63,815
  • 23
  • 109
  • 159
Mr NoBoDy
  • 1
  • 4
  • 7
    This is a good time to learn how to use a debugger. Can you at least tell us the exact line where the code crashes? – Tim Biegeleisen Apr 24 '17 at 15:31
  • 1
    Possible duplicate of [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Thomas Ayoub Apr 24 '17 at 15:33
  • 1
    You have fixed size 100x100 matrices in the argument list to `getmat()`; you are passing smaller VLA matrices. Happiness does not ensue. The fix is to pass the array dimensions before the array, and to specify that the functions are passed a VLA. – Jonathan Leffler Apr 24 '17 at 15:37
  • Here, this may help you : [link](http://stackoverflow.com/questions/14548753/passing-a-multidimensional-variable-length-array-to-a-function) – รยקคгรђשค Apr 24 '17 at 15:45
  • To listen to your compiler's warnings also is a good idea. – alk Apr 24 '17 at 16:47
  • 1
    @TimBiegeleisen It was getting out of bounds as i declared array[100][100] and was passing array with variable size. Passing array as pointers worked for me. – Mr NoBoDy Apr 24 '17 at 16:48
  • @JonathanLeffler Thankyou. I got it, passing the array as pointers worked for me. – Mr NoBoDy Apr 24 '17 at 16:51

2 Answers2

4

Use a Debugger

Also, all your functions are written to take a 100x100 matricies.

But you declare your matricies to have variable sizes:

int mat1[m1][n1];
int mat2[m2][n2];
int matmul1[m1][n2];
int matmul2[m2][n1];

When you pass a 3x3 matrix to a function that is expecting a 100x100 matrix, you will definitely have a bad time.

enter image description here

abelenky
  • 63,815
  • 23
  • 109
  • 159
-1

I see that you are setting up your arrays with variable sizes. You should really set it up as a very large array or use malloc to set up the array properly. When you call the processing routines, pass the arrays as pointers and use the sizes as arguments in order to set up your references. You should note that myarray[i, j] is the equivalent of a single valued array using i*rowsize + j. When you define your array in the subroutines as [100, 100], it will go to [i*100 + j] which is outside the bounds of your actual array.

This causes the program to crash.

Once you actually calculate everything properly, it should work.

sabbahillel
  • 4,357
  • 1
  • 19
  • 36