Upcoming SlideShare
×

# Chapter 5

2,562 views
2,332 views

Published on

C++ - 7 edition - Solution Manual

Published in: Technology
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
2,562
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
80
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Chapter 5

1. 1. Control Statements, Part 2:Solutions 5 Not everything that can be counted counts, and not every thing that counts can be counted. —Albert Einstein Who can control his fate? —William Shakespeare The used key is always bright. —Benjamin Franklin Intelligence … is the faculty of making artificial objects, especially tools to make tools. —Henri Bergson Objectives In this chapter you’ll learn: ■ The essentials of counter- controlled repetition. ■ To use for and do…while to execute statements in a program repeatedly. ■ To implement multiple selection using the switch selection statement. ■ How break and continue alter the flow of control. ■ To use the logical operators to form complex conditional expressions in control statements. ■ To avoid the consequences of confusing the equality and assignment operators. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
2. 2. 2 Chapter 5 Control Statements, Part 2: Solutions Student Solution Exercises 5.4 Find the error(s), if any, in each of the following: a) For ( x = 100, x >= 1, x++ ) cout << x << endl; ANS: For should be for. The commas should be semicolons. The ++ should be a decrement such as --. b) The following code should print whether integer value is odd or even: switch ( value % 2 ) { case 0: cout << "Even integer" << endl; case 1: cout << "Odd integer" << endl; } ANS: case 0 needs a break statement. c) The following code should output the odd integers from 19 to 1: for ( x = 19; x >= 1; x += 2 ) cout << x << endl; ANS: += should be -=. d) The following code should output the even integers from 2 to 100: counter = 2; do { cout << counter << endl; counter += 2; } While ( counter < 100 ); ANS: While should be while. Operator < should be <=. 5.5 (Summing Integers) Write a program that uses a for statement to sum a sequence of inte- gers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value per input statement. A typical input sequence might be 5 100 200 300 400 500 where the 5 indicates that the subsequent 5 values are to be summed. ANS: 1 // Exercise 5.5 Solution: ex05_05.cpp 2 // Total a sequence of integers. 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 int total = 0; // current total 9 int number; // number of values10 int value; // current value11 © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
3. 3. Student Solution Exercises 312 // display prompt13 cout << "Enter the number of values to be summed "14 << "followed by the values: n";15 cin >> number; // input number of values1617 // loop number times18 for ( int i = 1; i <= number; i++ )19 {20 cin >> value;21 total += value;22 } // end for2324 // display total25 cout << "Sum of the " << number << " values is " << total << endl;26 } // end mainEnter the number of values to be summed followed by the values:5 100 200 300 400 500Sum of the 5 values is 1500 5.8 (Find the Smallest Integer) Write a program that uses a for statement to find the smallest of several integers. Assume that the first value read specifies the number of values remaining. ANS: 1 // Exercise 5.8 Solution: ex05_08.cpp 2 // Find the smallest of several integers. 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 int number; // number of values 9 int value; // current value10 int smallest; // smallest value so far1112 cout << "Enter the number of integers to be processed ";13 cout << "followed by the integers: " << endl;14 cin >> number >> smallest;1516 // loop (number -1) times17 for ( int i = 2; i <= number; i++ )18 {19 cin >> value; // read in next value2021 // if current value less than smallest, update smallest22 if ( value < smallest )23 smallest = value;24 } // end for2526 // display smallest integer27 cout << "nThe smallest integer is: " << smallest << endl;28 } // end main © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.