Week 3 Handouts
Week 3 Handouts
3
Structured Program
Development in C
OBJECTIVES
In this chapter you will learn:
To develop algorithms through the process of
top-down, stepwise refinement.
To use the if selection statement and if...else
selection statement to select actions.
To use the while repetition statement to execute
statements in a program repeatedly.
Counter-controlled repetition and sentinel-controlled
repetition.
Structured programming.
The increment, decrement and assignment operators.
1
3
2
Example:
Write an algorithm to determine a student’s final grade and
indicate whether it is passing (>60) or failing. The final grade
is calculated as the average of four marks.
Pseudocode:
Input a set of 4 marks
Calculate their average by summing and dividing by 4
if average is below 60
Print “FAIL”
else
Print “PASS”
3
Example:
Draw the flowchart of an algorithm which finds the sum of two numbers
N and M
Example:
Draw the flowchart of an algorithm which finds the sum of the first 50
numbers
4
9
– Pseudocode:
If student’s grade is greater than or equal to 60
Print “Passed
10
If condition true
– Print statement executed and program goes on to next
statement
– If false, print statement is ignored and the program
goes onto the next statement
5
The if selection statement
General Form: if the condition is true then statement 1 is executed. If
the condition is false then statement 1 is skipped.
If there is only one statement inside the
if (condition) block, there is no need to use braces. Curly
statement 1; braces show the borders of the compound
statement or block.
if (condition) Example
{
statement 1; if (a < 50)
statement 2; {
… count=count +1;
statement n; sum =sum + a;
} }
Example:
if (a < 50)
{
count=count +1;
sum = sum + a;
if (b > a)
b = 0;
}
6
13
7
The if…else selection statement
Example:
if (x > y)
if (y < z)
k++;
else
m++;
else
j++;
(same) (same)
8
What is the output of the following code? Q
#include <stdio.h>
int main()
{
int x=1, y=2, z=3, k=0, j=0;
if (x > y){
if (y < z)
k++;}
else
j=j+1;
Write a program that reads in four integers from the keyboard and
then calculates the sum of the numbers which are positive. The screen
Q
dialogue should appear as follows:
Input four different integers: 14 8 11 -8
The sum of the positive numbers is: 25
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x1, x2, x3, x4;
int sum=0;
9
Ternary conditional operator (?:)
Takes three arguments (condition, value if true, value if false)
Condition
Example:
printf( "%s\n", grade >= 60 ? "Passed" : "Failed" );
OR
OR
if ( grade >= 60 )
printf( "Passed\n");
else
printf( "Failed\n");
Write a program (by using ternary operator) that reads in one integer Q
from the keyboard and then prints out a message whether integer is
greater than zero or not. The screen dialogue should appear as follows:
Input one integer: 14
Greater than zero
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x1;
system("Pause");
return 0; /* successful termination */
}
10
21
– Pseudocode:
While there are more items on my shopping list
Purchase next item and cross it off my list
22
Counter-Controlled Repetition
Counter-controlled repetition
– Loop repeated until counter reaches a certain value
– Definite repetition: number of repetitions is known
if number of repetitions is known a counter is used to
control repetition
11
23
12
25
13
23 /* loop while sentinel value not yet read from user */ 27
24 while ( grade != -1 ) {
25 total = total + grade; /* add grade to total */
26 counter = counter + 1; /* increment counter */ while loop repeats until user
27 enters a value of -1
28 /* get next grade from user */
29 printf( "Enter grade, -1 to end: " ); /* prompt for input */
30 scanf("%d", &grade); /* read next grade */
31 } /* end while */
32
33 /* termination phase */
34 /* if user entered at least one grade */ Ensures the user entered at least
35 if ( counter != 0 ) {
one grade
36
37 /* calculate average of all grades entered */
38 average = ( float ) total / counter; /* avoid truncation */
39
Converts total to
40 /* display average with two digits of precision */ float type
41 printf( "Class average is %.2f\n", average );
42 } /* end if */ Prints result with 2 digits
43 else { /* if no grades were entered, output message */ after decimal point
44 printf( "No grades were entered\n" );
45 } /* end else */
46
47 return 0; /* indicate program ended successfully */
48
49 } /* end function main */
2007 Pearson Education,
Inc. All rights reserved.
28
14
29
Performance Tip
30
Assignment Operators
c = c + 3;
Assignment operator
The form
variable = variable operator expression;
can be rewritten as
variable operator= expression;
can be abbreviated as
c += 3;
using the addition assignment operator
15
31
d -= 4; is equal to (d = d - 4)
e *= 5; is equal to (e = e * 5)
f /= 3; is equal to (f = f / 3)
g %= 9; is equal to (g = g % 9)
32
16
33
34
17
35
5
6
6
2007 Pearson Education,
Inc. All rights reserved.
18
37
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x1, x2, x3, x4;
x1=x2=x3=x4=1;
x1=x1+1; %1st
x2+=1; %2nd
x3++; %3th
++x4; %4th
printf("The results are: %d, %d, %d, %d\n",x1,x2,x3,x4);
system("Pause");
return 0; /* successful termination */
}
19
What is the expected output of each of the following? Q
int i=3;
printf( "%d ", (--i + 3) );
printf( "%d", (i++ + 10) );
Answer:
5 12
What is the value of i now?
20