chapter 3 - structured program development outline 3.1introduction 3.2algorithms 3.3pseudocode...

33
Chapter 3 - Structured Program Development Outline 3.1 Introduction 3.2 Algorithms 3.3 Pseudocode 3.4 Control Structures 3.5 The If Selection Structure 3.6 The If/Else Selection Structure 3.7 The While Repetition Structure 3.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition)

Upload: gerald-gray

Post on 04-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

Chapter 3 - Structured Program Development

Outline3.1 Introduction3.2 Algorithms3.3 Pseudocode3.4 Control Structures3.5 The If Selection Structure3.6 The If/Else Selection Structure3.7 The While Repetition Structure3.8 Formulating Algorithms: Case Study 1 (Counter-Controlled Repetition)

Page 2: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

Outline3.9 Sentinel-Controlled Repetition3.11Assignment Operators3.12Increment and Decrement Operators

Page 3: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.1Introduction• Before writing a program:

– Have a thorough understanding of problem – Carefully planned approach for solving it

• While writing a program: – Know what “building blocks” are available– Use good programming principles

Page 4: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.2Algorithms• Computing problems

– All can be solved by executing a series of actions in a specific order

• Algorithm: procedure in terms of– Actions to be executed – Order in which these actions are to be executed

• Program control – Specify order in which statements are to

executed

Page 5: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.3Pseudocode• Pseudocode

– Artificial, informal language that helps us develop algorithms

– Similar to everyday English– Not actually executed on computers – Helps us “think out” a program before writing

it • Easy to convert into a corresponding C++

program• Consists only of executable statements

Page 6: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.4Control Structures• Sequential execution

– Statements executed one after the other in the order written

• Transfer of control– When the next statement executed is not the

next one in sequence– Overuse of goto led to many problems.

• Bohm and Jacopini (1966)– All programs written in terms of 3 control

structures

Page 7: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

• Sequence structure: Built into C. Programs executed sequentially by default.

• Selection structures: C has three types- if, if/else, and switch .

• Repetition structures: C has three types - while, do/while, and for.

• These are C keywords

Page 8: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

• Flowchart – Graphical representation of an algorithm– Drawn using certain special-purpose symbols

connected by arrows called flowlines. – Rectangle symbol (action symbol): indicates

any type of action.– Diamond symbol: indicates that a decision is to

be made.

• Single-entry/single-exit control structures – Connect exit point of one control structure to

entry point of the next (control-structure stacking).

Page 9: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.5The if Selection Structure

• Selection structure: – Pseudocode:

If student’s grade is greater than or equal to 60 Print “Passed”

– Statement in C:

if ( grade >= 60 ) printf( "Passed\n" );

– C code corresponds closely to the pseudocode

Page 10: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

true

false

grade >= 60

print “Passed”

Flowchart of if statement

Page 11: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.6The if/else Selection Structure

• if– Only performs an action if the condition is true.

• if/else– A different action when condition is true than

when condition is false• Psuedocode: If student’s grade is greater than or equal to 60

Print “Passed” else

Print “Failed”

 

Page 12: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.6The if/else Selection Structure

• C code: if ( grade >= 60 ) printf( "Passed\n");

else printf( "Failed\n");

 

Page 13: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

truefalse

print “Failed”

print “Passed

grade >= 60

Page 14: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

•Nested if/else structures – multiple cases by placing if/else selection

structures inside if/else selection structureIf student’s grade is greater than or equal to 90

Print “A”else If student’s grade is greater than or equal to 80

Print “B” else

If student’s grade is greater than or equal to 70 Print “C”

else

Page 15: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

If student’s grade is greater than or equal to 60 Print “D”

elsePrint “F”

– Once condition is met, rest of statements skipped

Page 16: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

• C code:if ( grade >= 90 ) printf( “A \n” );

else if( grade >= 80 ) printf( “B \n” );

else if( grade >= 70 ) printf( “C \n” );

else if( grade >= 60 ) printf( “D \n” );

else

printf( “F \n” );

Page 17: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

• Compound statement– Set of statements within a pair of braces– Example:if ( grade >= 60 ) printf( "Passed.\n" );else { printf( "Failed.\n" ); printf( "You must take this course again.\n" );}

