Question

In: Computer Science

21. What is the output of the following segment of code if 7 is input by...

21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout << total << endl;

Solutions

Expert Solution

The output of the given segment of code when 7 is entered as the input by the user is 13.

Switch statement:
A switch statement allows us to select an option out of several options present based on the choice.

Syntax:
switch(choice/expression)
{
case value1: block-1; break;
case value2: block-2;  break;
.
.
//Any number of case statements is allowed.
default: block-d;
}

The choice can either be an integer value, character or an expression which evaluates to an integer value. Based on this value, the control is transferred to particular case value. If the value of the choice does not match any of the case values, then the statements in the default block will be executed, if default case is not specified then control comes out of the switch statement.
During execution, if the break statement is executed, then the control comes out of the switch statement and the statements after the switch statement is executed. If there is no break statement then the next cases are executed until a break is encountered or till the end of switch case block.

Given code explanation:
int num; // variable declaration
int total = 0;  // variable declaration and initialization
cout << "Enter a number from 1 to 10: ";  
cin >> num; // read the number from user
switch (num) // Based on the entered number execute the switch case statements
{  

// case-1 and case-2 have common statement to execute which is storing the value 5 in total.
case 1:  
case 2: total = 5;
case 3: total = 10; // storing the value 10 in total.
case 7: total = total + 3; // add 3 to the total
case 8: total = total + 6; // add 6 to the total
default: total = total + 4; //add 4 to the total
}     
cout << total << endl; // print the total

Note: There is no break statement after the cases, so all the cases are executed following the case which is entered as choice.

When the number entered is 7, then the control goes to the case 7 and executes its statement, since there is no break after case 7, all the following cases are executed.
So, after executing case-7, total=total+3=0+3=3(initially total=0), next
case-8 is executed, total=total+6=3+6=9(new total value obtained from case-7 is 3), finally
default case is executed, total=total+4=9+4=13(new total value obtained from case-8 is 9), come out of the switch block and execute the following statement which is printing the total, so it prints 13 as output.


Related Solutions

no need to explain 11. What is output by the following code segment? boolean isHighTemp =...
no need to explain 11. What is output by the following code segment? boolean isHighTemp = true; double degree = 100.01; if (isHighTemp) if(degree >= 110) System.out.print(“Extremely Hot”); else System.out.println(“Not Hot”); A) Extremely Hot B) Not Hot C) Hot D) Extremely Hot Not Hot 12. To produce the output 2 4 6 8 10, what is the loop condition for the following loop? int n = 0; Page 3 do { n = n + 2; System.out.print(n + “ ”);...
what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back('...
what is the expected output of the following segment of code? dq = Deque() dq.push_back('M') dq.push_back(' ') l = ['A', 'L', 'E', 'S', 'T', 'E'] for i in l: → if ord(i) % 3 == 0: → → dq.push_back(i) → elif ord(i) % 5 == 0: → → dq.push_front(i) → → dq.push_back(i) → elif ord(i) % 4 == 0: → → dq.push_back(i) → else: → → dq.peek_front() → → dq.push_front('X') → → dq.push_back('R') dq.push_front(dq.peek_back()) dq.pop_back() print(dq)
Modify the following code to make the input and output look like this. Input 5 Vader...
Modify the following code to make the input and output look like this. Input 5 Vader 1300 Kirk 1250 Adama 1000 Reynolds 1615 Oneill 1470 Output Enter the number of candidates: 5 Enter candidate's name :Vader 1300 Enter votes received :Enter candidate's name :Kirk 1250 Enter votes received :Enter candidate's name :Adama 1000 Enter votes received :Enter candidate's name :Reynolds 1615 Enter votes received :Enter candidate's name :Oneill 1470 Enter votes received : Name Votes Percentage Vader 1300.00 19.59% Kirk...
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.int count = 0;do{++ count;if (count == 2)Blank;cout << count << " ";} while (count <= Blank);cout << endl;
A system is needed to control a 7 segment display. The input is all of the...
A system is needed to control a 7 segment display. The input is all of the possible binary codes 0000 to 1111. The output will be a 7 segment display of the hexadecimal digits 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f. How many K-maps are needed to solve this problem?
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that integer squared, ending with newline. (Note: This assignment is configured to have students programming directly in the zyBook. Instructors may instead require students to upload a file). Below is a program that's been nearly completed for you. Click "Run program". The output is wrong. Sometimes a program lacking input will produce wrong output (as in this case), or no output. Remember to always pre-enter...
The code on pages 20-21 of the Text book asks you to input a number, and...
The code on pages 20-21 of the Text book asks you to input a number, and it will then sum the numbers from 1 to that number. The Prompt asks you to input a number, not necessarily an integer. The program will abort if a floating point number is entered. Your project, is to “fix” the program and allow for a floating point number to be entered. The program will NOT run, so your task is to convert the floating...
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT