strange array shift

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.
 
M

MN

That's probably the issue, anyway - insufficient storage for your array.
But since it's not obvious to me what you're trying to do, it may well be
that there are other issues too that I didn't spot.

What I want to get is a program that can do a polynomial
multiplication, so I supposed that each polynomial can be represeteb
by an array where:
array[0] represents the coeffcient of x^0
array[1] represents the coeffcient of x^1
array[2] represents the coeffcient of x^2
..... and so on.

After I can multiply each coefficient of the first array with all
coefficients of the second array and put the result in a new row of an
new array (in this case called C_1). At the end I can easily do the
sum of all elements that are in the column in C_1 and this will give
the final result of polynomial multiplication.
As I think (may be I'm wrong!) this is the simple approach to code
polynomial multiplication in c language. Perhaps there is another and
simple way to do this.
 
G

gw7rib

That's probably the issue, anyway - insufficient storage for your array.
But since it's not obvious to me what you're trying to do, it may well be
that there are other issues too that I didn't spot.

What I want to get is a program that can do a polynomial
multiplication, so I supposed that each polynomial can be represeteb
by an array where:
array[0] represents the coeffcient of x^0
array[1] represents the coeffcient of x^1
array[2] represents the coeffcient of x^2
.... and so on.

After I can multiply each coefficient of the first array with all
coefficients of the second array and put the result in a new row of an
new array (in this case called C_1). At the end I can easily do the
sum of all elements that are in the column in C_1 and this will give
the final result of polynomial multiplication.
As I think (may be I'm wrong!) this is the simple approach to code
polynomial multiplication in c language. Perhaps there is another and
simple way to do this.

If that's what you're trying to do, you don't need a two-dimensional
array at all. Given the difficulties of two-dimensional arrays in C,
an approach which does not use them should be simpler. What's wrong
with:

(allocate C to the right size, and initialise all the values to zero)

for (ii = 0; ii <(deg_a+1); ii++)
{
for(jj = 0; jj <(deg_b+1) ; jj++)
{
C[ii+jj] += A[ii]*B[jj];
}
}

Hope that helps.
Paul.
 
K

Keith Thompson

MN said:
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;

}

[...]

Among other oddities in your code, you compute deg_a and deg_b as 1
less than the lengths of the A and B arrays, but then you almost
always refer to deg_a+1 and deg_b+1, which are simply the lengths of A
and B. Why not just compute and use their lengths?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top