Newbie Problem: Arrays

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 5
Reputation: ZachH26 is an unknown quantity at this point 
Solved Threads: 0
ZachH26 ZachH26 is offline Offline
Newbie Poster

Newbie Problem: Arrays

 
0
  #1
Jun 25th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Newbie Problem: Arrays

 
0
  #2
Jun 26th, 2007
Read-up the basics of arrays, function, function parameters.. then write a program and ask for help..
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 5
Reputation: ZachH26 is an unknown quantity at this point 
Solved Threads: 0
ZachH26 ZachH26 is offline Offline
Newbie Poster

Re: Newbie Problem: Arrays

 
0
  #3
Jun 26th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Newbie Problem: Arrays

 
0
  #4
Jun 26th, 2007
>> 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..
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Newbie Problem: Arrays

 
0
  #5
Jun 26th, 2007
Pass the array into the function and load it, rather than trying to pass back a local array from the function.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 5
Reputation: ZachH26 is an unknown quantity at this point 
Solved Threads: 0
ZachH26 ZachH26 is offline Offline
Newbie Poster

Re: Newbie Problem: Arrays

 
0
  #6
Jun 26th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Newbie Problem: Arrays

 
0
  #7
Jun 26th, 2007
Originally Posted by ZachH26 View Post
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 !
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Newbie Problem: Arrays

 
0
  #8
Jun 26th, 2007
some code i wrote that basically does what you spoke of

  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
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 5
Reputation: ZachH26 is an unknown quantity at this point 
Solved Threads: 0
ZachH26 ZachH26 is offline Offline
Newbie Poster

Re: Newbie Problem: Arrays

 
0
  #9
Jun 26th, 2007
Thank you, that seems to make sense. I will play around with it a little bit and see what I can do with it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 759
Reputation: Killer_Typo will become famous soon enough Killer_Typo will become famous soon enough 
Solved Threads: 35
Killer_Typo's Avatar
Killer_Typo Killer_Typo is offline Offline
Master Poster

Re: Newbie Problem: Arrays

 
0
  #10
Jun 26th, 2007
now that im awake i will try to explain this some more lol!


  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)

  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

  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.

  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
Dont forget to spread the reputation to those that deserve!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC