Tabris42
asked on
Returning arrays
I've asked a question like this before, but got some very quirky answers. Thought I'd try it again and be a little more specific.
Below is my function to return an array from the randomgenerator function to say, main. I've gotten some good answers to this question, but they all leave me quite confused and dismayed, because none of my numbers quite remain the same. Here is the function:
int randomgenerator()
{
int limit = 1000; int max= 9999; int i; int randentry;
int randomnumbers[300];
printf("Enter a seed for random generation: ");
scanf("%d", &randentry);
if (randentry == 0){
randentry == time(0);
}
printf("\n");
srand(randentry);
for(i = 0;i < 300; i++){
randomnumbers[i] = (rand() % (max - limit)) + limit;
}
printf("\n");
system("PAUSE");
return 0;
}
Basically, I want to print that array in main, using code more or less like this if possible:
int main(){
for(i = 0;i < 300; i++){
printf("%d", randomnumbers[i]);
if (i%14){
printf(" ");
}else{
printf("\n");
}
}
}
Obviously pointers are the way to go with a problem like this, but I'm very confused by them, and just one working example would definitely clear up a lot of questions I have about them. Thanks!
Below is my function to return an array from the randomgenerator function to say, main. I've gotten some good answers to this question, but they all leave me quite confused and dismayed, because none of my numbers quite remain the same. Here is the function:
int randomgenerator()
{
int limit = 1000; int max= 9999; int i; int randentry;
int randomnumbers[300];
printf("Enter a seed for random generation: ");
scanf("%d", &randentry);
if (randentry == 0){
randentry == time(0);
}
printf("\n");
srand(randentry);
for(i = 0;i < 300; i++){
randomnumbers[i] = (rand() % (max - limit)) + limit;
}
printf("\n");
system("PAUSE");
return 0;
}
Basically, I want to print that array in main, using code more or less like this if possible:
int main(){
for(i = 0;i < 300; i++){
printf("%d", randomnumbers[i]);
if (i%14){
printf(" ");
}else{
printf("\n");
}
}
}
Obviously pointers are the way to go with a problem like this, but I'm very confused by them, and just one working example would definitely clear up a lot of questions I have about them. Thanks!
Tabris42
Try again
I posted the solution in the other link.................
just so that you have it also here
The problem is the following
When you assign the values to the vector, then, you continue printing the pointer from the end of the vector, so you're printing places where the data has not been assigned..........
for(i = 0;i < cantElements; randomnumbers++, i++){
*randomnumbers = (rand() % (max - limit)) + limit;
}
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
Here, as you don't reassign randomnumbers, you will print all the values stored after the end of the data stored
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////
for(i = 0;i < cantElements; randomnumbers++, i++){
printf("%d", *randomnumbers );
if (i%14){
printf(" ");
}else{
printf("\n");
}
}
Do as the following
void randomgenerator( int * randomnumbers, int cantElements )
{
int limit = 1000; int max= 9999; int i; int randentry;
int * tempPointer;
printf("Enter a seed for random generation: ");
scanf("%d", &randentry);
if (randentry == 0){
randentry = time(0);
}
printf("\n");
srand(randentry);
// Assign the pointer to use with the original origin of the vector
tempPointer = randomnumbers;
for(i = 0;i < cantElements; tempPointer++, i++){
*tempPointer = (rand() % (max - limit)) + limit;
}
// Reassign the pointer to use with the original origin of the vector
tempPointer = randomnumbers;
for(i = 0;i < cantElements; tempPointer++, i++){
printf("%d", *tempPointer );
if (i%14){
printf(" ");
}else{
printf("\n");
}
}
printf("\n");
system("PAUSE");
}
I tested this and works just fine
Cheers
Tincho
Try again
I posted the solution in the other link.................
just so that you have it also here
The problem is the following
When you assign the values to the vector, then, you continue printing the pointer from the end of the vector, so you're printing places where the data has not been assigned..........
for(i = 0;i < cantElements; randomnumbers++, i++){
*randomnumbers = (rand() % (max - limit)) + limit;
}
//////////////////////////
Here, as you don't reassign randomnumbers, you will print all the values stored after the end of the data stored
//////////////////////////
for(i = 0;i < cantElements; randomnumbers++, i++){
printf("%d", *randomnumbers );
if (i%14){
printf(" ");
}else{
printf("\n");
}
}
Do as the following
void randomgenerator( int * randomnumbers, int cantElements )
{
int limit = 1000; int max= 9999; int i; int randentry;
int * tempPointer;
printf("Enter a seed for random generation: ");
scanf("%d", &randentry);
if (randentry == 0){
randentry = time(0);
}
printf("\n");
srand(randentry);
// Assign the pointer to use with the original origin of the vector
tempPointer = randomnumbers;
for(i = 0;i < cantElements; tempPointer++, i++){
*tempPointer = (rand() % (max - limit)) + limit;
}
// Reassign the pointer to use with the original origin of the vector
tempPointer = randomnumbers;
for(i = 0;i < cantElements; tempPointer++, i++){
printf("%d", *tempPointer );
if (i%14){
printf(" ");
}else{
printf("\n");
}
}
printf("\n");
system("PAUSE");
}
I tested this and works just fine
Cheers
Tincho
ASKER
I'm just not getting the right numbers...
Sorry tabris, but please recheck your code
With the code i posted, it printed the random numbers in the function
and the same numbers in the main function
What do you mean by not getting the right numbers?
Tincho
With the code i posted, it printed the random numbers in the function
and the same numbers in the main function
What do you mean by not getting the right numbers?
Tincho
ASKER
What have you got in the main section?
My numbers come out garbled and messy, like really long negative numbers.
My numbers come out garbled and messy, like really long negative numbers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I hope this is what you need...........
Positive, neat output of the random numbers
Cheers
Tincho
Positive, neat output of the random numbers
Cheers
Tincho
ASKER
Ok, silly error on my part, thank you!
Ok, glad it helped
int randomnumbers[300];
int randomgenerator()
{
...
}
int main()
{
...
}
Data is divided into several types from "visibility" point of view. Most common are "local" and "global".
"Global" can be "seen" or used by any function, but local can be used only within the function it declared.
Also it is possible to transfer data from function to function.
Therefore to access randomnumbers from main() you have two major options:
1) Make randomnumbers array as global
2) Return its pointer to main() in randomgenerator() call
Last method is much more popular than global data.
It can be something like this:
int randomgenerator( int *randomnumbers )
{
...
}
int main()
{
int randomnumbers[300];
randomgenerator( randomnumbers );
...
}