How to develop a program?

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

Introduction to Flowcharting
Introduction to Flowcharting
Introduction to Flowcharting
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
Structured Program Development in C
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
CSC103: Introduction to Computer and Programming
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
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. 1 Structural Program Development: If, If-Else Outline.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
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.
C Lecture Notes 1 Structured Program Development.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Algorithm Design.
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.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 Introduction to Flowcharting Computer Science Principles ASFA.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Chapter 3 Structured Program Development in C C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
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.
ALGORITHMS AND FLOWCHARTS
The if…else Selection Statement
Introduction to Flowcharting
REPETITION CONTROL STRUCTURE
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
C Program Controls + Flow Structure
Chapter 2.1 Control Structures (Selection)
Introduction to Flowcharting
Flowcharting: Decision Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Introduction To Flowcharting
Introduction to Flowcharting
Ch 7: JavaScript Control Statements I.
Lecturer CS & IT Department UOS MBDIN
Programming Fundamentals
Control Structures Lecture 7.
ALGORITHMS AND FLOWCHARTS
Chapter 13 Control Structures
Lecture 2: Logical Problems with Choices
CS1100 Computational Engineering
MSIS 655 Advanced Business Applications Programming
Structured Program
1) C program development 2) Selection structure
Chapter 3 - Structured Program Development
ALGORITHMS AND FLOWCHARTS
3 Control Statements:.
` Structured Programming & Flowchart
Chapter 3 - Structured Program Development
Faculty of Computer Science & Information System
Introduction to Flowcharting
Structural Program Development: If, If-Else
Presentation transcript:

How to develop a program? Requirements Problem Analysis Design Designing algorithm Pseudo code Flow chart Implementation Implementing algorithm in c/c++ Validation Test Maintenance Refinement Software Development Life-Cycle

Requirements Discovery Write a program to compute an employee's weekly pay? Problem Analysis Problem Identification Abstraction Inputs and outputs determination Defining their relation How many hours did you work? How much do you get paid per hour? employee's weekly pay equal to multiplication of Hours by Pay Rate

Problem vs. Solution A problem is something that causes trouble The solution is how to solve or fixe the problem

Algorithm A procedure for solving a problem in terms of actions and the order in which these actions are to be executed in a computer program (program control) Action Order

Execution order Sequential execution, normally, statements in a program are executed one after the other in the order in which they’re written Various C statements enable you to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control

Structured Programming goto statement that allows programmers to specify a transfer of control to one of many possible destinations in a program Research had demonstrated that programs could be written without any goto statements Programs produced with structured techniques were clearer, easier to debug and modify and more likely to be bug free in the first place. Research had demonstrated that all programs could be written in terms of only three control structures, namely the sequence structure, the selection structure and the repetition structure

Pseudo code An artificial and informal language that helps you develop algorithms similar to everyday English are not executed on computers consists only of action statements pay-calculation program: Read Hours and Pay Rate weekly pay = Read Hours * Pay Rate

Flowchart START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END A flowchart is a diagram that depicts the flow of an algorithm pay-calculation program Each symbol represents a different type of operation

Terminals represented by rounded rectangles START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Terminal represented by rounded rectangles indicate a starting or ending point START END Flow line Terminal

Input/Output Operations START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END indicate an input or output operation Display message “How many hours did you work?” Input/Output Operation Read Hours

Processes START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END indicates a process such as a mathematical computation or variable assignment Multiply Hours by Pay Rate. Store result in Gross Pay. Process

Execution Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ? START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Output Operation How many hours did you work? Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ?

Execution Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Input Operation (User types 40) How many hours did you work? 40 Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ?

Execution Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END How much do you get paid per hour? Output Operation Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ?

Execution Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: ? START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END How many hours did you work? 20 Input Operation (User types 20) Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: ?

Execution Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END How many hours did you work? 20 Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Process (Gross Pay = Hours * Pay Rate)

Execution Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 START Display message “How many hours did you work?” Read Hours Display message “How much do you get paid per hour?” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Your gross pay is 800 Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Output Operation

Connectors Sometimes a flowchart will not fit on one page A connector (represented by a small circle) allows you to connect two flowchart segments A START END

Call calc_pay function Modules A program module (such as a function in C) is represented by a special symbol The position of the module symbol indicates the point the module is executed A separate flowchart can be constructed for the module START END Read Input Call calc_pay function Display results module

Control Structures Sequence Decision selection statement The if statement is called a single-selection statement because it selects or ignores a single action. The if…else statement is called a double-selection statement because it selects between two different actions. The switch statement is called a multiple-selection statement because it selects among many different actions Repetition while do…while for

Sequence Structure a series of actions are performed in sequence The pay-calculating example

Compound Statements A statement is a specification of an action to be taken by the computer as the program executes Compound Statements is a list of statements enclosed in braces, { }

Decision Structure One of two possible actions is taken, depending on a condition Selection structures are used to choose among alternative courses of action YES NO YES NO x < y? Process B Process A

C programming language Decision Structure The flowchart segment below shows how a decision structure is expressed in C as an if/else statement Flowchart C programming language YES NO x < y? Calculate a as x times 2. Calculate a as x plus y. if (x < y) a = x * 2; else a = x + y;

Example Compound statement is way to group many statements together so they are treated as one if (grade >= 60) printf( "Passed.\n" ); // { printf( "Passed.\n" ); } else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); }

C programming language Decision Structure The flowchart segment below shows a decision structure with only one action to perform Flowchart C programming language YES NO x < y? Calculate a as x times 2. if (x < y) a = x * 2;

Combining Structures YES NO if (x > min) { if (x < max) Display “x is within limits.” Display “x is outside the limits.” YES NO x > min? x < max? if (x > min) { if (x < max) printf("x is within the limits"); else printf("x is outside the limits"); }

Example int k = 1, m = 4; if (k < 2 || m == 3) { m = 2 + k; printf("%d", m); } else k = 1; printf("%d", k); int k = 1, m = 4; if (k < 2 || m == 3) { m = 2 + k, printf("%d", m); } else k = 1, printf("%d", k); Which is more readable?

Example if(x) if(x) if(x) if(y) { { printf("Yes"); if(y) if(y) else printf("No"); if(x) { if(y) printf("Yes"); else printf("No"); } if(x) { if(y) printf("Yes"); } else printf("No"); if (x < 0.25) count1++; else if (x >= 0.25 && x < 0.5) count2++; else if (x >= 0.5 && x < 0.75) count3++; else count4++; if (x < 0) sign = -1; else if (x == 0) sign = 0; else sign = 1;

Case Structure One of several possible actions is taken, depending on the contents of a variable

Case Structure indicates actions to perform depending on the value in years_employed If years_employed = 2, bonus is set to 200 If years_employed = 3, bonus is set to 400 CASE years_employed 1 2 3 Other bonus = 100 bonus = 200 bonus = 400 bonus = 800 If years_employed = 1, bonus is set to 100 If years_employed is any other value, bonus is set to 800

switch A switch statement allows a single variable (integer or char) to be compared with several possible constants A constant can not appear more than once, and there can only be one default expression

switch switch (variable) { case const: statements...; default: } switch (c = toupper(getch())) { case ‘R’: printf("Red"); break; case ‘G’: printf("Green"); default: printf("other"); } switch (variable) { case const: statements...; default: }

Example switch(betty) { case 1: printf("betty = 1\n"); case 2: case 3: CASE betty? 1 2 3 Other betty = 1 switch(betty) { case 1: printf("betty = 1\n"); case 2: printf("betty=2\n"); break; case 3: printf("betty=3\n"); default: printf("Not sure\n"); } betty = 2 betty = 3 Not sure

Repetition Structure A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists x < y? Process A YES

C programming language Repetition Structure The flowchart segment below shows a repetition structure expressed in C as a while loop Flowchart C programming language x < y? Add 1 to x YES while (x < y) x++;

While while (loop_repetition_condition) statement; OR //Compound statement { statement1; statement2; // … }

Controlling a Repetition Structure The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created In this flowchart segment, x is never changed. Once the loop starts, it will never end How can this flowchart be modified so it is no longer an infinite loop? x < y? Display x YES

Controlling a Repetition Structure Adding an action within the repetition that changes the value of x x < y? Display x Add 1 to x YES

A Pre-Test Repetition Structure This type of structure is known as a pre-test repetition structure. The condition is tested BEFORE any actions are performed if the condition does not exist, the loop will never begin x < y? Display x Add 1 to x YES

Example int counter = 0; while (counter < 1000) ; while (1); printf("%d\n", counter ++); int counter = 9; while (counter > 0) printf("%d\n", counter --); int counter = 0; while (counter < 9) { printf("%d\n", counter); counter++; } int counter = 0; while (counter < 9) { printf("%d\n", counter ++); }

A Post-Test Repetition Structure The condition is tested AFTER the actions are performed A post-test repetition structure always performs its actions at least once Display x Add 1 to x YES x < y? C programming language do { printf(…); x++; } while (x < y);

do-while do statement; while (loop_repetition_condition) OR do //Compound statement { statement1; statement2; // … }

Example Draw a flowchart for the following problem: Read 5 integer and display the value of their summation. Input : 5 integer n1, n2, n3, n4, n5 Output: The summation of n1, n2, .., n5 Input example: 2 3 4 5 6 Output example: 20

Assume input example: 2 3 4 5 6 start Input n1 2 n1 Input n2 3 n2 This flowchart does not use loop, hence we need to use 6 different variables Input n3 4 n3 input n4 5 n4 input n5 sum ← n1+n2+n3+n4+n5 6 n5 output sum 20 sum end

This loop is Uses only The counter counter-controlled 3 variables Assume input example: 2 3 4 5 6 3 2 1 4 6 5 counter counter ← 1, sum ← 0 2 20 9 14 5 sum 14 + 6 5 + 4 9 + 5 2 + 3 0 + 2 counter < 6 false 6 < 6 false 5 < 6 true 3 < 6 true 2 < 6 true 1 < 6 true 4 < 6 true true 4 6 3 2 5 input n n sum ← sum + n counter++ This loop is counter-controlled The counter Increases by 1 Uses only 3 variables output sum

Decreasing Counter-Controlled Loop counter ← 5, sum ← 0 counter > 0 false true input x sum←sum+ x counter-- output sum C Programming Language 48

For for (initial_value ; condition; update_counter) statement; OR // Compound statement for (initial_value ; condition; update_counter) { statement; // … }

int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x); counter ← 1, sum ← 0 int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x); sum = sum + x; } counter < 6 false true input n sum ← sum + n counter++ printf(“%d”,sum); output sum 50

??? num Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); _ 51

1 num Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); _ 52

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num _ 53

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 _ 54

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); 2 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 _ 55

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); 2 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 _ 56

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); 2 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 _ 57

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 _ 58

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 _ 59

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 3 _ 60

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); 4 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 3 _ 61

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); 4 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 3 _ 62

for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); 4 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 3 have come to exit_ 63

Example * ** *** **** ***** ****** #include <stdio.h> int main() { int iCounter = 0; int jCounter = 0; while (iCounter < 7) jCounter = 0; while (jCounter < iCounter) printf("*"); jCounter++; } printf("\n"); iCounter++; getch(); return 0;