Fundamentals of Programming

Similar documents
Fundamental of Programming (C)

Introduction to Flowcharting

Control Structure: Loop

depicts pictorially schematic representation of an algorithm document algorithms. used in designing or documenting

Structured Program Development

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

Introduction. C provides two styles of flow control:

Fundamentals of Programming. Lecture 6: Structured Development (part one)

Chapter 3 Structured Program Development

Fundamental of Programming (C)

Introduction to Programming

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Theory of control structures

Chapter 2 - Control Structures

Introduction to C Programming

BIL101E: Introduction to Computers and Information systems Lecture 8

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs

BRANCHING if-else statements

LECTURE 5 Control Structures Part 2

Decision Making and Loops

Chapter 2 - Control Structures

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Chapter 2 - Control Structures

Module 4: Decision-making and forming loops

Structured Program Development in C

Outline. Program development cycle. Algorithms development and representation. Examples.

Decision Making -Branching. Class Incharge: S. Sasirekha

Computer System and programming in C

FLOW CHART AND PSEUDO CODE

Lecture 6. Statements

Computers Programming Course 7. Iulian Năstac

Chapter 5: Control Structures

Chapter Two: Program Design Process and Logic

Chapter 13 Control Structures

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

REPETITION CONTROL STRUCTURE LOGO

3 The L oop Control Structure

A control structure refers to the way in which the Programmer specifies the order of executing the statements

CS1100 Introduction to Programming

UIC. C Programming Primer. Bharathidasan University

C: How to Program. Week /Mar/05

Flow Chart & Algorithms

Chapter 13. Control Structures

Week 1 Introduction to Computer and Algorithm (Part1) UniMAP Sem II 11/12 DKT121: Basic Computer Programming 1

Fundamentals of Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Fundamental of Programming (C)

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Name Roll No. Section

Chapter 2 - Introduction to C Programming

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Comments. Comments: /* This is a comment */

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Lab Session # 3 Conditional Statements. ALQUDS University Department of Computer Engineering

SELECTION STATEMENTS:

Score score < score < score < 65 Score < 50

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

IT 1033: Fundamentals of Programming Loops

Course Outline Introduction to C-Programming

Unit 7. Functions. Need of User Defined Functions

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Question Bank (SPA SEM II)

Chapter 13 Control Structures

Lecture 10. Daily Puzzle

Lecture 5 Tao Wang 1

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Repetition Algorithms

DECISION CONTROL AND LOOPING STATEMENTS

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

EECE.2160: ECE Application Programming Spring 2018

Repetition Structures II

Control Statements. If Statement if statement tests a particular condition

Programming for Engineers Iteration

The four laws of programs

1 EEE1008 C Programming

PSEUDOCODE AND FLOWCHARTS. Introduction to Programming

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Lecture 5: C programming

4*4*4 2. What is the output of the following flowchart for the values given below: (25 p)

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

CpSc 1111 Lab 4 Formatting and Flow Control

ALGORITHMS AND FLOWCHARTS

Topic 6: A Quick Intro To C

Transcription:

Fundamentals of Programming Lecture 5 - Structured Program Development Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad

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

Requirements Discovery Problem Analysis Problem Identification Abstraction Inputs and outputs determination Defining their relation Write a program to compute an employee's weekly pay? 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 3

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

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 5

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 6

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 7

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: 1. Read Hours and Pay Rate 2. weekly pay = Read Hours * Pay Rate 8

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

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

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

Processes indicates a process such as a mathematical computation or variable assignment Multiply Hours by Pay Rate. Store result in Gross Pay. Process 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 12

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

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

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

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

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

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

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

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

Sequence Decision selection statement Control Structures 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 21

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

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, { } 23

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

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

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" ); } 26

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

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

int k = 1, m = 4; if (k < 2 m == 3) { m = 2 + k; printf("%d", m); } else { k = 1; printf("%d", k); } Example 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? 29

if(x) if(y) printf("yes"); else printf("no"); if (x < 0) sign = -1; else if (x == 0) sign = 0; else sign = 1; Example 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) { if(y) printf("yes"); } else printf("no"); 30

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

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

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 33

switch switch (variable) { case const: statements...; default: statements...; } switch (c = toupper(getch())) { case R : printf("red"); break; case G : printf("green"); break; default: printf("other"); } 34

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

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? YES Process A 36

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

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

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? YES Display x 39

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

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 YES x < y? Display x Add 1 to x 41

while (1); Example int counter = 0; while (counter < 1000) ; int counter = 0; while (counter < 9) 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 ++); } 42

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 C programming language do { printf( ); x++; } while (x < y); Display x Add 1 to x x < y? YES 43

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

Draw a flowchart for the following problem: Example 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 45

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

Assume input example: 2 3 4 5 6 counter 1, sum 0 counter 12 34 56 sum 02 59 14 20 14 02 59 + 23 45 6 counter < 6 true input n sum sum + n counter++ output sum false 61 23 54 < < 6 6 false true n 23 45 6 The This Uses counter loop only is counter-controlled Increases 3 variables by 1 47

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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