Chapter 4

C++ How to Program · 22 exercises

Problem 2

Write four different \(C++\) statements that each add 1 to integer variable \(x\).

4 step solution

Problem 7

Write single \(C++\) statements that do the following: a. Input integer variable \(x\) with \(\operatorname{cin}\) and \(\gg>\) b. Input integer variable \(y\) with \(\operatorname{cin}\) and \(\gg>\) c. Set integer variable 1 to 1 d. Set integer variable power to 1 e. Multiply variable power by x and assign the result to power. f. Postincrement variable i by 1 g. Determine whether is less than or equal to \(y\). h. Output integer variable power with cout and \(<<\)

8 step solution

Problem 9

Identify and correct the errors in each of the following: a. while \((c<=5)\) \\{ product \(*=c\) \\[ \mathrm{c}++ \\] b. \(\operatorname{cin} \ll<\) value \(\mathbf{c}\) if \((\text { gender }=1\) cout \(<<\) "Woman" \(<<\) end 1 else: cout \(<<\) "Man" \(<<\) end 1

6 step solution

Problem 10

What is wrong with the following while repetition statement? \\[ \begin{array}{c} \text { while }\left(\begin{array}{rl} z & >\theta \end{array}\right) \\ \text { Sum }+=z \end{array} \\]

4 step solution

Problem 11

Identify and correct the error(s) in each of the following: a. if \((\text { age }>=65)\) cout \(<<\) "Age is greater than or equal to \(65^{\prime \prime}<<\) end 1 else cout \(<<\) "Age is less than \(65<<\) endl" b. if \((\text { age }>=65)\) cout \(<<\) "Age is greater than or equal to \(65^{\prime \prime}<<\) end 1 else; cout \(<<\) "Age is less than \(65<<\) end \(1 "\) c. int \(x=1,\) total; while \((x<=10)\) \\{ \\[ \begin{array}{l} \operatorname{total}+x ; \\ x++; \end{array} \\] \\} d. While \((x<=10 \theta)\) \\[ \begin{array}{l} \operatorname{total}+x ; \\ x++; \end{array} \\] e. while \((y>0)\) \\{ cout \(<

5 step solution

Problem 13

Perform each of these steps: a. Read the problem statement. b. Formulate the algorithm using pseudocode and top-down, stepwise refinement. c. Write a \(C++\) program. d. Test, debug and execute the \(C++\) program. Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a \(C++\) program that uses a while statement to input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point.

4 step solution

Problem 14

Perform each of these steps: a. Read the problem statement. b. Formulate the algorithm using pseudocode and top-down, stepwise refinement. c. Write a \(C++\) program. d. Test, debug and execute the \(C++\) program. Develop a \(C++\) program that will determine whether a department-store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available: a. Account number (an integer) b. Balance at the beginning of the month c. Total of all items charged by this customer this month d. Total of all credits applied to this customer's account this month e. Allowed credit limit The program should use a while statement to input each of these facts. calculate the new balance \((=\text { beginning balance }+\) charges credits) and determine whether the new balance exceeds the customer's credit limit. For those customers whose credit limit is exceeded, the program should display the customer's account number, credit limit, new balance and the message "Credit limit Exceeded.

4 step solution

Problem 15

Perform each of these steps: a. Read the problem statement. b. Formulate the algorithm using pseudocode and top-down, stepwise refinement. c. Write a \(C++\) program. d. Test, debug and execute the \(C++\) program. One large chemical company pays its salespeople on a commission basis. The salespeople each receive \(\$ 200\) per week plus 9 percent of their gross sales for that week. For example, a salesperson who sells \(\$ 5000\) worth of chemicals in a week receives \(\$ 200\) plus 9 percent of \(\$ 5000,\) or a total of \(\$ 650 .\) Develop a \(\mathrm{C}++\) program that uses a while statement to input each salesperson's gross sales for last week and calculates and displays that salesperson's earnings. Process one salesperson's figures at a time.

5 step solution

Problem 17

The process of finding the largest number (i.e., the maximum of a group of numbers) is used frequently in computer applications. For example, a program that determines the winner of a sales contest inputs the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write a pseudocode program, then a \(\mathrm{C}++\) program that uses a while statement to determine and print the largest number of 10 numbers input by the user. Your program should use three variables, as follows: counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed). number: The current number input to the program. largest: The largest number found so far.

4 step solution

Problem 18

Write a C++ program that uses a while statement and the tab escape sequence \t to print the following table of values: N 10*N 100*N 1000*N 1 10 100 1000 2 20 200 2000 3 30 300 3000 4 40 400 4000 5 50 500 5000

6 step solution

Problem 23

(Dangling-Else Problem) State the output for each of the following when x is 9 and y is 11 and when x is 11 and y is 9. Note that the compiler ignores the indentation in a C++ program. The C++ compiler always associates an else with the previous if unless told to do otherwise by the placement of braces {}. On first glance, the programmer may not be sure which if and else match, so this is referred to as the "dangling-else" problem. We eliminated the indentation from the following code to make the problem more challenging. [Hint: Apply indentation conventions you have learned.] a. if ( x < 10 ) if ( y > 10 ) cout << "*****" << endl; else cout << "#" << endl; cout << "$$$$$" << endl; b. if ( x < 10 ) { if ( y > 10 ) cout << "*****" << endl; } else { cout << "#" << endl; cout << "$$$$$" << endl; }

4 step solution

Problem 25

Write a program that reads in the size of the side of a square and then prints a hollow square of that size out of asterisks and blanks. Your program should work for squares of all side sizes between 1 and \(20 .\) For example, if your program reads a size of \(5,\) it should print

5 step solution

Problem 26

A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following five-digit integers is a palindrome: 12321,55555,45554 and \(11611 .\) Write a program that reads in a five-digit integer and determines whether it is a palindrome. [Hint: Use the division and modulus operators to separate the number into its individual digits.]

6 step solution

Problem 27

Input an integer containing only 0 s and 1 s (i.e., a "binary" integer) and print its decimal equivalent. Use the modulus and division operators to pick off the "binary" number's digits one at a time from right to left. Much as in the decimal number system, where the rightmost digit has a positional value of 1, the next digit left has a positional value of \(10,\) then \(100,\) then \(1000,\) and so on, in the binary number system the rightmost digit has a positional value of \(1,\) the next digit left has a positional value of \(2,\) then \(4,\) then \(8,\) and so on. Thus the decimal number 234 can be interpreted as \(2 * 100+3 * 10\) \(+4 * 1 .\) The decimal equivalent of binary 1101 is \(1 * 1+0 * 2+1 * 4+1 *\) 8 or \(1+0+4+8,\) or \(13 .\) [Note: The reader not familiar with binary numbers might wish to refer to Appendix D.]

7 step solution

Problem 29

Write a program that prints the powers of the integer \(2,\) namely 2,4,8,16 \(32,64,\) etc. Your while loop should not terminate (i.e., you should create an infinite loop). To do this, simply use the keyword true as the expression for the while statement. What happens when you run this program?

5 step solution

Problem 30

Write a program that reads the radius of a circle (as a double value) and computes and prints the diameter, the circumference and the area. Use the value 3.14159 for \(p\).

4 step solution

Problem 31

What is wrong with the following statement? Provide the correct statement to accomplish what the programmer was probably trying to do. \(\operatorname{cout}<<++(x+y)\)

5 step solution

Problem 32

Write a program that reads three nonzero double values and determines and prints whether they could represent the sides of a triangle.

7 step solution

Problem 33

Write a program that reads three nonzero integers and determines and prints whether they could be the sides of a right triangle.

5 step solution

Problem 34

(Cryptography) A company wants to transmit data over the telephone, but is concerned that its phones could be tapped. All of the data are transmitted as four-digit integers. The company has asked you to write a program that encrypts the data so that it can be transmitted more securely. Your program should read a four-digit integer and encrypt it as follows: Replace each digit by (the sum of that digit plus 7 ) modulus \(10 .\) Then, swap the first digit with the third, swap the second digit with the fourth and print the encrypted integer. Write a separate program that inputs an encrypted fourdigit integer and decrypts it to form the original number.

8 step solution

Problem 35

The factorial of a nonnegative integer \(n\) is written \(n !\) (pronounced " \(n\) factorial") and is defined as follows: \(n !=n \cdot(n 1) \cdot(n 2) \cdot \ldots \cdot 1 \text { (for values of } n \text { greater than to } 1)\) and \(n !=1\) (for \(n=0\) or \(n=1\) ). For example, \(5 !=5 \cdot 4 \cdot 3 \cdot 2 \cdot 1,\) which is \(120 .\) Use while statements in each of the following: a. Write a program that reads a nonnegative integer and computes and prints its factorial. b. Write a program that estimates the value of the mathematical constant \(e\) by using the formula: \(e=1+\frac{1}{1 !}+\frac{1}{2 !}+\frac{1}{3 !}+\dots\) Prompt the user for the desired accuracy of \(e\) (i.e., the number of terms in the summation). c. Write a program that computes the value of \(e^{x}\) by using the formula \\[ e^{x}=1+\frac{x}{1 !}+\frac{x^{2}}{2 !}+\frac{x^{3}}{3 !}+\ldots \\] Prompt the user for the desired accuracy of e (i.e., the number of terms in the summation).

9 step solution

Problem 36

[Note: This exercise corresponds to Section 4.13 , a portion of our software engineering case study.] Describe in 200 words or fewer what an automobile is and does. List the nouns and verbs separately. In the text, we stated that each noun might correspond to an object that will need to be built to implement a system, in this case a car. Pick five of the objects you listed, and, for each, list several attributes and several behaviors. Describe briefly how these objects interact with one another and other objects in your description. You have just performed several of the key steps in a typical object-oriented design.

4 step solution

Show/ page