Solved

Returning arrays

Posted on 2003-11-02
10
374 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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
 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to set environment variables in C 2 82
Want to delete all my personal data 13 145
List out all word 7 286
valid enum? 6 74
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

813 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

16 Experts available now in Live!

Get 1:1 Help Now