Looks like my previous answer was a bit terse…
cout << results[rand()%(sizeof(results)/sizeof(results[0]))] << endl;
could be broken down to
int num_elems = sizeof(results)/sizeof(results[0]);
int randomIndex = rand() % num_elems;
int randomValue = results[randomIndex];
cout << randomValue << endl;
In slightly more detail:
sizeof(results)/sizeof(results[0])
calculates the number of elements in the arrayresults
. (Note that this only works when you have access to the array definition – you can’t use this with a pointer to an array.)rand() % num_elems
calculates a random number in the range [0..num_elems-1]; this is the range of theresults
array.results[randomIndex]
returns a single, randomly selected, element fromresults
solved Can someone explain this line of c++? [closed]