Solved

Returning arrays

Posted on 2003-11-02
10
372 Views
Last Modified: 2010-05-18
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!
0
Comment
Question by:Tabris42
  • 6
  • 3
10 Comments
 
LVL 11

Expert Comment

by:dimitry
ID: 9667672
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9667698
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9667701
0
 

Author Comment

by:Tabris42
ID: 9667808
I'm just not getting the right numbers...
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9667842
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Tabris42
ID: 9667876
What have you got in the main section?
My numbers come out garbled and messy, like really long negative numbers.
0
 
LVL 9

Accepted Solution

by:
tinchos earned 500 total points
ID: 9667915
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
 
LVL 9

Expert Comment

by:tinchos
ID: 9667928
I hope this is what you need...........

Positive, neat output of the random numbers

Cheers

Tincho
0
 

Author Comment

by:Tabris42
ID: 9667980
Ok, silly error on my part, thank you!
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9667992
Ok, glad it helped
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now