Introduction to Pseudocode

Slides:



Advertisements
Similar presentations
Programming Types of Testing.
Advertisements

Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
ITEC113 Algorithms and Programming Techniques
TEL 104 / MKK Fundamental Programming: Lecture 3
Steps in Program Development
Logic and Algorithm. Developing an algorithm To help the initial analysis, the problem should be divided into 3 separate components: 1.Input: a list of.
1 TEL 104 / MKK Fundamental Programming: Lecture 4.
Program Design and Development
Repetition Control Structures
Pseudocode.
Chapter 1 Program Design
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
Pseudocode.
Chapter 3 Planning Your Solution
Chapter 9 Structuring System Requirements: Logic Modeling
DCT 1123 Problem Solving & Algorithms
Sw development1 Software Development 1.Define the problem (Analysis) 2.Plan the solution 3.Code 4.Test and debug 5.Maintain and Document.
Pseudocode algorithms using sequence, selection and repetition
Simple Program Design Third Edition A Step-by-Step Approach
First Steps in Modularization Simple Program Design Third Edition A Step-by-Step Approach 8.
Chapter 3 Developing an algorithm. Objectives To introduce methods of analysing a problem and developing a solution To develop simple algorithms using.
Developing an Algorithm
Selection Control Structures Simple Program Design Third Edition A Step-by-Step Approach 4.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Developing an Algorithm
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
EXERCISES for ALGORITHMS WRITING
Pseudocode Algorithms Using Sequence, Selection, and Repetition Simple Program Design Third Edition A Step-by-Step Approach 6.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Developing an Algorithm. Simple Program Design, Fourth Edition Chapter 3 2 Objectives In this chapter you will be able to: Introduce methods of analyzing.
Cosc175 - Define Problem/Design Solution/Pseudocode/Trace 1 DEFINE THE PROBLEM.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
1 Program Planning and Design Important stages before actual program is written.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Computer Programming CONTENTS Introduction to Operating Systems Introduction to programming languages Introduction to perl programming language Programming.
The Hashemite University Computer Engineering Department
Flowcharts. Symbol Terminal Symbol: indicates the starting or stopping pointin the logic. Input/Output Symbol: Represents an input or output process in.
Flowcharts Lecture 12.
LO: We’re learning to outline a program using Pseudo Code.
Lecture Notes 1/20/05 Pseudocode.  Pseudocode standard which we will follow in this class: - Statements are written in simple English; - Each instruction.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
Chapter 3 Program Design
Algorithm & Programming
Chapter 2: Input, Processing, and Output
Chapter 9 Structuring System Requirements: Logic Modeling
Chapter 2- Visual Basic Schneider
PROGRAM CONTROL STRUCTURE
Introduction To Flowcharting
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
CHAPTER 2 & 3: Pseudocode and Developing and Algorithm
Designing and Debugging Batch and Interactive COBOL Programs
Program Design Introduction to Computer Programming By:
Pseudocode.
Chapter 9 Structuring System Requirements: Logic Modeling
Pseudocode algorithms using sequence, selection and repetition
Chapter 9 Structuring System Requirements: Logic Modeling
1) C program development 2) Selection structure
Lecture Notes 8/24/04 (part 2)
Chapter 2- Visual Basic Schneider
Introduction to Algorithms and Programming
CHAPTER 1 Introduction to Structured Programming.
CHAPTER 4 Iterative Structure.
Flowcharts and Pseudo Code
Chapter 9 Structuring System Requirements: Logic Modeling
Chapter 9 Structuring System Requirements: Logic Modeling
Chapter 5 Desk Checking/Dry Running.
Presentation transcript:

Introduction to Pseudocode

What is Pseudocode? One of the popular representation of Algorithm Widely choosen because: easy to read and write allow the programmer to concentrate on the logic of the problem Structured in English language

Pseudocode Convention Statement are written in simple English Each instruction is written on a separate line Keywords and indentation are used to signify particular control structures. Each set of instructions is written from top to bottom, with only one entry and one exit. Groups of statements may be formed into modules, and that group given a name.

Six Basic Computer Operations A computer can receive information Verb used: Read  used when the algorithm is to receive the input from a record on a file Get  used when the algorithm is to receive input from the keyboard. Read student name Get system date Read number_1, number_2 Get tax_code

2. A computer can put out information Verb used: Print  used when the output is to be sent to the printer Write  used when the output is to be written to a file Put, Output, Display  used when the output is to be written to the screen Prompt  required before an input instruction Get, causes the message to be sent to the screen which requires the user responds, usually by providing input. Print `Program Completed´ Write customer record to master file Put out name, address and postcode Output total_tax Display ´End of data´ Prompt for student_mark Get student_mark

3. A computer can perform arithmetic Verb used: Compute Calculate Symbols used: +, -, *, /, () Add number to total Total = total + number Divide total_marks by student_count Sales_tax = cost_price * 0.10 Compute C = (F – 32) * 5/9

A computer can assign a value to a variable or memory location Three cases : To give data an initial value in pseudocode, the verbs Initialise or Set are used To assign a value as a result of some processing, the symbols ´=´or ´´ are written To keep a variable for later use, the verbs Save or Store are used. Initialize total_price to zero Set student_count to 0 Total_price = cost_price + sales_tax Total_price  cost_price + sales_tax Store customer_num in last_customer_num

A computer can compare two variables and select one of two alternate actions Keyword used: IF, THEN, ELSE IF student_attendance_status is part_time THEN add 1 to part_time_count ELSE Add 1 to full_time_count ENDIF

6. A computer can repeat a group of actions Keyword used: DOWHILE, ENDDO DOWHILE student_total < 50 Read student record Print student name, address to report Add 1 to student_total ENDDO

Meaningful names When designing a solution algorithm, a programmer should introduce unique names, which are: Represent the variables or objects in the problem Meaningful example: number1, number2, number3  more meaningful than A, B, C Used word separator if more than one word example: sales_tax, word_count Or Capital letter as separator example: salesTax, wordCount

The Structure Theorem It is possible to write any computer program by using only three basic control structures that are easily represented in pseudocode: Sequence Selection Repetition

Sequence Add 1 to pageCount Print heading line 1 Print heading line 2 Is the straightforward execution of one processing step after another. Statement a Statement b Statement c Add 1 to pageCount Print heading line 1 Print heading line 2 Set lineCount to zero Read customer record

Selection IF condition p is true THEN ELSE statement(s) in false case Presentation of condition and the choice between two actions IF condition p is true THEN statement(s) in true case ELSE statement(s) in false case ENDIF Example: IF student_attendance_status is part_time THEN add 1 to part_time_count ELSE add 1 to full_time_count ENDI>f

Repetition DOWHILE condition p is true statement block ENDDO The presentation of a set of instructions to be performed repeatedly, as long as a condition is true DOWHILE condition p is true statement block ENDDO Example: Set student_total to zero DOWHILE student_total < 50 Read student record Print student name, address to report Add 1 to student_total ENDDO

DESIGNING A SOLUTION ALGORITHM The most challengin task in the life cycle of a program First attempt usually doesnt result in a finished product Keep altering the step and algorithms till satesfied result achieved.

Point to be considered in Solution Algorithm A name should be given to the algorithm, which is describe the function of algorithm An END statement used to indicate the algorithm is complete All processing steps between the algorithm name and END statement should be indented for readability. Each processing step in the defining diagram relates directly to one or more statements in the algorithm.

Example 3.1. Solution Algorithm for example 2.1 A program is required to read three numbers, add them together and print their total.

Defining diagram Input Processing Output Number1 Number2 Number3 total

Solution Algorithm Add_three_numbers END Read number1, number2, number3 Total = number1 + number2 + number3 Print total END

Example 3. 2. Find average temperature A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Defining diagram Input Processing Output Max_temp Min_temp Prompt for temperatures Get temperatures Calculate average temperature Display average temperature Avg_temp

Solution Algorithm Find average_temperature Prompt operator for max_temp, min_temp Get max_temp, min_temp Avg_temp= (max_Temp + min_temp)/2 Output avg_temp to the screen END

What about this ? - Compute mowing time A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.

Solution Algorithm Calculate_mowing_time END Prompt operator for block_lenght, block_width Get block_length, block_width block_area = block_lenght*block_width Prompt operator for house_lenght, house_width Get house_lenght, house_width house_area=house_lenght*house_width Mowing_area=block_area-house_area Mowing_time=mowing_area/2 Output mowing_time to screen END

Checking the solution algorithm (Desk Checking) Tracing through the logic of the algorithm with some chosen data..

Step in desk Checking an algorithm Choose valid simple input test case (2-3 enough) Establish what the expected result should be. Make a table of relevant variable names Checking the test case line by line, step by step Repeat process 4 for other test case Check if expected result 2 matches with actual result 5

Example 3.4. Desk Chek for example 2.1 A program is required to read three numbers, add them together and print their total.

Solution Algorithm Add_three_numbers END Read number1, number2, number3 Total = number1 + number2 + number3 Print total END

Desk Checking Choose two sets input test data. Set 1: 10,20, 30 and Set 2: 40, 41, 42 Data Set 1 Data Set 2 Number 1 10 40 Number 2 20 41 Number 3 30 42

2. Establish the expected result for each test case Data Set 1 Data Set 2 Total 60 123

3. Set up a table of relevant variable names, and pass each test data set statement by statement. Statement number number1 number2 number3 total First Pass 1 10 20 30 2 60 3 Print Second Pass 40 41 42 123

4. Check the expected results (60 and 123) match the actual results.

Desk Check of Example 3. 2. A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Solution Algorithm Find average_temperature Prompt operator for max_temp, min_temp Get max_temp, min_temp Avg_temp= (max_Temp + min_temp)/2 Output avg_temp to the screen END

Desk Checking Choose two sets input test data. Set 1: 30, 10 and Set 2: 40, 20 Data Set 1 Data Set 2 Max_temp 30 40 Min_temp 10 20

2. Establish the expected result for each test case Data Set 1 Data Set 2 Avg_temp 20 30

3. Set up a table of relevant variable names, and pass each test data set statement by statement. Statement number Max_temp Min_temp Avg_temp First Pass 1,2 30 10 3 20 4 0utput Second Pass 40 output

4. Check the expected results match the actual results.

Assignment 2: Desk Checking for Compute mowing time A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.

Solution Algorithm Calculate_mowing_time END Prompt operator for block_lenght, block_width Get block_length, block_width block_area = block_lenght*block_width Prompt operator for house_lenght, house_width Get house_lenght, house_width house_area=house_lenght*house_width Mowing_area=block_area-house_area Mowing_time=mowing_area/2 Output mowing_time to screen END

Calculate_mowing_time Assignment 3 – Desk Checking for Mowing_time which now contains a logic error Calculate_mowing_time Prompt operator for block_lenght, block_width Get block_length, block_width block_area = block_lenght * block_width Prompt operator for house_lenght, house_width Get house_lenght, house_width house_area=block_lenght * block_width Mowing_area=block_area - house_area Mowing_time=mowing_area/2 Output mowing_time to screen END

Rule of Assignment Submission Submit at the latest one day before the following class begin. Submission after the time will be considered as delay and affect the mark. Use a combination of your name and assignment number as file name. For example: BasnendarE_Assigment1.doc