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
1) #include #include int power(int, int); int main(void) { int x, n; printf("Enter a number and powerto raise it to: "); scanf_s("%d %d", &x, &n); printf("Result: %d\n", power(n, x)); return 0; } int power(int x, int n) { int m; if (n == 0) return 1; if (n % 2 == 0) { m = power(x, n / 2); return m * m; } else return x * power(x, n - 1); } 2) #include int hcf(int n1, int n2); int main() { int n1, n2; printf("Enter any two positive integers: "); scanf("%d %d", &n1, &n2); printf("Greatest Common Divisor of %d and %d is %d.", n1, n2, hcf(n1,n2)); return 0; } int hcf(int n1, int n2) { if (n2 != 0) return hcf(n2, n1%n2); else return n1; } 3) #include int main() { int array[], minimum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d",&size); printf("Enter %d integers\n", size); for ( c = 0 ; c < size ; c++ ) scanf("%d", &array[c]); minimum = array[0]; for ( c = 1 ; c < size ; c++ ) { if ( array[c] < minimum ) {…
! 463 476 465 #include using namespace std; void myfunction(int num2, int num1); lint main() { my function (5,2); return 0; } void myfunction(int num1, int num2) {if (num1>3) cout using namespace std; int f(); int main() (int i, select; select=f(); select=select+1; cout>num:
#include using namespace std; void myfunction(int num2, int num1); lint main() { my function (5,2); return 0; } void myfunction(int num1, int num2) {if (num1>3) cout << "A1"; else if (num1<3) cout<<"A2"; else cout<<"A3";} O A2 O A1 O A3 A1 A2 A3

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