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
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 2, Problem 32SA

Explanation of Solution

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

#include <iostream>

#include <string>

using namespace std;

const double CONVERSION = 3.5;

Explanation:

In the above lines of code, the constant CONVERSION is declared and initialized.

int main() {

    const int TEMP = 23;

    string name;

    int id;

    int num;

    double decNum;

    double mysteryNum;

Explanation:

In the above lines, inside the main function, the declaration and initialization of the constant TEMP is followed by the declaration of the variables name, id, num, decNum and mysteryNum.

    cout << "Enter last name: ";

Explanation:

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

Enter last name:

    cin >> name;

Explanation:

The stream extraction operator >> and cin is used to accept the user input of a string ("Miller") which are assigned to the variable name. The insertion point remains in the same line as the string which was 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 << "Enter a two digit integer: ";

Explanation:

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

Enter a two digit integer:

    cin >> id;

Explanation:

The stream extraction operator >> and cin are used to accept the user input of a two-digit number (34) which is assigned to the variable id. The insertion point remains in the same line as the string which was typed as input at the console.

    cout << endl;

Explanation:

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

    num = (id * TEMP) % (static_cast<int>(CONVERSION));

Explanation:

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

(id * TEMP) % (static_cast<int>(CONVERSION))

 = (id * TEMP) % (static_cast<int>(CONVERSION))

= (34 * 23) % (static_cast<int>(3.5))          (value substitution)

= (34 * 23) % (3)                          (static_cast operator converts the floating point

                      number into int type discarding the decimal portion)

= 782 % 3                 (parentheses has highest precedence, * operator

                      evaluated)

= 2                    (modulus operator - 782 / 3 leaves 2 as remainder)

    cout << "Enter a decimal number: ";

Explanation:

The stream insertion operator cout is used to print a string "Enter a decimal number: "...

Blurred answer
Students have asked these similar questions
Zybooks C++ 1.7 LAB: Introduction to data structures labs Step 1: Producing correct output Three commented-out lines of code exist in main(). Uncomment the lines and click the "Run program" button. Verify that the program's output is: 2 + 2 = 4 Unknown function: PrintPlus2 Secret string: "abc" Submit your code for grading. Your submission will pass the "Compare output" test only, achieving 1 of the possible 10 points. Step 2: Inspecting the LabPrinter class Inspect the LabPrinter class implemented in the LabPrinter.h file. Access LabPrinter.h by clicking on the orange arrow next to main.cpp at the top of the coding window. Member functions Print2Plus2() and PrintSecret() print strings using std::cout. Step 3: Implementing CallFunctionNamed() Remove the three uncommented lines from main(). Then implement the CallFunctionNamed() function in main.cpp to handle three cases: If functionName is "Print2Plus2", call printer's Print2Plus2() member function. If functionName is "PrintSecret",…
PROGRAMMING LANGUAGE: C++   Add screenshots of outputs as well Execute the following example and write a comment after each line of code. In result print output of code. #include<iostream> #include<string> using namespace std; class Date {public: Date() {month=1;  day=1;  year=1990; } Date(int m,int d, int y)   { month=m; day=d; year=y; } void set() { int d,m,y; cout<<"Enter day: "; cin>>d; cout<<"Enter month: "; cin>>m; cout<<"Enter year: "; cin>>y;                                                 month=m;                                                 day=d;                                                 year=y;                                 }                                 void print()                                 {                                                 cout<<day<<"-"<<month<<"-"<<year;                                 }                                                 private:…
6. //Program 4.6 #include #include int main (){ char c1, c2, с3, с4; cl=65; c2='A'; c3=0x41; c4=0101; printf("%c %d %f %s", c1, c2, c3, c4); getch(); return 0; H N M 4567 o O

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
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++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr