Program Design Lecture 4 Mohammed Arif Mazumder Senior

  • Slides: 27
Download presentation
Program Design Lecture 4 Mohammed Arif Mazumder Senior Lecturer Daffodil International University

Program Design Lecture 4 Mohammed Arif Mazumder Senior Lecturer Daffodil International University

This Lesson Includes Following section Program Design & Algorithm Flow Chart 2

This Lesson Includes Following section Program Design & Algorithm Flow Chart 2

Computer Program A computer program is a set of detailed directions telling the computer

Computer Program A computer program is a set of detailed directions telling the computer exactly what to do, one step at a time. A program can be as short as one line of code, or as long as several millions lines of code. Typically, a program is stored as a collection of files. Computer has to be precisely told what it needs to do; otherwise: 3

Program Design The Evolution of Programming Languages To build programs, people use languages that

Program Design The Evolution of Programming Languages To build programs, people use languages that are similar to human language. The results are translated into machine code, which computers understand Programming has changed a lot since the first computers were created. They can be categorized based on how close to normal speech they are, and thus how far from the computer's internal language. 1 st GL • Machine Language (1945( 2 nd GL • Assembly Language (1950( 3 rd GL • High Level Language (1960( 4 th GL • Very High Level Language (1970( 5 th GL • Natural Language (1980( 4

Five generations of Programming languages

Five generations of Programming languages

1 GL: Machine language 1 st generation: machine language • Written in binary •

1 GL: Machine language 1 st generation: machine language • Written in binary • Different for every CPU • Used for programming computers in the earliest days of computers. • Computers only understand this language

2 GL: Assembly language 2 nd generation: assembly language Machine language • Symbolic representation

2 GL: Assembly language 2 nd generation: assembly language Machine language • Symbolic representation of machine language. LOAD BASEPAY – opcodes ADD OVERPAY STORE GROSSPAY – operands – labels • More readable to human (not computer). add A, B Assembly language 1000111000010110 • Easy to translate to machine language. Assembly language Machine language

3 GL- High-level language 3 rd generation: high-level language • Supports structured and object-oriented

3 GL- High-level language 3 rd generation: high-level language • Supports structured and object-oriented programming (OOP) • Abstracts the hardware implementation from the programmer • Code is reusable and portable • May be compiled/ or interpreted

Program execution (C example)

Program execution (C example)

4 GL-Very high-level languages 4 th generation: very high-level language Non procedural high-level specification

4 GL-Very high-level languages 4 th generation: very high-level language Non procedural high-level specification language. 4 GL open up the development environment to a wider population Examples: Database query language: SQL; General use: Power. Builder;

5 GL- Natural languages 5 th generation: natural language § Algorithms do not have

5 GL- Natural languages 5 th generation: natural language § Algorithms do not have to be specified § Aims to make the programmer redundant § May not be created yet § Will create software automatically

Commonly used Programming languages

Commonly used Programming languages

C Pytho n Pascal Cobol . Net Perl Microsoft Basic Common Program Languages C++

C Pytho n Pascal Cobol . Net Perl Microsoft Basic Common Program Languages C++

Program Design - Algorithm An algorithmis a sequence of finite instructions, often used for

Program Design - Algorithm An algorithmis a sequence of finite instructions, often used for calculation and data processing. Start with a real-world problem that needs to be solved. Convert the real-world problem into a computational problem Develop an algorithm and use it to solve the computational problem An algorithm is a precise list of instructions that determine what operations to perform on what pieces of data, in what order. Lets see How to Cook ? 14

Program Design - Pseudocode Generic way of describing an algorithm, without use of any

Program Design - Pseudocode Generic way of describing an algorithm, without use of any specific programming language. It Helps programmers to plan an algorithm. This is not an actual programming language, but may borrow syntax from popular programming languages. Example Problem: Calculate the bill when someone buys a specific number of some item: Pseudocode: READ number of items being purchased READ price per item CALCULATE subtotal CALCULATE tax CALCULATE total DISPLAY total 15

Program Design - Pseudocode Example Problem: Known Values Inputs Calculations Outputs Calculate two childrens’

Program Design - Pseudocode Example Problem: Known Values Inputs Calculations Outputs Calculate two childrens’ allowances, based upon 75 cents per year old. Rate = 75 cents per year Ages of children Allowance = Age x Rate Allowances for each child Pseudo Code Algorithm: PROMPT for Age of Child 1 READ Age of Child 1 PROMPT for Age of Child 2 READ Age of Child 2 CALCULATE Allowance for Child 1 = Age of Child 1 x Rate CALCULATE Allowance for Child 2 = Age of Child 2 x Rate DISPLAY Allowance for Child 1 DISPLAY Allowance for Child 2 16

Flow Chart - Flow Control Design Tool Flowchart - a graphical way of writing

