M
MN
Hi,
I was writing a program that must dynamically allocate memory to a 2-
dimensions array, initialize it to 0, then place the result of
multiplication of two 1-dimension arrays as described in the next code
example.
My code is:
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int A[7] = {1,1,1,1,1,1,1};
int B[4] = {2,2,2,2};
int i = 0;
int ii = 0;
int j = 0;
int jj = 0;
int k = 0;
int deg_a = sizeof(A)/sizeof(A[0]) - 1;
int deg_b = sizeof(B)/sizeof(B[0]) - 1;
int C_dimension = deg_a+deg_b+1;
int** C_1 = NULL;
if (C_1 != NULL)
free(C_1);
C_1 = (int**)malloc(sizeof(int**) * (deg_a+1));
for (i = 0; i< (deg_a+1); i++)
C_1= malloc(C_dimension);
// Initialize all values of C_1 to 0
printf("C_1 is initialized to 0:\n");
for(j = 0; j < (deg_a+1); j++)
{
for (k = 0; k < C_dimension; k++)
{
C_1[j][k] = 0;
// For verification
printf("%3d ", C_1[j][k]);
}
printf("\n");
}
// Do multiplication of A[deg_a+1] and B[deg_b+1]
for (ii = 0; ii <(deg_a+1); ii++)
{
for(jj = 0; jj <(deg_b+1) ; jj++)
{
C_1[ii][ii+jj] = A[ii]*B[jj];
}
}
printf("\n");
// For verification
printf("After multiplication of A[%d] and B[%d], C_1 is:\n", deg_a
+1, deg_b+1);
for(j = 0; j < (deg_a+1); j++)
{
for (k = 0; k < C_dimension; k++)
{
printf("%3d ", C_1[j][k]);
}
printf("\n");
}
return 0;
}
After executing I get:
../test_array
C_1 is initialized to 0:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
After multiplication of A[7] and B[4], C_1 is:
2 2 2 2 0 0 0 0 0 2
0 2 2 2 2 0 0 0 0 0
0 0 2 2 2 2 0 0 0 0
0 0 0 2 2 2 2 0 0 0
0 0 0 0 2 2 2 2 0 0
0 0 0 0 0 2 2 2 2 0
2 0 0 0 0 0 2 2 2 2
what I want as result is:
2 2 2 2 0 0 0 0 0 0
0 2 2 2 2 0 0 0 0 0
0 0 2 2 2 2 0 0 0 0
0 0 0 2 2 2 2 0 0 0
0 0 0 0 2 2 2 2 0 0
0 0 0 0 0 2 2 2 2 0
0 0 0 0 0 0 2 2 2 2
After debugging I noticed that C_1[1][0] and C_1[0][9] get the same
value at the same time, although I didn't change the value of C_1[0]
[9].
So why this occurs?
Thanks for your help.
I was writing a program that must dynamically allocate memory to a 2-
dimensions array, initialize it to 0, then place the result of
multiplication of two 1-dimension arrays as described in the next code
example.
My code is:
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int A[7] = {1,1,1,1,1,1,1};
int B[4] = {2,2,2,2};
int i = 0;
int ii = 0;
int j = 0;
int jj = 0;
int k = 0;
int deg_a = sizeof(A)/sizeof(A[0]) - 1;
int deg_b = sizeof(B)/sizeof(B[0]) - 1;
int C_dimension = deg_a+deg_b+1;
int** C_1 = NULL;
if (C_1 != NULL)
free(C_1);
C_1 = (int**)malloc(sizeof(int**) * (deg_a+1));
for (i = 0; i< (deg_a+1); i++)
C_1= malloc(C_dimension);
// Initialize all values of C_1 to 0
printf("C_1 is initialized to 0:\n");
for(j = 0; j < (deg_a+1); j++)
{
for (k = 0; k < C_dimension; k++)
{
C_1[j][k] = 0;
// For verification
printf("%3d ", C_1[j][k]);
}
printf("\n");
}
// Do multiplication of A[deg_a+1] and B[deg_b+1]
for (ii = 0; ii <(deg_a+1); ii++)
{
for(jj = 0; jj <(deg_b+1) ; jj++)
{
C_1[ii][ii+jj] = A[ii]*B[jj];
}
}
printf("\n");
// For verification
printf("After multiplication of A[%d] and B[%d], C_1 is:\n", deg_a
+1, deg_b+1);
for(j = 0; j < (deg_a+1); j++)
{
for (k = 0; k < C_dimension; k++)
{
printf("%3d ", C_1[j][k]);
}
printf("\n");
}
return 0;
}
After executing I get:
../test_array
C_1 is initialized to 0:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
After multiplication of A[7] and B[4], C_1 is:
2 2 2 2 0 0 0 0 0 2
0 2 2 2 2 0 0 0 0 0
0 0 2 2 2 2 0 0 0 0
0 0 0 2 2 2 2 0 0 0
0 0 0 0 2 2 2 2 0 0
0 0 0 0 0 2 2 2 2 0
2 0 0 0 0 0 2 2 2 2
what I want as result is:
2 2 2 2 0 0 0 0 0 0
0 2 2 2 2 0 0 0 0 0
0 0 2 2 2 2 0 0 0 0
0 0 0 2 2 2 2 0 0 0
0 0 0 0 2 2 2 2 0 0
0 0 0 0 0 2 2 2 2 0
0 0 0 0 0 0 2 2 2 2
After debugging I noticed that C_1[1][0] and C_1[0][9] get the same
value at the same time, although I didn't change the value of C_1[0]
[9].
So why this occurs?
Thanks for your help.