I am trying to add 2 dynamically created arrays. I create 2 arrays then a third
ID: 3869229 • Letter: I
Question
I am trying to add 2 dynamically created arrays. I create 2 arrays then a third array to store the results of the addition of the first 2 arrays. My program seems to be functioning fine and it hangs up somewhere in the addition function.
Here is my code that creates the arrays which is working:
void CreateArrays(int** a, int**b)
{
int dataReadSuccess =0;
int i,j;
if(a != NULL)
{
FreeArray(a, N_a);
}
if(b != NULL) //
{
FreeArray(b, N_b);
}
int N=0;
printf("Enter the size N to create two NxN arrays: ");
scanf("%d", &N);
while(N<2)
{
printf("Error--Enter a positive size integer greater than 2. ");
printf("Enter the size N to create two NxN arrays: ");
scanf("%d", &N);
}
N_a = N;
M_a = N;
N_b = N;
M_b = N;
int errorInAlloc = 0;
a = (int**) malloc(N_a * sizeof(int));
if (a==NULL) errorInAlloc =1;
for (i=0; i<M_a && errorInAlloc ==0;i++)
{
a[i]=(int*)malloc(M_a * sizeof(int));
}
for (i=0;i<N_a;i++)
{
for (j=0;j<M_a;j++)
{
a[i][j]=rand()%10+1;
if(a[i][j] == NULL) errorInAlloc = 1;
}
}
b = (int**) malloc(N_b * sizeof(int));
if (b==NULL) errorInAlloc =1;
for (i=0; i<M_b && errorInAlloc ==0;i++)
{
b[i]=(int*)malloc(M_b * sizeof(int));
}
for (i=0;i<N_b;i++)
{
for (j=0;j<M_b;j++)
{
b[i][j]=rand()%10+1;
if(b[i][j] == NULL) errorInAlloc = 1;
}
}
if(errorInAlloc==1)
{
printf("Error-- Unable to allocate arrays ");
}
else
{
printf("The first array is: ");
PrintArray(a, N_a, M_a);
printf(" ");
printf("The second array is: ");
PrintArray(b, N_b, M_b);
printf(" ");
}
}
This is the code I use to create the "results array" and call the addition function:
if(N_a==N_b && M_a == M_b)
{
if(c != NULL) FreeArray(c, N_c);
N_c = N_a;
M_c = M_a;
c = (int**) malloc(N_c * sizeof(int*));
for(i=0;i<N_c;i++)
{
c[i] = (int*)malloc(M_c * sizeof(int));
}
MatrixAddition(a,b,c,N_a,M_a);
printf("A + B = ");
PrintArray(c, N_c, M_c);
}
and here is my addition function:
void MatrixAddition (int** a, int** b, int** result, int N, int M)
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
result[i][j] = a[i][j]+b[i][j];
}
}
}
I am confident that my function declarations and defintions match. I'm getting no build errors either.
Thanks!
Explanation / Answer
This is a heavily modified code. But the problem seems to be passing empty arrays to the function CreateArrays.
Do this before calling the function CreateArrays.
a = (int**) malloc(sizeof(int*)*N);
b = (int**) malloc(sizeof(int*)*N);
#include <stdio.h>
#include <stdlib.h>
//#include <malloc.h>
//int **a,**b,**c;
void PrintArray(int N,int*a[N])
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
printf("%d ",a[i][j]);
}
printf(" ");
}
}
void CreateArrays(int N,int* a[N], int*b[N])
{
int dataReadSuccess =0;
int i,j;
/*if(a != NULL)
{
FreeArray(a, N);
}
if(b != NULL) //
{
FreeArray(b, N);
}*/
//printf("hklfhk");
//int errorInAlloc = 0;
//if (a==NULL) errorInAlloc =1;
//printf("hklfhk1");
for (i=0; i<N;i++)
{
a[i]=(int*)malloc(N * sizeof(int));
}
//printf("hklfhk2");
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
a[i][j]=rand()%10+1;
//if(a[i][j] == NULL) errorInAlloc = 1;
}
}
//b = (int**) malloc(N * sizeof(int));
//if (b==NULL) errorInAlloc =1;
for (i=0; i<N;i++)
{
b[i]=(int*)malloc(N * sizeof(int));
}
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
b[i][j]=rand()%10+1;
//if(b[i][j] == NULL) errorInAlloc = 1;
}
}
/*if(errorInAlloc==1)
{
printf("Error-- Unable to allocate arrays ");
}
else*/
printf("The first array is: ");
PrintArray(N,a);
printf(" ");
printf("The second array is: ");
PrintArray(N,b);
printf(" ");
//return N;
}
void MatrixAddition (int N, int* a[N], int*b[N], int*result[N])
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
result[i][j] = a[i][j]+b[i][j];
}
}
}
//This is the code I use to create the "results array" and call the addition function:
int main(void)
{
int**a=NULL;
int**b=NULL;
int**c=NULL;
//int N;
int i,j;
int N=0;
printf("Enter the size N to create two NxN arrays: ");
scanf("%d",&N);
//printf("hklfhk0");
while(N<2)
{
printf("Error--Enter a positive size integer greater than 2. ");
printf("Enter the size N to create two NxN arrays: ");
scanf("%d", &N);
}
a = (int**) malloc(sizeof(int*)*N);
b = (int**) malloc(sizeof(int*)*N);
CreateArrays(N,a,b);
//MatrixAddition(a,b,c,N,N);
//if(c != NULL) FreeArray(c, N);
c = (int**) malloc(N * sizeof(int*));
for(i=0;i<N;i++)
{
c[i] = (int*)malloc(N * sizeof(int));
}
MatrixAddition(N,a,b,c);
printf("A + B = ");
PrintArray(N,c);
return 0;
}
//and here is my addition function:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.