C++ Programming: From Problem Analysis to Program Design
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN: 9781337102087
Author: D. S. Malik
Publisher: Cengage Learning
Expert Solution & Answer
Book Icon
Chapter 2, Problem 31SA

Explanation of Solution

Below, the program execution and the output (if applicable) for relevant blocks of code are explained.

#include <iostream>

using namespace std;

const int NUM = 10;

const double X = 20.5;

int main() {

    int firstNum, secondNum;

    double z;

    char grade;

    firstNum = 62;

Explanation:

In the above lines of code, the constants NUM and X are declared and initialized, followed by the declaration of the variables firstNum, secondNum, z, and grade. The variable firstNum is also assigned a value of 62 through an assignment statement.

    cout << "firstNum = " << firstNum << endl;

Explanation:

The stream insertion operator << and cout are used to print a string "firstName = " and the value of the firstNum variable which is 62 at this point of execution. The insertion point is finally set to a new line through the use of the manipulator endl. So the output is,

firstNum = 62

    cout << "Enter three numbers: ";

Explanation:

The stream insertion operator << and cout are used to print a string "Enter three numbers: ". The insertion point remains in the same line as the string output on the console. So the output is,

Enter three numbers:

    cin >> firstNum >> z >> secondNum;

Explanation:

The stream extraction operator >> and cin are used to accept the user inputs of three numbers (35, 10.5 and 27) which are assigned respectively to the variables firstNum, z, and secondNum. The insertion point remains in the same line as the numbers which were typed as input at the console.

    cout << endl;

Explanation:

The stream insertion operator << cout is used to move the insertion point to a new line through the use of the manipulator endl.

    cout << "The numbers you entered are "

         << firstNum << ", " << z << ", and "

         << secondNum << endl;

Explanation:

The above three lines of code are read as a single line of code by the compiler. The stream insertion operator << and cout are used to print a string "The numbers you entered are " followed by the value of the firstNum variable (which has a value assigned to 35), another string ", " followed by the value of variable z (which has a value assigned to 10.5). This in turn is followed by a string ", and " and the value of the variable secondNum (which has a value assigned to 27) at this point of execution. The insertion point is finally set to a new line through the use of the manipulator endl. So the output is,

The numbers you entered are 35, 10.5, and 27

    z = z - X + 2 * firstNum - secondNum;

Explanation:

The assignment statement assigns a new value to the variable z after evaluating the right-hand-side arithmetic expression. The expression is evaluated as follows,

z - X + 2 * firstNum - secondNum

= 10.5 - 20.5 + 2 * 35 - 27        (value substitution)

= 10.5 - 20.5 + (2 * 35) - 27     (* operation has higher precedence than the other operators)

= -10.0 + (2 * 35) - 27              (expression evaluated left to right)

= -10.0 + 70 - 27                       (integer multiplication)

= -10.0 + 70.0 - 27                    (+ operator with mixed operands - convert to floating point)

= 60.0 - 27                                 (floating point addition)

= 60.0 - 27.0                              (- operator with mixed operands - convert to floating point)

= 33.0                                         (floating-point subtraction)

    cout << "z = " << z << endl;

Explanation:

The stream insertion operator << and cout are used to print a string "z = " followed by the value of z which is 33.0 at this point of execution. However, cout prints it as 33 due to lack of formatting expressions. The insertion point is set to a new line through the use of the manipulator endl. So the output is,

z = 33

    cout << "Enter grade: ";

Explanation:

The stream insertion operator << and cout are used to print a string "Enter grade: ". The insertion point remains in the same line as the string output on the console. So the output is,

Enter grade:

    cin >> grade;

Explanation:

The stream extraction operator >> and cin are used to accept the user input of a letter ('B') which are assigned respectively to the variable grade. The insertion point remains in the same line as the letter which was typed as input at the console...

Blurred answer
Students have asked these similar questions
Write a FancyCar class to support basic operations such as drive, add gas, honk horn, and start engine. FancyCar.java is provided with method stubs. Follow each step to gradually complete all methods. Note: This program is designed for incremental development. Complete each step and submit for grading before starting the next step. Only a portion of tests pass after each step but confirm progress. The main() method includes basic method calls. Add statements in main() as methods are completed to support development mode testing. Step 0. Declare private fields for miles driven as shown on the odometer (int), gallons of gas in tank (double), miles per gallon or MPG (double), driving capacity (double), and car model (String). Note the provided final variable indicates the gas tank capacity of 14.0 gallons. Step 1 (2 pts). 1) Complete the default constructor by initializing the odometer to five miles, tank is full of gas, miles per gallon is 24.0, and the model is "Old Clunker". 2)…
Find the error: daily_sales = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday',                     'Wednesday', 'Thursday', 'Friday',                     'Saturday'] for i in range(7):         daily_sales[i] = float(input('Enter the sales for ' \                                      + day_of_week[i] + ': ')
Find the error: daily_sales = [0.0, 0,0, 0.0, 0.0, 0.0, 0.0, 0.0] days_of_week = ['Sunday', 'Monday', 'Tuesday',                     'Wednesday', 'Thursday', 'Friday',                     'Saturday'] for i in range(7):         daily_sales[i] = float(input('Enter the sales for ' \                                      + days_of_week[i] + ': ')

Chapter 2 Solutions

C++ Programming: From Problem Analysis to Program Design

Ch. 2 - Which of the following are valid C++ assignment...Ch. 2 - Write C++ statements that accomplish the...Ch. 2 - Write each of the following as a C++ expression....Ch. 2 - Prob. 14SACh. 2 - Suppose x, y, and z are int variables and wandt...Ch. 2 - 16. Suppose x, y, and z are int variables and x =...Ch. 2 - Suppose a and b are int variables, c is a double...Ch. 2 - 18. Write C++ statements that accomplish the...Ch. 2 - Which of the following are correct C++ statements?...Ch. 2 - Give meaningful identifiers for the following...Ch. 2 - 21. Write C++ statements to do the following....Ch. 2 - Prob. 22SACh. 2 - The following program has syntax errors. Correct...Ch. 2 - Prob. 24SACh. 2 - Prob. 25SACh. 2 - Preprocessor directives begin with which of the...Ch. 2 - 27. Write equivalent compound statements if...Ch. 2 - 28. Write the following compound statements as...Ch. 2 - 29. Suppose a, b, and c are int variables and a =...Ch. 2 - Suppose a, b, and sum are int variables and c is a...Ch. 2 - Prob. 31SACh. 2 - Prob. 32SACh. 2 - Prob. 33SACh. 2 - Prob. 34SACh. 2 - 1. Write a program that produces the following...Ch. 2 - Prob. 2PECh. 2 - Prob. 3PECh. 2 - 4. Repeat Programming Exercise 3 by declaring...Ch. 2 - Prob. 5PECh. 2 - Prob. 6PECh. 2 - 7. Write a program that prompts the user to input...Ch. 2 - Prob. 8PECh. 2 - 9. Write a program that prompts the user to enter...Ch. 2 - 10. Write a program that prompts the user to input...Ch. 2 - 11. Write a program that prompts the capacity, in...Ch. 2 - 12. Write a C++ program that prompts the user to...Ch. 2 - 13. To make a profit, a local store marks up the...Ch. 2 - 14. (Hard drive storage capacity) If you buy a 40...Ch. 2 - 15. Write a program to implement and test the...Ch. 2 - 16. A milk carton can hold 3.78 liters of milk....Ch. 2 - 17. Redo Programming Exercise 16 so that the user...Ch. 2 - Prob. 18PECh. 2 - 19. Write a program that prompts the user to input...Ch. 2 - 20. For each used car a salesperson sells, the...Ch. 2 - 21. Newton's law states that the force, , between...Ch. 2 - 22. One metric ton is approximately 2,205 pounds....Ch. 2 - 23. Cindy uses the services of a brokerage firm to...Ch. 2 - 24. A piece of wire is to be bent in the form of a...Ch. 2 - 25. Repeat Programming Exercise 24, but the wire...Ch. 2 - 26. A room has one door, two windows, and a...Ch. 2 - Prob. 27PECh. 2 - 28. In an elementary school, a mixture of equal...Ch. 2 - 29. A contractor orders, say, 30 cubic yards of...
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage