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.

Read-up the basics of arrays, function, function parameters.. then write a program and ask for help..

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?

>> 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..

Pass the array into the function and load it, rather than trying to pass back a local array from the function.

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.

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 !

some code i wrote that basically does what you spoke of

#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

Thank you, that seems to make sense. I will play around with it a little bit and see what I can do with it.

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

#include "stdafx.h"
#include <cstdlib>
#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)

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

//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.

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 :)

Thank you Killer Typo, your explanation was very well done and has been the first practical example of a pointer that I have seen in my learning experience. Now that I understand the syntax and implementation of how to use the pointer to full effect I think I can learn from it. Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.