I've got something like this:
static int n = 0; // global static int int *arr = new int[n]; // global int* int randToArray(int arr[], int min, int max) { srand(time(NULL)); for(int i = 0; i <= n; i++) { arr[i] = (rand() % max + min); } } void printArray() { if(n) { for(int i = 0; i < n; i++) cout << arr[i] << endl; } else cout << "The array hasn't been drawed yet."; } And then in the main function I have a menu with a switch and options for getting random numbers and printing the array:
switch(option) { case 1: cout << "Set the size of the array: "; cin >> n; randToArray(arr, 1, 99); break; case 2: printArray(); wait(); break; } I need my array to be available globally in order to use in several other functions.
Everything works except one thing: when I want to print the array I get working properly only 8 first elements. Then the terminal shows up some very large numbers.
i < nas your array is index from 0 to n-1.