dimanche 23 septembre 2018

Why this program works without functions but with them it doesn't?

This generate well slots in the array (eg. arr[0] =753, [1] = 324, [2] = 876)

int main(void){
// Introducing the 'n' number, the max number in the array
int num;
cout << "Introduce the quantity of random numbers between 100 and 999\nNumber: "; cin >> num;
int arr[num];

// Generating random numbers for the i's slots in the array
srand(time(0));

// Filling the array
for(int i = 0; i < num; i++)
    arr[i] = (rand() % 900) + 100;

// Printing the array
for(int i = 0; i < num; i++)
    printf("item [%i] = %i\n", i, arr[i]);}

But this one generates things like arr[0] = 9, [1] = 425345, [2] = 0, [3] = 324, [4] = 48, [5] = 232424 But just when I put a n >= 8, from 1 to 7 works, but with 8 or higher it doesn't work

void randomArray(int size)
{
int arr[size];

// Generating random numbers for the i's slots in the array
srand(time(0));

for(int i = 0; i < size; i++)
    arr[i] = (rand() % 900) + 100;

}

void printArray(int size)
{
int arr[size];

for(int i = 0; i < size; i++)
    printf("item [%i] = %i\n", i, arr[i]);
}

int main(void)
{
// Introducing the 'n' number, the max number in the array
int num;
cout << "Introduce the quantity of random numbers between 100 and 999\nNumber: "; cin >> num;
int arr[num];


srand(time(0));

// Filling the array
randomArray(num);

// Printing the array
printArray(num);}




Aucun commentaire:

Enregistrer un commentaire