Here is some additional practice with switch statements.
You’re a time traveler with a computer, but it happens to have very limited capabilities thanks to the Time Police. You also happen to be a history buff that loves the Roman Empire, so you traveled back in time with your silly little programmable computer to the silly little Roman Empire. You’ve decided to teach the Romans modern Arabic numbers rather than their weird letter numbers. To expedite the teaching process, you want to make a program that can convert modern integers into Roman numerals up to 10. Write a program that uses a switch statement to convert integers up to 10 into Roman numerals. Hint: Remember, the Romans didn’t have a number for 0, and they would probably like to be prompted for an input. You would also like the program to stay running rather than restarting for every input. The Time Police Limited Computer (TTPLC) only allows one program to be run a month. Thanks, Time Police.
Answer:
#include <stdio.h>
int main(void)
{
int number;
do
{
("Press 111 to exit.\n");
printf("What number would you like to convert?\n");
printf("%d", &number);
scanf
switch(number)
{
case 1:
("%d as a Roman numeral would be I.\n", number);
printfbreak;
case 2:
("%d as a Roman numeral would be II.\n", number);
printfbreak;
case 3:
("%d as a Roman numeral would be III.\n", number);
printfbreak;
case 4:
("%d as a Roman numeral would be VI.\n", number);
printfbreak;
case 5:
("%d as a Roman numeral would be V.\n", number);
printfbreak;
case 6:
("%d as a Roman numeral would be VI.\n", number);
printfbreak;
case 7:
("%d as a Roman numeral would be VII.\n", number);
printfbreak;
case 8:
("%d as a Roman numeral would be VIII.\n", number);
printfbreak;
case 9:
("%d as a Roman numeral would be IX.\n", number);
printfbreak;
case 10:
("%d as a Roman numeral would be X.\n", number);
printfbreak;
case 111:
return 0;
default:
("That was not a valid entry. Try again with a number between 1 and 10, or the exit value.\n");
printfbreak;
}
} while(number != 111);
}
You have returned from your time traveling adventure and are back to working your [generic service job]. The Romans appreciated the number system lesson, but they promptly dropped it because it only went up to 10. Time paradox avoided.
There has been a recent problem with mobile orders. You decided to take a look at the mobile order system to locate the issue. You located where orders are differentiated in the code. You have the menu for reference. Is there an issue in the code? If there is, what is it?
/*
+-------------------------------------------+
| 1. Chicken Sandwich |
| 2. Cheddar Broccoli Bread Bowl |
| 3. Ham Egg and Cheese Breakfast Burrito |
| 4. Vegan Crunch Wrap |
| 5. Bacon Egg and Salsa Breakfast Bagel |
| 6. Turkey Super Sandwich |
| 7. Avocado Toast |
| 8. Cheddar Bacon Ham Croissantwich |
+-------------------------------------------+
*/
...
switch(order)
{
case 1:
total = total + 4.50;
break;
case 2:
total = total + 5.00;
break;
case 3:
total = total + 3.79;
break;
case 4:
total = total + 3.00;
break;
case 5:
total = total + 4.00;
case 6:
total = total + 7.85;
break;
case 7:
total = total + 4.29;
break;
case 8:
total = total + 3.35;
}
...
Answer: Yes. The switch case on line 30 does not
have a break
in it.
Upper management was very happy with your help finding the mobile order issue in the system. So happy, in fact, that you ended up having a small article in the company newsletter. You were elated to be recognized for your good work.
You received an email the evening of the release of the newsletter from an anonymous address.
From:
redactedSubject: RECRUITMENT CHALLENGE
Greetings.
We are very aware of your recent programming accomplishments. We have decided to extend an opportunity of employment to you. Successful completion our challenge will be taken as an acceptance of the offer. It is advised to look up an ASCII table. Best of luck.
Attached to the email was a code snippet and the challenge. You have been tasked with predicting the output of the code snippet given specific inputs. The input list will be separated by commas.
5, 109
82, 105
15, 105
66, 116
0, 104
Note: \0
is the NULL
character.
int option;
int subOption;
...
switch(option)
{
case 5:
("Hello, World!");
printfcase ' ' + 'z':
("?");
printfbreak;
case 78:
("Nintendo Switch");
printfbreak;
case 15:
("Switch Statements.");
printfcase 'B':
switch(subOption)
{
case 'i':
("Alphabet Soup");
printfbreak;
case 'm':
("Sonder by");
printfcase 'e':
(" The Wrecks");
printfbreak;
case 't':
("Break ");
printfbreak;
}
case 1:
("check!");
printfbreak;
case 27 + 54:
default:
("Fallout 76");
printfbreak;
case 'Q' + 1:
("Baby ");
printfcase 3:
("Shark");
printfbreak;
case 291:
("Croissantwich? What?");
printfbreak;
case 20 * '\0':
break;
}
...
Answer: Hello, World!? Baby Shark Switch Statements.Alphabet Soupcheck! Break check! [NO OUTPUT]
You, a master of switch statements, successfully analyzed the code and sent your answer as a reply and went to bed. The following morning, you had another email, this time from the Time Police, congratulating you on your successful completion of the challenge and your new position as a Time Police programmer. You graciously accept the offer, as they offer great benefits, good pay, and the option to work remotely or in person.
Your first project is to program their phone system. There are several requirements, but they give them to you in a very handy list.
- There will be six options. 1 - 5 and 0 for operator.
- There should not be a prompt to press 0, as it is for internal use only.
- The first option should be for time emergencies and have no sub options.
- Options 2 - 5 should be for time travel requests, time ban appeals, time fine payment, and human resources.
- Each option should have four or five sub-options, including an unprompted 0 option.
- Human resources should have 3 sub-options, including the 0 option.
- For the main menu, the default case should be for inputs not on the list (6 - 9).
- The 0 option should take the user to the front desk of the menu it is on.
They provided an example to give you a general idea of the expected prompts:
#include <stdio.h>
int main(void)
{
...
("Press 1 for Time Emergencies. Press 2 for...");
printf("%d", &option);
scanf
switch(option)
{
case 1:
("Connecting you to Time Police operator.");
printfbreak;
case 2:
("Press 1 for SUBEXAMPLE 1. Press 2 for...");
printf("%d", &subOption);
scanf
("Connecting you to ");
printfswitch(subOption)
{
case 1:
("SUBEXAMPLE 1.");
printfbreak;
...
}
break;
case -1:
break;
}
return 0;
}
As you may notice, -1 is the terminator value. This will also not be prompted, as that isn’t an option on a phone. That is our placeholder for hanging up. It isn’t required to put a -1 case in each nested switch, but you may. They will not count towards the total sub-options, but they will be included in the solution.
The sub-options are up to you, as this is intended as practice with nested switch statements. You decide what the Time Police handle, but maintain the “connecting you to” standard. Remember, those are the expected prompts.
Answer:
#include <stdio.h>
int main(void)
{
int option;
int subOption;
("Press 1 for Time Emergencies.\n");
printf("Press 2 for time travel requests.\n");
printf("Press 3 for time ban appeals.\n");
printf("Press 4 for time fine payment.\n");
printf("Press 5 for human resources.\n");
printf("%d", &option);
scanf
switch(option)
{
case 1:
("Connecting you to Time Police operator.\n");
printfbreak;
// Time Travel Requests
case 2:
("Time Travel Requests\n\n");
printf("Press 1 for 10000 BCE - 5000 BCE.\n");
printf("Press 2 for 4999 BCE - 0 CE.\n");
printf("Press 3 for 1 CE - 1500 CE.\n");
printf("Press 4 for 1501 CE - Present.\n");
printf("%d", &subOption);
scanf
("Connecting you to");
printfswitch(subOption)
{
case 1:
(" Ancient BCE Time Travel.\n");
printfbreak;
case 2:
(" Recent BCE Time Travel.\n");
printfbreak;
case 3:
(" Distant CE Time Travel.\n");
printfbreak;
case 4:
(" Recent CE Time Travel.\n");
printfbreak;
case 0:
(" Time Travel front desk.\n");
printfbreak;
case -1:
break;
}
break;
//Time Ban Appeals
case 3:
("Time Ban Appeals\n\n");
printf("Press 1 for Minor Appeals.\n");
printf("Press 2 for Moderate Appeals.\n");
printf("Press 3 for Timeline-Altering Appeals.\n");
printf("%d", &subOption);
scanf
("Connecting you to");
printfswitch(subOption)
{
case 1:
(" Minor Appeals.\n");
printfbreak;
case 2:
(" Moderate Appeals.\n");
printfbreak;
case 3:
(" Timeline-Altering Appeals.\n");
printfbreak;
case 0:
(" Time Ban Appeals front desk.\n");
printfbreak;
case -1:
break;
}
break;
//Time Fine Payment
case 4:
("Time Fine Payment\n\n");
printf("Press 1 for Fine Balance.\n");
printf("Press 2 for Billing.\n");
printf("Press 3 for Payment Plans.\n");
printf("%d", &subOption);
scanf
("Connecting you to");
printfswitch(subOption)
{
case 1:
(" Time Accounts office.\n");
printfbreak;
case 2:
(" Billing office.\n");
printfbreak;
case 3:
(" Payment office.\n");
printfbreak;
case 0:
(" Business office front desk.\n");
printfbreak;
case -1:
break;
}
break;
// Human Resources
case 5:
("Human Resources\n\n");
printf("Press 1 for Application Status.\n");
printf("Press 2 for Openings.\n");
printf("%d", &subOption);
scanf
("Connecting you to");
printfswitch(subOption)
{
case 1:
(" Application office\n");
printfbreak;
case 2:
(" Employment Opportunity Directory.\n");
printfbreak;
case 0:
(" Human Resources front desk.");
printfbreak;
case -1:
break;
}
break;
case 0:
("Connecting you to Time Police front desk.\n");
printfbreak;
// Hang up
case -1:
break;
default:
("That was not one of the options.\n");
printfbreak;
}
return 0;
}