| | |
Newbie Problem: Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2007
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
>> 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..
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.. ?
some code i wrote that basically does what you spoke of
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
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <cstdlib> #define length(a) (sizeof a / sizeof a[0]) void ArrayGenerator(int * MyArray, int ArraySize) { int lowest = 1, highest = 50, range = (highest - lowest) + 1; //act like we are generating random numbers for(int i = 0; i < ArraySize ; i++) { *(MyArray + i) = lowest + int(range * rand() / (RAND_MAX + 1.0)); } } int _tmain(int argc, _TCHAR* argv[]) { int FillThisArray[10]; int * fillArray = FillThisArray; ArrayGenerator( fillArray, length(FillThisArray) ); return 0; }
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!
now that im awake i will try to explain this some more lol! 
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)
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
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.
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

C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <cstdlib> #define length(a) (sizeof a / sizeof a[0])
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)
void ArrayGenerator(int * MyArray, int ArraySize) { int lowest = 1, highest = 50, 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)
//act like we are generating random numbers for(int i = 0; i < ArraySize ; i++) { *(MyArray + i) = lowest + int(range * rand() / (RAND_MAX + 1.0)); } }
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)
int _tmain(int argc, _TCHAR* argv[]) { int FillThisArray[10]; int * fillArray = FillThisArray; ArrayGenerator( fillArray, length(FillThisArray) ); return 0; }
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!
![]() |
Other Threads in the C++ Forum
- Previous Thread: How do I remove topmost property
- Next Thread: cannot convert from 'Book *' to 'node *'
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






