Question
Practice on Selecting the Appropriate Loop: Scenario You go to an arcade with 20 tokens, and each game costs 2 tokens. What loop should you
Practice on Selecting the Appropriate Loop:
Scenario You go to an arcade with 20 tokens, and each game costs 2 tokens. What loop should you use, and why?
Scenario You go to an arcade with 100 tokens, and each game costs a variable number of tokens. What loop should you use, and why?
We will use one while loop as follows:
tokens = 100;
while(tokens > 0)
current_game = Choose_game();
tokens = tokens - Cost_of(current_game);
This while loop will iterate untill all the tokens are used.
Scenario You go to an all-you-can-eat buffet. You are trying to eat only until you are no longer hungry. What loop should you use, and why?
Scenario It is Saturday night, and you are watching the Lottery draw for Fantasy Five. You get inspired to write a program to generate 10 lotto tickets, each ticket has 5 Lotto numbers. What loop should you use, and why?
We will use two nested while loops as follows:
int ticket =1;
while(ticket <= 10)
Lotto_numbers = []
int n = 1;
int tmp = random_integer();
Lotto_numbers.append( tmp );
n = n + 1;
ticket = ticket + 1;
Outer While loop will iterate over tickets 1 to 10 and inner while loop will generate 5 Lotto numbers for each ticket.
Scenario You are playing a guessing game with your little cousin. She wants you to guess her favorite color. What loop should you use for that, and why?
We will use a for loop.
Colors = Array containing all color names;
for(int i = 0; i< Colors.length(); i++)
if(Colors[i] is favorite color )
We are done;
Write a program to demonstrate #1, & 5. These loops are required to be written in 1 program, worth 50 points per loop. Any additional ones are extra credit, each worth 25 points.
Just need help to finish this.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started