Introduction to Programming with C++, 3rd edition
Introduction to Programming with C++, 3rd edition
3rd Edition
ISBN: 9780133377477
Author: Y. Daniel Liang
Publisher: Pearson Academic Computing
bartleby

Concept explainers

bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 3, Problem 34CP

Explanation of Solution

Given: The following code:

int x = 1, a = 3;

if (a == 1)

x += 5;

else if (a == 2)

x += 10;

else if (a == 3)

x += 16;

else if (a == 4)

x += 34;

To find: The final value of the variable x after the complete execution of the code.

Solution:

The value of x at the end of the code will be 17 as the value of x is initialized with by 1 and a is initialized by 3. If the else if (a == 3) block will be evaluated as true, then inside the else if block, the value of x will be added to 16. Hence, the value of x will become 17. The other if and else blocks will be evaluated as false.

Program-

// Including header file

#include

// Including header file

#include

//Decalaring namespace

using namespace std;

// Declaring main of code

int main() {

// Declaring the variables...

Explanation of Solution

Given: The following code:

int x = 1, a = 3;

if (a == 1)

x += 5;

else if (a == 2)

x += 10;

else if (a == 3)

x += 16;

else if (a == 4)

x += 34;

To find: The equivalent statement by using the switch statement and the switch statement flow chart

Solution:

The following is the statement equivalent to the if-statement with the help of switch-statement.

switch(a)

{

case 1:

// Assigning the value.

x += 5;

// Using the break-keyword.

break;

case 2:

// Assigning the value.

x += 10;

// Using the break-keyword.

break;

case 3:

// Assigning the value.

x += 16;

// Using the break-keyword.

break;

case 4:

// Assigning the value.

x += 34;

}

Program-

// Including header file

#include

// Including header file

#include

//Declaring namespace

using namespace std;

// Declaring the main of code

int main() {

// Variable declaration

int x = 1, a = 3;

switch(a)

{

case 1:

// Assigning the value...

Blurred answer
Students have asked these similar questions
CPS 2390 Extra Credit Assignment For each problem, choose the best answer and explain how you arrived at your answer. (15 points each.) 1.If control is redirected to location x4444 after the execution of the following instructions, what should have been the relationship between R1 and R2 before these instructions were executed? Address Instruction x4400 1001100010111111 x4401 0001100100100001 x4402 0001100001000100 x4403 0000100001000000 A. R1 R2 (R1 was greater than R2) B. R1 R2 (R2 was greater than R1) C. R1 R2 (R1 and R2 were equal) = D. Cannot be determined with the given information. 2. If the value stored in RO is 5 at the end of the execution of the following instructions, what can be inferred about R5? Address x3000 Instruction 0101000000100000 x3001 0101111111100000 x3002 0001110111100001 x3003 0101100101000110 x3004 0000010000000001 x3005 0001000000100001 x3006 0001110110000110 x3007 0001111111100001 x3008 0001001111111000 x3009 0000100111111000 x300A 0101111111100000 A. The…
Need help writing code to answer this question in Python! (image attached)
Need help with python code! How do I simplify my code for a beginner to understand, simple fixed format and centering? Such as:  print(f"As an int variable: {age_int:^7}") print(f"In numeric binary: {age_int:^7b}") My Code:name = input("Enter your name: ")print(f"In text name is: {' '.join(name)}")decimal_values = []binary_values = []for letter in name:   ascii_val = ord(letter)   binary_val = format(ascii_val, '08b')   decimal_values.append(str(ascii_val))   binary_values.append(binary_val)# Loop through each letter:print(f"In ASCII decimal: {' '.join(decimal_values)}")print(f"In ASCII binary: {' '.join(binary_values)}")# Ageage_str = input("Enter your age: ")age_int = int(age_str)print(f"As a string \"{age_str}\": {' '.join(age_str)}")age_decimal_values = []age_binary_values = []for digit in age_str:   ascii_val = ord(digit)   binary_val = format(ascii_val, '07b')   age_decimal_values.append(str(ascii_val))   age_binary_values.append(binary_val)print(f"In ASCII decimal: {'…

Chapter 3 Solutions

Introduction to Programming with C++, 3rd edition

Chapter 3, Problem 11CPChapter 3, Problem 12CPChapter 3, Problem 13CPChapter 3, Problem 14CPChapter 3, Problem 15CPChapter 3, Problem 16CPChapter 3, Problem 17CPChapter 3, Problem 18CPChapter 3, Problem 19CPChapter 3, Problem 20CPChapter 3, Problem 21CPChapter 3, Problem 22CPChapter 3, Problem 23CPChapter 3, Problem 24CPChapter 3, Problem 25CPChapter 3, Problem 26CPChapter 3, Problem 27CPChapter 3, Problem 28CPChapter 3, Problem 29CPChapter 3, Problem 30CPChapter 3, Problem 31CPChapter 3, Problem 32CPChapter 3, Problem 33CPChapter 3, Problem 34CPChapter 3, Problem 35CPChapter 3, Problem 36CPChapter 3, Problem 37CPChapter 3, Problem 38CPChapter 3, Problem 39CPChapter 3, Problem 40CPChapter 3, Problem 41CPChapter 3, Problem 1PEChapter 3, Problem 2PEChapter 3, Problem 3PEChapter 3, Problem 4PEChapter 3, Problem 5PEChapter 3, Problem 6PEChapter 3, Problem 7PEChapter 3, Problem 8PEChapter 3, Problem 9PEChapter 3, Problem 10PEChapter 3, Problem 11PEChapter 3, Problem 12PEChapter 3, Problem 13PEChapter 3, Problem 14PEChapter 3, Problem 15PEChapter 3, Problem 16PEChapter 3, Problem 17PEChapter 3, Problem 18PEChapter 3, Problem 19PEChapter 3, Problem 20PEChapter 3, Problem 21PEChapter 3, Problem 22PEChapter 3, Problem 23PEChapter 3, Problem 24PEChapter 3, Problem 25PEChapter 3, Problem 26PEChapter 3, Problem 27PEChapter 3, Problem 28PEChapter 3, Problem 29PEChapter 3, Problem 30PEChapter 3, Problem 31PEChapter 3, Problem 32PEChapter 3, Problem 33PEChapter 3, Problem 34PEChapter 3, Problem 35PEChapter 3, Problem 36PE

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Control Structure in Data Structure - Data Structures - Computer Science Class 12; Author: Ekeeda;https://www.youtube.com/watch?v=9FTw2pXLhv4;License: Standard YouTube License, CC-BY