473,414 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

strange array shift

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[i]= 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.

Sep 19 '08 #1
5 1548
MN said:
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};
Okay, we have an array of seven ints, and an array of four ints. For matrix
multiplication, the number of columns in A has to match the number of rows
in B. So there are only two ways for this to make sense. One of them is:

1
1
1
[ 1 ] * [ 2 2 2 2 ]
1
1
1

which multiplies out to

2 2 2 2
2 2 2 2
2 2 2 2
[ 2 2 2 2 ]
2 2 2 2
2 2 2 2
2 2 2 2

and the other is the same deal only rotated 90 degrees.

>
int i = 0;
int ii = 0;

int j = 0;
int jj = 0;
Indices, presumably.
>
int k = 0;

int deg_a = sizeof(A)/sizeof(A[0]) - 1;
int deg_b = sizeof(B)/sizeof(B[0]) - 1;
deg_a is 6, and deg_b is 4. These look like "max index" values to me.
int C_dimension = deg_a+deg_b+1;
It is no longer obvious to me what you are actually trying to do.
>
int** C_1 = NULL;
if (C_1 != NULL)
free(C_1);
How can it not be NULL? You only just set it to NULL.
C_1 = (int**)malloc(sizeof(int**) * (deg_a+1));
That's wrong. C_1 has type int **, so you're using it to point at a bunch
of int *, so you need space for a bunch of int *, not a bunch of int **.
You can get this right every time using the following template:

p = malloc(n * sizeof *p);

Thus, in your case:

C_1 = malloc((deg_a + 1) * sizeof *C_1);
Okay, so what do you do with this memory?
for (i = 0; i< (deg_a+1); i++)
C_1[i]= malloc(C_dimension);
Here's your problem right here. Well, /a/ problem. You're not allocating
enough storage (unless ints are only 1 byte wide on your system, which can
happen, but isn't typical on desktop machines).
>
// 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");
}
Well, okay, you could simplify this a lot while you're fixing the bug:

if(C_1 == NULL)
{
take evasive action
}
else
{
for(i = 0; i < deg_a + 1; i++)
{
C_1[i] = calloc(C_dimension, sizeof *C_1[i]);
if(C_1[i] == NULL)
{
take evasive action
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 19 '08 #2
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.
Sep 19 '08 #3
On 19 Sep, 12:05, MN <mazouz.nezh...@gmail.comwrote:
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.
Sep 19 '08 #4
MN

Paul,
Thanks for help!! program is working !
Sep 19 '08 #5
MN <ma************@gmail.comwrites:
[...]
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[i]= 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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 19 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
15
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
6
by: Joe Piscapo | last post by:
Hello, When I compile my program in Visual Studio it does not work for every input, some numbers make it crash. But compiling in gcc works for those numbers that made it crash in Visual Studio. ...
2
by: Hans Kesting | last post by:
Hi, Navigating with "tab" and "shift-tab" between textbox and radiobutton has sometimes some strange results. Some example fields: - several textboxes (say: tb1, tb2) - some radiobuttons...
4
by: Kev | last post by:
Hello, I have an Access 2003 database running on an XP network. I have a datasheet subform containing a 28 day roster - shift1 to shift28. Each record has 1 RosterEmpID, 1 EmployeeNumber, 28...
2
by: Bint | last post by:
Hi, What is a simple way to shift the elements in an array, circularly? Is there a way to do it so that you don't need a separate storage array? IE I want to shift array A{1,2,3,4,5,6,7,8} by...
11
by: perez.angela7 | last post by:
Hi all, I'm a student who just started programming in C. I have the following question: if I want to shift the elements of an array and insert a new value as the last element, I use this syntax...
1
by: pantagruel | last post by:
Hi, I have an array like the following: for(x=0;x<results.length;x++){ alert(results.length); extracted=results.shift(); alert(results.length); if(results.indexOf(extracted)== -1){
1
by: pitjpz | last post by:
We have moved our Database to another server. The server it was on used SQL 4 and the new one its on now uses SQL5 the only problem we can find is that when you attempt to delete a record from...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.