Question

In: Computer Science

Write a switch statement that uses the colour of a swab sample of COVID testing vehicle...

Write a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Use the messages given for each colour in the table below. Justify your answer.                                                                                        

Colour

Message

Blue

“No virus”

Yellow

“Needs to be under observation”

Red

“Needs to be admitted in COVID ward”

Solutions

Expert Solution

The question is asked to implement a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Now, switch statement is a special statement in C, which can be used to check different values and based on the checking, corresponding case will be executed. The algorithm to implement the switch statement is given below:


Step 1: Get the sample as input to the program. Also define a function sendMessage() that takes a string as an argument to send it to a local doctor.

Step 2: Write a switch case that will take the argument as the sample. Now implement different cases.

Step 3: If the argument is "Blue", call the function sendMessage( "No virus" ).

Step 4: If the argument is "Yellow", call the function sendMessage( "Needs to be under observation" ).

Step 5: If the argument is "Red", call the function sendMessage( "Needs to be admitted in COVID ward" ).

Step 6: In default case, send "Invalid swab sample or invalid test" to the local doctor or to the laboratory.


If the function sendMessage() is defined to send a message to the doctor, then the switch case implementation in C++/C would be:

switch(sample)
{
   case "Blue":       //in case the sample is blue
       sendMessage("No virus\n");
       break;
   case "Yellow":       //in case the sample is yellow
       sendMessage("Needs to be under observation\n");
       break;
   case "Red":           //in case the sample is red
       sendMessage("Needs to be admitted to COVID ward\n");
       break;
   default:
       sendMessage("Invalid swab sample or invalid test\n");
}


Analysis: At first, the program will get the swab sample from the user. Now this sample variable is passed to the argument of the switch statement. When the sample is Blue, corresponding message will be passed, when it is yellow, corresponding message will be passed and so on. The message is passed using a function sendMessage() that sends the message to a local doctor. After each case, the break statement must be there in order to avoid the other cases. There is also a default case (does not come under any other defined case), that sends a message about the invalid sample or invalid test to the doctor.


Related Solutions

Write a switch statement that uses the colour of a swab sample of COVID testing vehicle...
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Use the messages given for each colour in the table below. Justify your answer.                                                                                         Colour Message Blue “No virus” Yellow “Needs to be under observation” Red “Needs to be admitted in COVID ward”
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle...
Write a switch statement that uses the colour of a swab sample of COVID testing vehicle to send a message to a local doctor. Use the messages given for each colour in the table below. Justify your answer.                                                                                         Colour Message Blue “No virus” Yellow “Needs to be under observation” Red “Needs to be admitted in COVID ward”
In the main(), below, write a program that uses a switch-statement to choose between three numbers....
In the main(), below, write a program that uses a switch-statement to choose between three numbers. The number, choice, should be prompted for and each choice should print out a unique statement. Also, the prompt should continue indefinitely (i.e. keep prompting until the user force-quits the program), and there should be a general catch-all if the user enters other than the three possible choices. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0;...
1.C++ uses the_______________symbol to represent the AND operator. 2.The switch statement uses the value of a(n)...
1.C++ uses the_______________symbol to represent the AND operator. 2.The switch statement uses the value of a(n) _____________expression to determine which group of statements to branch through. 3.C++ allows the programmer to compare numeric values using ________________________ 4.The local t-shirt shop sells shirts that retail for $12. Quantity dis-counts are given as follow: Number of Shirts Discount 5–10                    10% 11–20                   15% 21–30                   20% 31 or more           25% Write a program that prompts the user for the number of shirts required and...
In a sample of 5000 people, 2% of the population have COVID. In testing, there is...
In a sample of 5000 people, 2% of the population have COVID. In testing, there is a false positive rate of 1.6% and a false negative rate of 4%. a. Compile the results in a chart or a tree diagram. b. In this sample, how many people have COVID? c. What percent of the population have a positive test for COVID? d. What is the probability that a patient has COVID given a positive test? e. What is the probability...
Use a switch statement to write a function that returns TRUE if a character is a...
Use a switch statement to write a function that returns TRUE if a character is a consonant and returns FALSE otherwise.
Have a look at the code below. Write a switch statement to check for the mode...
Have a look at the code below. Write a switch statement to check for the mode type. When the designated mode is matched, display an appropriate string for that mode. The default case should display the statement, “We do not offer this mode of course delivery.” public class SwitchCourse {     enum CourseMode { ONLINE, RESIDENTIAL, HYBRID }     CourseMode myCourse = CourseMode.HYBRID;        public void myCourse()     {         switch (/*___*/)         {             case ONLINE:                 System.out.println("You...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
WRITTEN IN C PLEASE. Write a switch statement that tests the value of the char variable...
WRITTEN IN C PLEASE. Write a switch statement that tests the value of the char variable response and performs the following actions: if response is y, the message Your request is being processed is printed if response is n, the message Thank you anyway for your consideration is printed if response is h, the message Sorry, no help is currently available is printed for any other value of response, the message Invalid entry; please try again is printed
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT