• C

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!
Tabris42Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dimitryCommented:
The simplest way is to make this array global:

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 );
...
}



0
tinchosCommented:
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
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

Tabris42Author Commented:
I'm just not getting the right numbers...
0
tinchosCommented:
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
0
Tabris42Author Commented:
What have you got in the main section?
My numbers come out garbled and messy, like really long negative numbers.
0
tinchosCommented:
I just used the code I posted before..............

Try this

#include <iostream>
#include <string>
#include <stdlib.h>

#include <time.h>

using namespace std;


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

   tempPointer = randomnumbers;
   for(i = 0;i < cantElements; tempPointer++, i++){
        *tempPointer = (rand() % (max - limit)) + limit;
   }

   tempPointer = randomnumbers;
   for(i = 0;i < cantElements; tempPointer++, i++){
        printf("%d", *tempPointer );
        if (i%14){
            printf(" ");
        }else{
            printf("\n");
        }
   }
    printf("\n");
    system("PAUSE");
}




int main()

{
      int vector[30];

      randomgenerator( vector, 30 );

                system("PAUSE");

      return 0;
}

And the output will be the following

1041
1469 7334 9502 2171 7725 3479 3361 9964 7466 6705 2148 6283 8828 1962
1491 3995 3943 5827 6436 6394 6605 4902 1153 1292 4383 9422 1718 2720
2897
Presione una tecla para continuar . . .

Hope this helps

Tincho
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tinchosCommented:
I hope this is what you need...........

Positive, neat output of the random numbers

Cheers

Tincho
0
Tabris42Author Commented:
Ok, silly error on my part, thank you!
0
tinchosCommented:
Ok, glad it helped
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.