944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1979
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 25th, 2007
0

Newbie Problem: Arrays

Expand Post »
I'm quite new to C++ and programming and general and I have a question about arrays. I realize this has been asked before but it has never been sufficiently explained, nor have I ever even been able to use the code given. I work with the standard C++ library and if somebody could give a sufficient explaination along with code examples I would very much appreciate it.

My problem is this: I have a function that will generate random non-repeating values and then store those values in a local array. How would it be possible to use references to, in essence, "return" that array and store it in an equally sized array in the calling function. Thank you, if I am not even asking the right question and you have an entirely different solution, please do tell =D.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ZachH26 is offline Offline
5 posts
since Jun 2007
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

Read-up the basics of arrays, function, function parameters.. then write a program and ask for help..
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

So does that mean I should show you the source code? I could show you but it will not compile and is completely wrong. I believe I've read "the basics." I'm not asking for actual source I just want to be pointed in the right direction. Should I, in fact, use a reference or am I off?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ZachH26 is offline Offline
5 posts
since Jun 2007
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

>> I'm not asking for actual source, I just want to be pointed in the right direction
That's good to know. Understandably the code you've written would be a good indication of which direction you're going in, so one can tell you whether to take a 180 degree turn or just 10 degree..
If you want "general reference/direction" I would say just search for words random and array in forums and you'll have plenty of directions to go in..
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

Pass the array into the function and load it, rather than trying to pass back a local array from the function.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

Thank you very much WaltP, that helps me tremendously. I'm not exactly sure how to implement what you mean as I'm so new, but you have put me in the right direction. Off to reading again! Thanks again.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ZachH26 is offline Offline
5 posts
since Jun 2007
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

Click to Expand / Collapse  Quote originally posted by ZachH26 ...
I'm not exactly sure how to implement what you mean as I'm so new, but you have put me in the right direction.
Read-up the basics of arrays, function, function parameters.. <<= That's the direction !
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

some code i wrote that basically does what you spoke of

C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <cstdlib>
  3. #define length(a) (sizeof a / sizeof a[0])
  4. void ArrayGenerator(int * MyArray, int ArraySize) {
  5. int lowest = 1,
  6. highest = 50,
  7. range = (highest - lowest) + 1;
  8. //act like we are generating random numbers
  9. for(int i = 0; i < ArraySize ; i++)
  10. {
  11. *(MyArray + i) = lowest + int(range * rand() / (RAND_MAX + 1.0));
  12. }
  13. }
  14.  
  15. int _tmain(int argc, _TCHAR* argv[])
  16. {
  17. int FillThisArray[10];
  18. int * fillArray = FillThisArray;
  19. ArrayGenerator( fillArray, length(FillThisArray) );
  20. return 0;
  21. }

this function takes a pointer to an array so i can write code and pass it an array and have it modify the original array and not have to worry about returning anything.


It's pretty neat and now i think im going to go to bed, it's 1:30 in the morning :-X

sorry if it's a bunked (what i said) as i am tired right now :-P
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

Thank you, that seems to make sense. I will play around with it a little bit and see what I can do with it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ZachH26 is offline Offline
5 posts
since Jun 2007
Jun 26th, 2007
0

Re: Newbie Problem: Arrays

now that im awake i will try to explain this some more lol!


C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <cstdlib>
  3. #define length(a) (sizeof a / sizeof a[0])
i've included the cstdlib to gain access to rand and RAND_MAX so i can generate some random numbers.

Also note the define, i have created this define to find the length of an array, since the sizeof returns the byte size of something and not the actual number of elements i need to get the actual byte size of an int and the the array and divide em out to get the total number of elements (or so i believe that is what is going on lol)

C++ Syntax (Toggle Plain Text)
  1. void ArrayGenerator(int * MyArray, int ArraySize) {
  2. int lowest = 1,
  3. highest = 50,
  4. range = (highest - lowest) + 1;

the lines lowest, highest, and range are used to generate random numbers within a certain area, i just happened to want numbers between 1 and 50, you could very easily modify the function to require arguments specifiying the range

C++ Syntax (Toggle Plain Text)
  1. //act like we are generating random numbers
  2. for(int i = 0; i < ArraySize ; i++)
  3. {
  4. *(MyArray + i) = lowest + int(range * rand() / (RAND_MAX + 1.0));
  5. }
  6. }

these lines are pretty simple, while im less than the array enter a random number into the array at the pointed too position

*(MyArray + i) states to go to the location pointed at plus whatever 'i' is.

C++ Syntax (Toggle Plain Text)
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3. int FillThisArray[10];
  4. int * fillArray = FillThisArray;
  5. ArrayGenerator( fillArray, length(FillThisArray) );
  6. return 0;
  7. }

first i initialize an array 10 in size, then i create an int pointer (*) to the array fillArray.

i call the fuction and pass it my pointer to the array and call the defined macro length to get the size of the array.

all is well that ends well right
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How do I remove topmost property
Next Thread in C++ Forum Timeline: cannot convert from 'Book *' to 'node *'





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC