In this section, we present a pseudocode algorithm (Fig. In this example, repetition terminates when the counter exceeds 10. This technique uses a variable called a counter to control the number of times a set of statements will execute. We use counter-controlled repetition to input the grades one at a time. Let us use pseudocode to list the actions to be executed and specify the order in which these actions should be executed. 4.5 Flowcharting the while repetition structure. The algorithm for solving this problem on a computer must input each of the grades, perform the averaging calculation and print the result.įig. The class average is equal to the sum of the grades divided by the number of students. The grades (integers in the range 0 to 100) for this quiz are available to you. Consider the following problem statement:Ī class of ten students took a quiz. To illustrate how algorithms are developed, we solve several variations of a class-averaging problem. Ĥ.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition) Execution continues with the next statement after the while. This result terminates the repetition, with 1024 as product’s final value. When product becomes 1024, the condition product <= 1000 in the while structure becomes false.
Variable product is repeatedly multiplied by 2, taking on the values 4, 8, 16, 32, 64, 128, 256, 5 successively. When the while structure is entered, product is 2. At this point, the while structure is exited, and control passes to the next statement in the program. The flowline emerging from the rectangle wraps back to the decision, which is tested each time through the loop until the decision eventually becomes false. The flowchart clearly shows the repetition. The empty rectangles and diamonds are then filled in with appropriate actions and decisions. Imagine, again, a deep bin of empty while structures that may be stacked and nested with other control structures to form a structured implementation of an algorithm’s flow of control. Once again, note that, besides small circles and arrows, the flowchart contains only a rectangle symbol and a diamond symbol. 4.5 illustrates the flow of control of the preceding while repetition structure.
While ( product <= 1000 ) product = 2 * product
6.13 Example Using Recursion: The Fibonacci Series.5.8 Labeled break and continue Statements.3.7 Java Applet Internet and World Wide Web Resources.3.4 Two More Simple Applets: Drawing Strings and Lines.