Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / A question about data management

Author
Message
Gary1234
13
Years of Service
User Offline
Joined: 22nd Sep 2011
Location:
Posted: 30th Oct 2011 17:27
I would like to write a program with the following functions:

1)
float array[Row][2]
//("Row" is entered by the user)

2)
void function(float array[Row][2])
//("array[Row][2]" will put into this function)
//(the function will calculate and change the value of the original array)(pass by reference?)
//(i.e. "array[m][2]" is updated by the function)

I have no ideal how to mondify the program,

the following code is just my thinking.....
(Sorry for poor english....and thankyou for your help!)




Would you please help me to modify it?

Thankyou@@!
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 31st Oct 2011 02:21
Change:

to:

You will also need to free the memory used simply:


I'm hoping I've got it right. I just typed it up and I'm up late...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 31st Oct 2011 12:37
Close WLGfx, it's
though.

Gary1234
13
Years of Service
User Offline
Joined: 22nd Sep 2011
Location:
Posted: 31st Oct 2011 18:10
@W@" I am afraid that i still don't understand....

(i don't know where should the

and

put....)

would you mind giving me an example?

or correct the the above code...?

Thank you very much!

Thankyou@@!
Gary1234
13
Years of Service
User Offline
Joined: 22nd Sep 2011
Location:
Posted: 5th Nov 2011 17:24
I have try to code with Dynamic Memory

here is the code:



and the System have the following Output...


I have no idea what is going wrong....Can anyone help me to solve it? Thank you~

Thankyou@@!
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 6th Nov 2011 09:37 Edited at: 6th Nov 2011 09:42
Here is a solution. I only slightly modified the code in your first post to make it work.



Note that "float *pa" is a "simple pointer", which you could use as a pointer to a one-dimensional array but not to a two-dimensional. To create a pointer to an array, you need to use the "float (*pa)[COLUMNS]" form. The brackets are necessary because "float *pa[COLUMNS]" would be an array of pointers and not a pointer to an array.

I also exchanged Columns for COLUMNS because constant names are usually written in uppercase but that's only a style question.

Variables are NOT automatically initialized to zero. If you create an array and immediately print out its values, without initializing the array, it will work but the values will be garbage. In a "real" program, make sure to always initialize your variables.

Finally, don't do this:



dbStr creates always a new string in memory when it is called, therefore it creates a memory leak if you don't delete[] the returned pointer after use. (By the way I'm not sure that dbInput does not have the same problem, so I would probably delete its returned pointer too when it's no longer needed.)

For printouts it is better to use the sprintf formatting function of C++, see the example above. I have also included an empty line printing after each row of the array, so that it's easier to see where a line ends. (You can try to further develop the program to print the values of one array row into one line on the screen, because the lines quickly scroll away and you won't be able to see most of the printouts.)
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 6th Nov 2011 10:03 Edited at: 6th Nov 2011 11:24
I forgot to mention that the second dimension must be included when you pass the array to a function, but you can see that in the example.

The syntax of handling multi-dimensional arrays is tricky if they are dynamic (I actually had to double-check this in a book), I'd probably try to avoid it when possible, although it's not so bad when the number of dimensions is only two and the second size is fixed.

Login to post a reply

Server time is: 2024-11-19 06:47:04
Your offset time is: 2024-11-19 06:47:04