Reputation: 75
I am multiplying 2-D arrays and am using dynamic allocation when initializing the arrays (but I don't know if the implementation is right). I am compiling my code with CodeBlock16.0. The program runs well for matrix sizes below 200, but I want this code to run for matrix sizes over 1000.
This is the code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
int **matriks1=NULL,**matriks2=NULL,**result=NULL, r1=0, c1=0, r2=0, c2=0, i, j, k;
printf("Enter rows and column for first matrix: ");//input size for matrix 1
scanf("%d %d", &r1, &c1);
matriks1=(int **)malloc(r1*sizeof(int*));
for(int i=0;i<r1;++i) matriks1[i]=(int*)malloc(c1*sizeof(int));
result=(int**)calloc(r1,sizeof(int*));
printf("Enter rows and column for second matrix: ");//input size for matrix 2
scanf("%d %d",&r2, &c2);
matriks2=(int **)malloc(r2*sizeof(int*));
for(int i=0;i<r2;++i) matriks2[i]=(int*)malloc(c2*sizeof(int));
for(int i=0;i<c2;++i) result[i]=(int*)calloc(c2,sizeof(int));
srand(time(NULL));
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! Kolom matriks pertama tidak sama dengan baris matriks kedua.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf("\nElements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j){
printf("Elements a%d%d: ",i+1, j+1);
matriks1[i][j]=rand()%10;
printf("%d ",matriks1[i][j]);
//scanf("%d", &a[i][j]);
}
// Storing elements of second matrix.
printf("\nElements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j){
printf("Enter elements b%d%d: ",i+1, j+1);
matriks2[i][j]=rand()%10;
printf("%d ",matriks2[i][j]);
}
// Initializing all elements of result matrix to 0
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=matriks1[i][k]*matriks2[k][j];
}
// Displaying the result
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf("\n\n");
}
for(int i=0;i<r1;i++){
free(matriks1[i]);
free(matriks2[i]);
free(result[i]);
}
free(matriks1);
free(matriks2);
free(result);
return 0;
}
Upvotes: 2
Views: 36
Reputation: 31599
result = calloc(r1,sizeof(int*)); //<== `r1` count declared in array
//for(int i = 0; i<c2; ++i) //<== remove
for(int i = 0; i < r1; ++i) //<== should allocate up to `r1`
result[i] = (int*)calloc(c2, sizeof(int));
The for
loop is clearly meant to count up to r1
, not c2
. Also you don't need the casting in C language.
Clearly not all the arrays are the same size. Free the arrays according to their size as follows:
//for(int i=0;i<r1;i++){ <= remove
// free(matriks1[i]);
// free(matriks2[i]);
// free(result[i]);
//}
for(int i = 0; i < r1; i++) free(matriks1[i]);
for(int i = 0; i < r2; i++) free(matriks2[i]);
for(int i = 0; i < r1; i++) free(result[i]);
free(matriks1);
free(matriks2);
free(result);
Upvotes: 1