dimanche 11 août 2019

How to store Generated number from 1 to 500 in two Array

I have generate random number from 1 to 500 and want to store in 2 arrays says array1(store 1 to 250 elements) and array2(store 251 to 500 elements). Size of Both array is 250. I successfully store in array1 but not in array2.

I tried but the loop goes on Infinite loop

#define MAX 250
int a1[MAX], a2[MAX];
srand((int)time(0));

for (i = 0; i < 500; i++) {
    int c = (rand() % 500) + 1;
    if (i >= 0 && i < 250) {
        cout << "Array 1 ";
        for (j = 0; j < 250; j++) {
            a1[j] = c;
            cout << a1[j] << " ";
        }
    }
    if (i >= 250 && i < 500) {
        cout << endl
             << "Array 2 ";
        for (int k = 0; k < 250; k++) {
            a2[k] = c;
            cout << a2[k] << " ";
        }
    }




Aucun commentaire:

Enregistrer un commentaire