– Without the braces, printf( "You must take this course again.\n" );would be automatically executed

• Block: compound statements with declarations

Page 18: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

• Syntax errors– Caught by compiler

• Logic errors: – Have their effect at execution time– Non-fatal: program runs, but has incorrect

output– Fatal: program exits prematurely

Page 19: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.7The while Repetition Structure

• Repetition structure– an action to be repeated while some condition

remains true– Psuedocode:

find the powers of 2 while it is less than 1000 – while loop repeated until condition becomes false

 

Page 20: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

• Example:

int product = 2;

while ( product <= 1000 )product = 2 * product;

Page 21: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

product <= 1000

product = 2 * product

true

false

Page 22: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.8 Formulating Algorithms (Counter-Controlled Repetition)

• Counter-controlled repetition– Loop repeated until counter reaches a certain

value.– Definite repetition: number of repetitions is

known – Example: A class of ten students took a quiz.

The grades (integers in the range 0 to 100) Determine the class average on the quiz.

Page 23: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

/* Fig. 3.6: fig03_06.c

#include <stdio.h>

int main()

{ int counter, grade, total, average total = 0; counter = 1; while ( counter <= 10){ printf(“Enter grade: ”); scanf(“%d”, &grade ); total = total + grade; counter = counter + 1; }

Page 24: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

average = total/10;

printf (“Class average is %d\n”, average); return 0;}

Page 25: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

Enter grade: 98Enter grade: 76Enter grade: 71Enter grade: 87Enter grade: 83Enter grade: 90Enter grade: 57Enter grade: 79Enter grade: 82Enter grade: 94Class average is 81

Page 26: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.9Sentinel-Controlled Repetition

• Problem becomes: Develop a class-averaging program that will process an arbitrary number of grades each time the program is run.

– Unknown number of students

– How will the program know to end?

• Use sentinel value – Also called signal value, dummy value, or flag value

– Indicates “end of data entry.”

– Loop ends when sentinel inputted

– Sentinel value chosen so it cannot be confused with a regular input (such as -1 in this case)

Page 27: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

/* Fig. 3.8: fig03_08.c#include <stdio.h>

int main(){

float average;int counter, grade, total; total = 0;Counter = 0;printf( "Enter grade, -1 to end: " );scanf( "%d", &grade );

while ( grade != -1 ) { total = total + grade;

Page 28: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

counter = counter + 1; printf( "Enter grade, -1 to end: " ); scanf( "%d", &grade ); } if ( counter != 0){ average = (float) total/counter; printf(“ Class average is %.2f”, average); } else printf( “No grade were enter \n”);

return 0;}

Page 29: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

Enter grade, -1 to end: 75Enter grade, -1 to end: 94Enter grade, -1 to end: 97Enter grade, -1 to end: 88Enter grade, -1 to end: 70Enter grade, -1 to end: 64Enter grade, -1 to end: 83Enter grade, -1 to end: 89Enter grade, -1 to end: -1Class average is 82.50

Page 30: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.11 Assignment Operators

• Assignment operators abbreviate assignment expressionsc = c + 3; abbreviated as c += 3;

• Statements of the formvariable = variable operator expression;

can be rewritten asvariable operator = expression;

• Examples of other assignment operators:d -= 4 (d = d - 4)e *= 5 (e = e * 5)f /= 3 (f = f / 3)g %= 9 (g = g % 9)

Page 31: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

3.12 Increment and Decrement Operators

• Increment operator (++) - can be used instead of c+=1

• Decrement operator (--) - can be used instead of c-=1.

• Preincrement

– Operator is used before the variable (++c or --c)

– Variable is changed, then the expression it is in is evaluated

Page 32: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

• Postincrement– Operator is used after the variable (c++ or c--)

– Expression executes, then the variable is changed

• If c = 5, then

printf( "%d", ++c); • Prints 6

printf( "%d", c++); • Prints 5

– In either case, c now has the value of 6

Page 33: Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The

Home Work #4

y = 5x2 + 6x + 10

Input x = -1.69, 2.3, 5.4, -0.5, please find

1. the corresponding value of y and print them out

2. print the maximum and minimum values of y

3. the average of y’s values

Using the statements, printf, scanf, while, if/else