0

It compiles with no errors but when I try to execute when it reaches this part of the code it freaks out and stops.(crashes and the exe doesn't respond)

//Creation of array 2 lines_2 x cols_2 size
array_2 = (int **)malloc(lines_2 * sizeof(int*));
for( i=0; i< lines_2;i++)
{
    array_2[i] = (int*) malloc(cols_2 * sizeof(int));
}

Full code I am trying to create a program that multiplies two dynamic arrays.

#include <stdio.h>
#include <stdlib.h>

int CheckIfMul(int ,int );//intx2
void Fill( int **,int ,int );//ar intx2
void ArrayMuArray( int **, int **, int **,int,int );

int main(){
    int i,lines_1,lines_2,cols_1,cols_2,**array_1,**array_2,**array_r1,**array_r2;
    //input
    do{
       do{
          printf("Enter the lines of the first array.\n");
          scanf(" %d/n",&lines_1);
       }while(lines_1 <1);
       do{
          printf("Enter the columns of the first array.\n");
          scanf(" %d/n/n",&cols_1);
       }while(cols_1 >5);
       do{
          printf("Enter the lines of the second array.\n");
          scanf(" %d/n",&lines_2);
       }while(lines_1 <1);
       do{
          printf("Enter the columns of the second array.\n");
          scanf(" %d/n/n",&cols_2);
       }while(cols_2 >5);
    }while(CheckIfMul(lines_1,cols_2)==0);
    printf("HERE");
    //Creation of array 1 lines_1 x cols_1 size
    array_1 = (int **)malloc(lines_1 * sizeof(int*));
    for( i=0; i<lines_1;i++){
        array_2[i] = (int *)malloc(cols_1 * sizeof(int));
    }
    //Creation of array 2 lines_2 x cols_2 size
    array_2 = (int **)malloc(lines_2 * sizeof(int*));
    for( i=0; i< lines_2;i++){
        array_2[i] = (int*) malloc(cols_2 * sizeof(int));
    }

    //Creating array for mult array_1 * array_2 = array_r1
    array_r1 = (int **)malloc(lines_1 * sizeof(int*));
    for( i=0; i< lines_1;i++){
        array_r1[i] = (int*) malloc(cols_2 * sizeof(int));
    }
    //Creating array for mult array_r1 * a = array_r2
    array_r2 = (int **)malloc(lines_1 * sizeof(int*));
    for( i=0; i< lines_1;i++){
        array_r2[i] = (int*) malloc(cols_2 * sizeof(int));
    }

    Fill(array_1,lines_1,cols_1);//user fills array 1
    Fill(array_2,lines_2,cols_2);//user fills array 2


    ArrayMuArray(array_1,array_2,array_r1,lines_1,cols_2);//Multiplier

    return 0;
}

int CheckIfMul (int lines,int cols)//checks if the arrays 1 and 2 can be multiplied
{
     if(lines==cols){
        //true
        return 1;
     }else{
        //flase
        printf("\aThese arrays can't be multiplied.\n\n");
        return 0;
     }
}

void Fill(int **array,int lines,int cols){//Asks the user to input int into a dynamic array
    int i,j;
    for( i=0;i<lines;i++){
        for( j=0;j<cols;j++){
            printf("Enter value for position (%d,%d).",i,j);
            scanf(" %d/t",&array[i][j]);
        }
        printf("\n");
    }
}

void ArrayMuArray(int **array_1,int **array_2,int **array_r1,int lines,int cols){
    int i,j,u,s;
    for( i=0;i<lines;i++){
        for( j=0;j<cols;j++){
            for( u=0;u<cols;u++){
                s = s + (array_1[i][u] * array_2[u][j]);
            }
            array_r1[i][j] = s;
            s = 0;
        }
    }
}
KALALEX
  • 430
  • 1
  • 5
  • 19

1 Answers1

1

In this part of the code is the problem...

array_1 = (int **)malloc(lines_1 * sizeof(int*));
for( i=0; i<lines_1;i++){
    array_2[i] = (int *)malloc(cols_1 * sizeof(int));
}

Since this line.

array_2[i] = (int *)malloc(cols_1 * sizeof(int));

Should be.

array_1[i] = (int *)malloc(cols_1 * sizeof(int));

Because at that point you haven't allocated memory for array_2 yet.

Note: You should read this, it's not recommended to cast malloc.

Community
  • 1
  • 1
Patricio Sard
  • 2,092
  • 3
  • 22
  • 52