162k views
2 votes
The Beaufort Wind Scale is used to characterize the strength of winds. The scale uses integer values and goes from a force of 0, which is no wind, up to 12, which is a hurricane. The following script first generates a random force value. Then, it prints a message regarding what type of wind that force represents, using a switch statement. You are to rewrite this switch statement as one nested 150 CHAPTER 4: Selection Statements if-else statement that accomplishes exactly the same thing. You may use else and/or elseif clauses.

ranforce = randi([0, 12]); switch ranforce case 0 disp('There is no wind') case {1,2,3,4,5,6} disp('There is a breeze') case {7,8,9} disp('This is a gale') case {10,11} disp('It is a storm') case 12 disp('Hello, Hurricane!') end

1 Answer

5 votes

Answer:

ranforce = randi([0, 12]);

if (ranforce == 0)

disp('There is no wind')

else if(ranforce>0 && ranforce <7)

disp('There is a breeze')

else if(ranforce>6 && ranforce <10)

disp('This is a gale')

else if(ranforce>9 && ranforce <12)

disp('It is a storm')

else if(ranforce==12)

disp('Hello, Hurricane!')

end

Step-by-step explanation:

Replace all switch case statements with if and else if statements.

An instance is:

case {7,8,9}

is replaced with

else if(ranforce>9 && ranforce <12)

All other disp statements remain unchanged

User Tom Leys
by
4.7k points