A program needs to simulate 15 trials. During each trial, a game spinner with four possible options is spun 50 times. Which loop structure best represents the code needed to accomplish this task?

for(int s = 0; s < 50; s++)
for(int t = 0; t < 15; t++)
for(int s = 1; s < 50; s++)
for(int t = 1; t < 15; t++)
for(int t = 0; t < 15; t++)
for(int s = t; s < 50; s++)
for(int t = 0; t < 15; t++)
for(int s = 0; s < 50; s++)
for(int t = 1; t < 15; t++)
for(int s = 1; s < 50; s++)

Respuesta :

tonb

You create two nested loops like this:

for(int t = 0; t < 15; t++)

{

   for(int s = 0; s < 50; s++)

   {

   }

}

Loop variables typically run from 0 to < the end value, so it is excluded.

E.g., if you create a loop from 0 to 5, the loop variable takes the values 0,1,2,3 and 4 (not 5), but the loop is in fact executed 5 times. This is why software people like to start counting at 0.