473,805 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s izeof(int**) * (deg_a+1));

for (i = 0; i< (deg_a+1); i++)
C_1[i]= malloc(C_dimens ion);

// 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 1575
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(s izeof(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_dimens ion);
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_dimens ion, 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.comwrite s:
[...]
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(s izeof(int**) * (deg_a+1));

for (i = 0; i< (deg_a+1); i++)
C_1[i]= malloc(C_dimens ion);

// 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_Keit h) 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
2786
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
6253
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 *xPrev, float *yPrev);
6
1554
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. Someone told me to check if Visual Studio is using the gcc libraries. How do I do that? I found an option for Runtime Library that has options like Single-threaded, etc.
2
1736
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 (rb3, rb4, rb5) - again some textboxes (tb6, tb7)
4
2802
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 shift fields, and 1 shiftTotal field and 1 HoursTotal field. Datasheet may look like:
2
10970
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 3 to the right. The result would be B{6,7,8,1,2,3,4,5) Thanks! B
11
2201
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 (for an array a of elements size): for (i=0; i < size-1; i++) a=a a=newValue;
1
2075
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
2291
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 the DB the following happens: When Deleting a record: Fatal Error: Can't call method "fetchrow_arrayref" on an undefined value at GT::SQL::File::delete_records line 275. Stack Trace: GT::Base (2704): main::fatal called at...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10370
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10109
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.