© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this time you have learned four looping constructs (also called repetition statements). Each of these four loops are ideal to use in different situations. The while Loop The while loop is a pre-test loop. It is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. It is also ideal if you want to use a sentinel. The while-break Loop The while-break loop is a mid-test loop. It is ideal in situations where you want to test an exit condition within the loop. The do-while Loop The do-while loop is a post test loop. It is ideal in situations where you always want the loop to iterate at least once. The for Loop The for loop is a pre-test loop that first executes an initialization expression. In addition, it automatically executes an update expression at the end of each iteration. It is ideal for situations where a counter variable is needed. The for loop is primarily used when the exact number of required iterations is known. Source: Section 5.8 “Focus on Software Engineering” Starting out with C++ Alternate 2 nd Edition, by Tony Gaddis
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.2Essentials of Counter-Controlled Repetition For each question write down these 3 elements of counter- controlled repetition –Data type, name, and initial value of the control variable int counter = 2; –The condition that tests for the final value of the control variable counter <= 5; –The increment (or decrement) by which the control variable is modified during each iteration of the loop. counter++ ;
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.2 while Repetition Statement In Section 6.2 we found the first power of 3 greater than 50 Identify the key elements of repetition for this example. Rewrite the example using a do…while repetition statement. Rewrite the example using a for repetition statement.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6.5 do…while Repetition Statement (Cont.) In Section 6.5 we taught C++ how to count to 3, using a do…while repetition statement. Identify the key elements of repetition for this example. Rewrite the example using a while repetition statement. Rewrite the example using a for repetition statement.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8.4Examples Using the for Statement In Section 8.4 we stepped the control variable i from 7 to 77 in increments of 7. Identify the key elements of repetition for this example. Rewrite the example using a while repetition statement. Rewrite the example using a do…while repetition statement.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Factorial In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 0! is a special case that is explicitly defined to be 1. Here is the pseudo-code for one possible solution. Define variable f of type integer and initialize to 1 Prompt user for a number n Using a repetition statement calculate the factorial Display the message “factorial = ” followed by the factorial. Identify the key elements of repetition for this example. Write your program using a while repetition statement. Rewrite your program using a do…while repetition statement. How did you handle the 0! case? Rewrite your program using a for repetition statement.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Sum of Numbers Write a program that asks the user for a counting number. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 9, the loop will find the sum of , Input Validation: Do not accept a zero or negative starting number. Design Challenge: Can you discover a way to rewrite the program to run faster? Hint: 0+9=9, 1+8=9, …4+5=9 Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2 nd Edition, by Tony Gaddis
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Distance Traveled The distance a vehicle travels can be calculated as follows: distance = speed * time For example, if a train travels 40 mph for 3 hours, the distance traveled is 120 miles. Write a program that asks the user for the speed of a vehicle (in miles-per-hour) and how many hours it has traveled. It should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled Input Validation: Do not accept a negative number for speed and do not accept any value less than one for time traveled. Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2 nd Edition, by Tony Gaddis
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Pennies For Pay Write a program that calculates how much a person would earn over a period of time if his salary is one penny the first day, two pennies the second day, and continued to double each day. The program should ask the user for the number of days. Display a table showing how much the salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a dollar amount, not the number of pennies. Input Validation: Do not accept a number less than one for the number of days worked Source: Chapter 5 “Looping” Programming Challenges from Starting out with C++ Alternate 2 nd Edition, by Tony Gaddis
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fibonacci Number Consider the following sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, …. Given the first two numbers of the sequence (say a 1 and a 2 ), the nth number a n, n >= 3, of this sequence is given by a n = a n-1 + a n-2 Thus a 3 = a 2 + a 1 = = 2, a 4 = a 3 + a 2 = = 3,and so on… Such a sequence is called a Fibonacchi sequence. Write a program that given any first two numbers, using this algorithm, determine the nth number an, n <= 3, of the sequence. Sample Run Enter the first two Fibonacci numbers -> The first two Fibonacci numbers are 12 and 16 Enter the desired Fibonacci number to be determined -> 10 The 10 th Fibonacci number is 796. Source: Chapter 5 “Control Stucture II” C++ Programming, by D.S. Malik
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. More Programming Exercises 1.An integer is divisible by 9 if the sum of its digits is divisible by 9. Write a program that prompts the user to input an integer. The program should then output the number and a message string stating whether the number is divisible by 9. It does so by first adding the digits and then checking whether the sum of the digits is divisible by 9. 2.Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as ; output the individual digits of 8030 as ; output the individual digits of as ; output the individual digits of 4000 as ; and output the individual digits of-2345 as Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345, the output should be Your program must also output 5000 as 0005 and 980 as Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers. 5.Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is divisible by 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Source: Chapter 5 “Control Stucture II” C++ Programming, by D.S. Malik