Week 5 C Programming
Week 5 C Programming
Computer Programming I
Week 5
Structured Program Development – II
3.10 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 3: Nested Control Statements
Sentinel value
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement Case
Study 2: Sentinel-Controlled Iteration—Sentinel Values
• The user then types the sentinel value
• Sentinel-controlled iteration is often called indefinite iteration because the number of
iterations isn’t known before the loop begins executing
• Choose a sentinel value that’s not an acceptable input value
• Grades on a quiz are non-negative integers, so 1 is an acceptable sentinel
• The sentinel value 1 should not enter into the averaging calculation
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement
Case Study 2: Sentinel-Controlled Iteration—Top-Down, Stepwise
Refinement (1 of 2)
• Divide the top into smaller tasks listed in the order in which they need to be
performed
• This results in the following first refinement:
– Initialize variables
– Input, sum, and count the quiz grades
– Calculate and print the class average
• Here, only the sequence structure has been used
• Each refinement is a complete specification of the algorithm
• Only the level of detail varies.
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement
Case Study 2: Sentinel-Controlled Iteration—Second Refinement
(1 of 3)
• can be refined as
– Initialize total to zero
– Initialize counter to zero
• The pseudocode algorithm was developed after only two levels of refinement
• Sometimes more levels are necessary
• You terminate the top-down, stepwise refinement process when the pseudocode
algorithm provides sufficient detail for you to convert the pseudocode to C
• The most challenging part of solving a problem on a computer is developing the
algorithm for the solution
• Once a correct algorithm has been specified, producing a working C program
usually is straightforward
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement
Case Study 2: Sentinel-Controlled Iteration—Class-Average
Program for an Arbitrary Number of Grades (1 of 9)
Output 1:
• Figure 3.4 shows the C program
Enter grade, −1 to end: 75
• Although only integer grades are entered, the Enter grade, −1 to end: 94
Enter grade, −1 to end: 97
averaging calculation is likely to produce a number Enter grade, −1 to end: 88
with a decimal point Enter grade, −1 to end: 70
Enter grade, −1 to end: 64
• Type int cannot represent such a number Enter grade, −1 to end: 83
Enter grade, −1 to end: 89
• This program introduces the data type double to Enter grade, −1 to end: −1
handle numbers with decimal points—floating-point Class average is 82.50
numbers
Output 2:
• Note that lines 13 and 23 both include the sentinel
Enter grade, −1 to end: −1
value in the prompts requesting data entry No grades were entered
– This is a good practice in a sentinel-controlled
loop
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-
Controlled Iteration—Class-Average Program for an Arbitrary Number of Grades (6 of 9)
– This would cause an infinite loop if the user did not input -1 as the first grade
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement
Case Study 2: Sentinel-Controlled Iteration—Class-Average
Program for an Arbitrary Number of Grades (7 of 9)
• After reading the problem statement carefully, we make the following observations:
– The program must process 10 test results. We’ll use a counter-controlled loop.
– Each test result is a number—either a 1 or a 2. Each time the program reads a test result, it must
determine whether the result is a 1 or a 2. We’ll test for a 1 in our algorithm. If the number is not a 1,
we’ll assume that it’s a 2. Exercise 3.27 asks you to ensure that every test result is a 1 or a 2.
– Two counters are used—one to count the number of students who passed the exam and one to
count the number of students who failed the exam.
– After the program has processed all the results, it must decide whether more than 8 students
passed the exam and, if so, print "Bonus to Instructor!".
3.10 Formulating Algorithms with Top-Down,
Stepwise Refinement Case Study 3: Nested
Control Statements (4 of 9)
• First Refinement
– Initialize variables
– Input the ten quiz grades and count passes and failures
– Print an exam-results summary and decide whether to bonus the instructor
• Further refinement is necessary.
3.10 Formulating Algorithms with Top-Down, Stepwise
Refinement Case Study 3: Nested Control Statements (6 of 9)
• Second Refinement
– Commit to specific variables
– We need counters to record the passes and failures, a counter to control
the looping process, and a variable to store the user input.
– The pseudocode statement
▪ Initialize variables
– can be refined as follows:
▪ Initialize passes to zero
▪ Initialize failures to zero
▪ Initialize student to one
– Only the counter and totals are initialized.
3.10 Formulating Algorithms with Top-Down, Stepwise
Refinement Case Study 3: Nested Control Statements (7
of 9)
• Second Refinement
– The following pseudocode statement requires a loop
▪ Input the ten quiz grades and count passes and failures
– We know there are ten exam results, so we use counter-controlled looping
– Inside the loop (that is, nested within the loop), a double-selection statement will determine
whether each exam result is a pass or a failure and will increment the appropriate counter
– The refinement of the preceding pseudocode statement is then
▪ While student counter is less than or equal to ten
Input the next exam result
If the student passed
Add one to passes
else
Add one to failures
Add one to student counter
3.10 Formulating Algorithms with Top-Down, Stepwise
Refinement Case Study 3: Nested Control Statements (8 of
9)
• Second Refinement
– The pseudocode statement
▪ Print an exam-results summary and decide whether to bonus the instructor
– may be refined as follows:
▪ Print the number of passes
Print the number of failures
If more than eight students passed
Print “Bonus to instructor!”
3.10 Formulating Algorithms with Top-Down, Stepwise
Refinement Case Study 3: Nested Control Statements (9 of 9)
• Output 1
• Output 2
1 to d
=
a st eri sk eq ual s
e = 5
e as te ris k e qu als 5 e = e 5
e e qu als e as ter is k 5
20 to e
/=
f or war d sla sh eq ual s
f /= 3
f fo rw ard sl as h e qu als 3
f = f / 3
f = f fo rw ard sl as h 3
2 to f
%=
p er cen t equ als
g %= 9
g pe rc ent eq ua ls 9
g = g %9
g e qu als g pe rce nt 9
3 to g
3.12 Increment and Decrement Operators (1 of 9)
--
min us si gn mi nus s ign
-- b Decrement b by 1, then use the new value of b in the
expression in which b resides.
--
min us si gn mi nus s ign
b --
b mi nus si gn mi nus s ign
Use the current value of b in the expression in which b
resides, then decrement b by 1.
3.12 Increment and Decrement Operators (3 of 9)
• Figure 3.7 demonstrates the difference between the preincrementing and the
post-incrementing versions of the ++ operator.
3.12 Increment and Decrement
Operators (5 of 9)
1. // fig03_07.c
2. // Preincrementing and postincrementing.
3. #include <stdio.h>
4.
5. // function main begins program execution
6. int main(void) {
7. // demonstrate postincrement
8. int c = 5; // assign 5 to c
9. printf("%d\n", c); // print 5
10. printf("%d\n", c++); // print 5 then postincrement
11. printf("%d\n\n", c); // print 6
12.
13. // demonstrate preincrement
14. c = 5; // assign 5 to c
15. printf("%d\n", c); // print 5
16. printf("%d\n", ++c); // preincrement then print 6
17. printf("%d\n", c); // print 6
18. } // end function main
3.12 Increment and Decrement
Operators (6 of 9)
• Output:
5
5
6
5
6
6
3.12 Increment and Decrement Operators (7 of 9)
• The three assignment statements in Fig 3.6
ure
– passes = passes + 1;
– failures = failures + 1;
– student = student + 1;
• can be written more concisely with assignment operators as
– passes += 1;
– failures += 1;
– student += 1;
• with preincrement operators as
– ++passes;
– ++failures;
– ++student;
• or with postincrement operators as
– passes++;
– failures++;
– student++;
3.12 Increment and Decrement Operators (8 of 9)
asterisk, forward slash, percent / % left to right multiplicative
+
p lus , min us si gn
- left to right additive
== !=
e qua ls eq ua ls, ex cl ama ti on mar k equ als
left to right equality
?:
q ues ti on ma rk col on
right to left conditional
= += -= = /= %=
e qua ls , p lu s e qua ls , m inu s sig n equ als , ast er isk eq ua ls, f orw ard s las h equ als , per ce nt equ al s right to left assignment
Questions