Flow Chart - Flow Control Design Tool Flowchart - a graphical way of writing algorithms Rectangle is used for calculations Parallelogram is used for input and output Circle is used as connector Diamond is used as decision Symbols are connected by arrows to represent the order of the operations 17

Flow Chart – Flow Control Symbol Process Alternet Process Decision Data Document Manual Input

Flow Chart – Flow Control Symbol Process Alternet Process Decision Data Document Manual Input Manual Operation Connector Predefined Process Display Internal Storage Stored Data Terminator Extract - Marge 18

Flow Chart – Flow Control Symbol § Every solution starts somewhere and terminates somewhere

Flow Chart – Flow Control Symbol § Every solution starts somewhere and terminates somewhere § Every flowchart must have one start symbol and one end symbol § Start and end symbols are ovals § § A start symbol denotes the start of the algorithm An end symbol indicates the algorithm has terminated Start Solution Stop 19

Flow Chart – Flow Control Symbol Build an application that asks • User to

Flow Chart – Flow Control Symbol Build an application that asks • User to input two numbers. Then • Calculates the sum of the two numbers • Tells the user which one is the larger number. Start Input first_number Input second_number Sum = first_number + second_number Output Sum Stop 20

Flow Chart – Flow Control Symbol Calculate two children‘s allowances, based upon 75 cents

Flow Chart – Flow Control Symbol Calculate two children‘s allowances, based upon 75 cents per year old. start prompt Age 1 input Age 1 prompt Age 2 Previous Pseudocode Algorithm: 1. READ Age of Child 1 2. PROMPT for Age of Child 1 3. READ Age of Child 2 4. PROMPT for Age of Child 2 5. CALCULATE Allowance for Child 1 = Age of Child 1 x Rate 6. CALCULATE Allowance for Child 2 = Age of Child 2 x Rate 7. DISPLAY Allowance for Child 1 8. DISPLAY Allowance for Child 2 input Age 2 Allow 1 = Age 1 x Rate Allow 2 = Age 2 x Rate Print Allow 1 Print Allow 2 stop 21

Flow Chart – Flow Control Symbol Take 2 number from user and find out

Flow Chart – Flow Control Symbol Take 2 number from user and find out which one is a bigger number Start Input first_number Input second_number YES Is first_number greater than second_number? NO Output second_number is bigger Output first_number is bigger Stop 22

Flow Chart – Flow Control Symbol Start Input first_number Input second_number Sum = first_number

Flow Chart – Flow Control Symbol Start Input first_number Input second_number Sum = first_number + second_number TRUE Is first_number greater than second_number? Output Sum & first_number is bigger FALSE Output Sum & second_number is bigger Stop 23

Flow Chart – Flow Control Symbol Lets think about this … Start Write a

Flow Chart – Flow Control Symbol Lets think about this … Start Write a program to output the sum of 1 to 10. Sum =0 Number = 1 That is, 1 + 2 + 3 + 4 + 5 + 6 +7 +8 +9 +10 Sum = Sum + Number = Number +1 True Is Number < 10? False Output Sum Stop 24

Flow Chart – Flow Control Symbol Calculate one child’s allowance, based upon 75 cents

Flow Chart – Flow Control Symbol Calculate one child’s allowance, based upon 75 cents per year old if s/he is under 10, and $1 per year if 10 or over. • start Previous Pseudocode Algorithm: – PROMPT for Age prompt Age – READ Age input Age – IF Age less than 10 – THEN CALCULATE Allowance = Age x Young. Rate – ELSE CALCULATE Allowance = Age x Older. Rate TRUE Age < 10 Allow = Age x Young. Rate FALSE Allow = Age x Older. Rate – DISPLAY Allowance Print Allow stop 25

Flow Chart – Flow Control Symbol Calculate the total allowance paid to each of

Flow Chart – Flow Control Symbol Calculate the total allowance paid to each of three children at $1 per year old. • Previous Pseudo code Algorithm: – Set Kids. Paid to 0 – Set Total to 0 start Kids. Paid = 0 Total = 0 TRUE Kids. Paid < 3 FALSE Prompt Age – WHILE Kids. Paid < 3 DO – PROMPT for Age – READ Age – CALCULATE Allowance = Age x Rate – ADD Allowance to Total – INCREMENT Kids. Paid – DISPLAY Total Read Age Calc Allowance Display Total stop Add Allowance to Total Increment Kids. Paid 26

Flow Chart – Flow Control Symbol Write pseudo code and draw flowchart for printing

Flow Chart – Flow Control Symbol Write pseudo code and draw flowchart for printing Sequesnce: 1, 3, 5, 9, …. N Start Input N 1. 2. 3. 4. Start Input N Set Count = 1 If Count < N 4 a. Yes go to Step 5 4 b. No, go to Step 7 5. Print Count 6. Count = Count +2 , Go to Step 4 7. End I=1 When I<=N Output I I = I+ 2 End 27