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

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 Control structures  Algorithm & flowchart  If statements  While statements.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
Lec3: Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Structured Program Development in C
Lecture 3 Structured Program Development in C
The University of Texas – Pan American
 2003 Prentice Hall, Inc. All rights reserved.  2004 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Computer Organization Six logical units in every.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
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. Objectives To understand basic problem-solving techniques. To be able to develop algorithms through the process.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 - JavaScript: Control Statements I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
1 JavaScript/Jscript 2 Control Structures I. 2 Introduction Before programming a script have a –Thorough understanding of problem –Carefully planned approach.
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
 2007 Pearson Education, Inc. All rights reserved Structured Program Development in C.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Structured Program Development in C
Algorithm: procedure in terms of
- Standard C Statements
JavaScript: Control Statements I
Chapter 2.1 Control Structures (Selection)
Chapter 8 - JavaScript: Control Statements I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Lecture 2: Logical Problems with Choices
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 – Control Structures
Chapter 3 - Structured Program Development
Structured Program Development in C
2.6 The if/else Selection Structure
EPSII 59:006 Spring 2004.
Structured Program Development in C
Structured Program Development in C
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

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

Outline 3.9Sentinel-Controlled Repetition 3.11Assignment Operators 3.12Increment and Decrement Operators

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

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

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

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

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

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).

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

true false grade >= 60 print “Passed” Flowchart of if statement

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”

3.6The if/else Selection Structure C code: if ( grade >= 60 ) printf( "Passed\n"); else printf( "Failed\n");

true false print “Failed” print “Passed” grade >= 60

Nested if/else structures –multiple cases by placing if/else selection structures inside if/else selection structure If 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

If student’s grade is greater than or equal to 60 Print “D” else Print “F” –Once condition is met, rest of statements skipped

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” );

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

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

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

Example: int product = 2; while ( product <= 1000 ) product = 2 * product;

product <= 1000 product = 2 * product true false

3.8Formulating 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.

/* Fig. 3.6: fig03_06.c #include 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; }

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

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

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)

/* Fig. 3.8: fig03_08.c #include 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;

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; }

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

3.11Assignment Operators Assignment operators abbreviate assignment expressions c = c + 3; abbreviated as c += 3; Statements of the form variable = variable operator expression ; can be rewritten as variable 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)

3.12Increment 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

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

Home Work #4 y = 5x 2 + 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