dear
I want to ask help on this problem.
Array a[N] is partitioned into a0[N/2] and a1[N/2] in main(). Then
a1[N/2] is partitioned into a2[N/4] and a3[N/4] in th_partition()
function. And I think this problem is something about parameter
passing.
If someone give me comment, it will be thankful.
thankyou very much
/*************** *************** *************** **/
#include <stdio.h>
void* th_partition(vo id*);
#define N 20 // number of elements
struct tag{
int *a0_temp; // temporary array
int *a1_temp;
int *a2_temp;
int *a3_temp;
int *a0_N; // number of elements for each array
int *a1_N;
int *a2_N;
int *a3_N;
}arrays;
int main(int argc, char* argv[])
{
int i,j,*pt;
int a[N];
// Initial array (N elements)
for(i=0;i<N;i++ )
a[i] = i;
pt = a;
/* partition of a[] ==> a0[] and a1[] */
/* j : position in the middle */
j = N / 2;
int tmp0[j];
int tmp1[N-j];
arrays.a0_N = (void*) (j);
arrays.a1_N = (void*) (N-j);
for(i=0;i<j;i++ )
tmp0[i]=a[i];
for(i=j;i<N;i++ )
tmp1[i-j]=a[i];
arrays.a0_temp = tmp0;
arrays.a1_temp = tmp1;
for(i=0;i<j;i++ )
printf(" %d ",arrays.a0_tem p[i]);
printf("\n");
for(i=0;i<N-j;i++)
printf(" %d ",arrays.a1_tem p[i]);
printf("\n");
th_partition(&a rrays);
// Test ===> strange !!
int test = (int)arrays.a2_ N;
for(i=0;i<test; i++)
printf(" %d ", arrays.a2_temp[i]);
printf("\n");
}
/* Partition of a1[] ==> a2[] and a3[] */
void* th_partition(vo id* parameters)
{
int i,j,M;
struct tag* para = (struct tag*)parameters ;
M = (int) para->a1_N;
j = M / 2;
int tmp2[j];
int tmp3[M-j];
para->a2_N = (void* )(j );
para->a3_N = (void* )(M-j);
for(i=0;i<j;i++ )
tmp2[i]=para->a1_temp[i];
for(i=j;i<M;i++ )
tmp3[i-j]=para->a1_temp[i];
para->a2_temp = tmp2;
for(i=0;i<j;i++ )
printf(" %d ",para->a2_temp[i]);
printf("\n");
para->a3_temp = tmp3;
for(i=0;i<M-j;i++)
printf(" %d ",para->a3_temp[i]);
printf(" \n ");
} 2 1627
Pasacco wrote: dear
I want to ask help on this problem. Array a[N] is partitioned into a0[N/2] and a1[N/2] in main(). Then a1[N/2] is partitioned into a2[N/4] and a3[N/4] in th_partition() function. And I think this problem is something about parameter passing.
Your problem is not about parameter passing, it is about
storage duration.
The variables declared within th_partition() live only until
the end of th_partition. So, accessing or trying to access
these temporary arrays after they ceased to exist invokes
undefined behaviour.
If you really want to double the information, you need
dynamic memory allocation.
If someone give me comment, it will be thankful.
thankyou very much
/*************** *************** *************** **/ #include <stdio.h>
For dynamic memory allocation, you need to
#include <stdlib.h> void* th_partition(vo id*);
#define N 20 // number of elements
struct tag{ int *a0_temp; // temporary array int *a1_temp; int *a2_temp; int *a3_temp;
int *a0_N; // number of elements for each array int *a1_N; int *a2_N; int *a3_N;
Your comment is misleading: These are addresses of numbers of
elements.
You probably want int a0_N; and so on.
}arrays;
int main(int argc, char* argv[]) { int i,j,*pt; int a[N];
// Initial array (N elements) for(i=0;i<N;i++ ) a[i] = i;
pt = a;
/* partition of a[] ==> a0[] and a1[] */ /* j : position in the middle */
j = N / 2;
int tmp0[j]; int tmp1[N-j];
Note: This works only in C99.
arrays.a0_N = (void*) (j); arrays.a1_N = (void*) (N-j);
This is complete crap. j and N-j are integer values which you
convert into void *, i.e. an address. Afterwards, they are implicitly
converted into int * (the type of .a0_N and .a1_N).
You want "int a0_N" and "arrays.a0_ N = j"... for(i=0;i<j;i++ ) tmp0[i]=a[i];
for(i=j;i<N;i++ ) tmp1[i-j]=a[i];
arrays.a0_temp = tmp0; arrays.a1_temp = tmp1;
for(i=0;i<j;i++ ) printf(" %d ",arrays.a0_tem p[i]); printf("\n");
for(i=0;i<N-j;i++) printf(" %d ",arrays.a1_tem p[i]); printf("\n"); th_partition(&a rrays);
You fail to check the return value of th_partition(). // Test ===> strange !! int test = (int)arrays.a2_ N; for(i=0;i<test; i++) printf(" %d ", arrays.a2_temp[i]); printf("\n");
With dynamic memory allocation, you would have to
free(arrays.a2_ temp);
free(arrays.a3_ temp);
It is better to
return 0; }
/* Partition of a1[] ==> a2[] and a3[] */ void* th_partition(vo id* parameters) { int i,j,M; struct tag* para = (struct tag*)parameters ;
M = (int) para->a1_N; j = M / 2;
int tmp2[j];
tmp2 lives from here till the end of the function
int tmp3[M-j];
tmp3 lives from here till the end of the function
Dynamic memory allocation would give you storage which lives
on until free()d:
int *tmp2, *tmp3;
tmp2 = malloc(j * sizeof *tmp2);
tmp3 = malloc((M-j) * sizeof *tmp3);
if (tmp2 == NULL || tmp3 == NULL) {
/* Handle error and return NULL or abort */
} para->a2_N = (void* )(j ); para->a3_N = (void* )(M-j);
As above: Crap. for(i=0;i<j;i++ ) tmp2[i]=para->a1_temp[i];
for(i=j;i<M;i++ ) tmp3[i-j]=para->a1_temp[i];
para->a2_temp = tmp2;
for(i=0;i<j;i++ ) printf(" %d ",para->a2_temp[i]); printf("\n");
para->a3_temp = tmp3;
for(i=0;i<M-j;i++) printf(" %d ",para->a3_temp[i]); printf(" \n ");
Your compiler should complain here:
You claim that you return void * but you don't. }
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
now i learn dynamic allocation and it works...
thankyou very much This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Judi Keplar |
last post by:
I am currently taking a course to learn Python and was looking for
some help. I need to write a Python statement to print a comma-
separated repetition of the word, "Spam", written 511 times ("Spam,
Spam, … Spam").
Can anybody help me get started? I am completely new to programming!
Thanks in advance!
|
by: Philip Mette |
last post by:
I am begginner at best so I hope someone that is better can help.
I have a stored procedure that updates a view that I wrote using 2
cursors.(Kind of a Inner Loop) I wrote it this way Because I couldn't
do it using reqular transact SQL.
The problem is that this procedure is taking longer and longer to run.
Up to 5 hours now! It is anaylizing about 30,000 records. I think
partly because we add new records every month.
The procedure...
|
by: Grrrbau |
last post by:
I'm a beginner.
I'm looking for a good C++ book.
Someone told me about Lafore's "Object-Oriented Programming in C++".
What do you think?
Grrrbau
|
by: BobJohnson |
last post by:
Just started learning C++ and I need some help with my homework, shouldn't
take long for people around here. I need to create a simple money
calculator but I don't know how to make the output numbers two decimal
places long like 10.01 I only know how to define numbers as int or double.
Do I use float?
Also, I'm using Visual Studio .NET is there anyway to keep the compiler on
the screen long enough to actually see what it's outputting. ...
|
by: ranger |
last post by:
Hi,
I'm a beginner with C++, studying on my own from a book and I've
encoutered quite some problems.. If possible, someone out there might
help me out..
The problem is the following.. I've tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I'm just a beginner in C++, with noone
around me to help.. I'd appreciate if someone might help me out!
| |
by: Bshealey786 |
last post by:
Okay im doing my final project for my first computer science class(its
my major, so it will be my first of many), but anyway im a beginner so
im not to great with C++ yet. Anyway this is the error msg that im
getting: "Error executing cl.exe"
this is the code that I have, but I know whats causing it, ill just
show you the whole thing first though
//file: Quadratic
|
by: hl |
last post by:
Hi,
I'm a beginner and need a little help with getting data back from a web
service.
I am using VB.Net and have added a web reference to a Wsdl that was
provided to me.
My reference.vb file that was generated has the following code at the end.
<System.Xml.Serialization.SoapTypeAttribute("Map",
|
by: vitalia |
last post by:
Hello everyone, I really need help to create this syntax.
I am a beginner. I was able to write just simple code but I couldn't restrict the number of digits and letters.
What I need is:
All inputs should be verified. Any input which fails the verification should result in a suitable error message.
Customer Reference – 4 digits only
Title- between1 and 4 alphabetic characters(allowed Dr, Lady, Lord, Miss, Mr,Mrs,Ms,Sir.
Surname-...
|
by: macmac |
last post by:
I'm new to this forum and I am in a beginner level with programming too. I posted this thing out hoping that somebody could help me out in making this program. I am working right now as a C++ developer well of course as a junior developer, I know fundamentals of C++ and a little knowledge on classes, I also have a little knowledge in networking. My problem right now is that the company is urging me to deliver a bot for D2 game, specifically a...
|
by: harky |
last post by:
Hi
I need some serious help!!
I am a beginner with very little knowledge of access
I am looking to get a database to tell me who has not payed their bill, they have 30 days to pay it. if possible this could be a pop up alert telling me who hasnt paid. Then I need to send a email to that Id(client) asking them to settle the outstanding bill.
Thanks claire
|
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,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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();...
|
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...
| |