Chapter 2 Control Structures.

Slides:



Advertisements
Similar presentations
CSC 123 Systems Analysis & Design
Advertisements

Describing Process Specifications and Structured Decisions Systems Analysis and Design, 7e Kendall & Kendall 9 © 2008 Pearson Prentice Hall.
Ratios * Of means to multiply Using percents  To calculate sales tax of an item, simply multiply the cost of the item by the tax rate.  Example: You.
Chapter 2: Input, Processing, and Output
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Chapter 3 Planning Your Solution
Review Algorithm Analysis Problem Solving Space Complexity
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
DCT 1123 Problem Solving & Algorithms
An ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time Let's examine the four parts of this.
Sales Tax, Discounts, and Commissions Section 6.7.
Describing Process Specifications and Structured Decisions Systems Analysis and Design, 7e Kendall & Kendall 9 © 2008 Pearson Prentice Hall.
Section 3.9 Percents Mr. Beltz & Mr. Sparks. Ratio A PERCENT is a ratio that compares a number to 100. You can write a percent as a FRACTION, DECIMAL,
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Chapter 2: General Problem Solving Concepts
Procedural Programming. Programming Process 1.Understand the problem 2.Outline a general solution 3.Decompose the general solution into manageable component.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
1 Program Planning and Design Important stages before actual program is written.
1. 2 Sales tax is calculated by finding the percent of the total purchase.
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.
Copyright © 2011 Pearson Education Process Specifications and Structured Decisions Systems Analysis and Design, 8e Kendall & Kendall Global Edition 9.
PROBLEM SOLVING. What is a Problem? A problem is a situation that needs to be resolved.
CIS 115 All Exercises Devry University (Devry) For more course tutorials visit CIS 115 All Exercises Devry University.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
CIS 115 All Exercises Devry University (Devry) FOR MORE CLASSES VISIT CIS 115 All Exercises Devry University.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Introduction to Programming
CIS 115 Slingshot Academy / cis115.com
Topics Designing a Program Input, Processing, and Output
Exercise : Write a program that print the final price of purchase at a store where everything costs exactly one dollar. Ask for the number of items purchased.
Introduction to Algorithms
1-1 Logic and Syntax A computer program is a solution to a problem.
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
Algorithm & Programming
Chapter 2: Input, Processing, and Output
Pseudocode Upsorn Praphamontripong CS 1110 Introduction to Programming
The Selection Structure
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.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithm and Ambiguity
CIS115 Education for Service-- snaptutorial.com
CIS 115 Teaching Effectively-- snaptutorial.com
Unit# 9: Computer Program Development
Problem Solving Techniques
Program Design Introduction to Computer Programming By:
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Programming Right from the Start with Visual Basic .NET 1/e
Programming Fundamentals (750113) Ch1. Problem Solving
Iteration: Beyond the Basic PERFORM
Chapter 2- Visual Basic Schneider
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Introduction to Algorithms and Programming
Chapter 2- Visual Basic Schneider
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Programming Fundamentals (750113) Ch1. Problem Solving
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
CHAPTER 4 Iterative Structure.
Topics Designing a Program Input, Processing, and Output
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Programming Fundamentals (750113) Ch1. Problem Solving
Topics Designing a Program Input, Processing, and Output
Topics Introduction to Repetition Structures
Just Basic Lessons 7&8 Mr. Kalmes.
Chapter 2: Input, Processing, and Output
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
WJEC GCSE Computer Science
Algorithms, Part 3 of 3 Topics In-Class Project: Tip Calculator
Introduction to Programming
Presentation transcript:

Chapter 2 Control Structures

Control Structures Solving a problem using the Stepwise refinement method, the instructions that make up the algorithm and the structured chart are combinations of 3 control structures: Sequence Structure Selective Structure Iterative Structure

Sequence Structure The most commonly used and simple structure Consists of two or more sub components Instructions are executed/processed one after another in a sequence, in a top to bottom manner The structure is read from left to right Normally used in conjunction with one or more of the other control structures; ie, selection and/or iteration

Sequence Structure The instructions are represented in the subcomponents of the structured diagram , i.e., elementary components An elementary component is one which has no lower-level components associated and is represented in a data structure diagram simply by placing its name inside a rectangle

Sequence Structure The sequence control structure can be used to represent 4 basic computer operations; to receive information e.g. Read customername, address put out information e.g. Display pageCount perform arithmetic e.g. add 1 to pageCount assign values e.g. set pageCount to zero

Sequence Structure: General B C The component (box) A is a sequence. Boxes B and C (Elementary components) represent component parts of the sequence (sub-components) A is a sequence of B followed by C.

Sequence structure: Pseudocode use a separate line for each logical element. align each statement vertically until a change in logic flow occurs Sequence block is preceded by BEGIN, terminated by END

Sequence Structure: Pseudocode General format: BEGIN SEQUENCE_BLOCK_NAME sequence sequence statement; … END SEQUENCE_BLOCK_NAME sequence

Sequence Structure: Example 1 Name Read Title Read FirstName Read Surname Print full name Sub-components of a Sequence Structure - Name

Sequence Structure: Pseudocode Example 1 BEGIN NAME sequence Read Title FirstName Surname Name Print full name READ title; READ firstname; READ surname; WRITE title + firstname +surname; END NAME sequence

Sequence Structure: Example 2 Problem Specification: Design a program to process an order. A process consists of accepting the quantity sold and price of an item and then display the sales value

Problem Analysis Initialization Input Definition Output Definition none Input Definition qty, price Output Definition sales : $ 999.99

Problem Analysis Processing Requirements Read qty, price Calculate sales sales = qty * price Display sales Processing Control sales is displayed in 2 decimal places

Problem Analysis Test Data & Expected Result qty price sales 12 10.00 120.00 5 100.00 500.00 10 2.00 20.00

Sequence Structure: Example 2 Order Display sales sales = qty * price Read qty, price

Sequence Structure: Example 2 Pseudocode: BEGIN ORDER sequence END ORDER sequence Order Display sales sales = qty * price Read qty,price READ qty, price; sales = qty * price; WRITE sales;

Sequence Structure: Example 3 Problem Specification: Calculate the amount due on an electricity bill. The present meter reading and the previous meter readings are entered. The amount due is the difference between the two readings multiplied by the cost of a unit of electricity, currently 5 cents per unit.

Sequence Structure: Your task! Do the PROBLEM ANALYSIS Draw the STRUCTURED CHART Write the PSEUDOCODE

Problem Analysis Initialization Input Definition Output Definition Cost = 0.05 Input Definition current, previous Output Definition Amount due : $ 999.99

Problem Analysis Processing Requirements Read current, previous Calculate bill 2.1. used = current – previous 2.2. bill = used * cost Display bill Processing Control current value must be >= previous value bill is displayed in 2 decimal places

Problem Analysis Test Data & Expected Result current previous used bill 1200 680 520 26.00 5000 1328 3672 183.60 10000 13400 error

Sequence Structure: Example 3 Electricity Bill Cost = 0.05 Read meters Calculate bill Display bill Read current Read previous used = current – previous bill = used*cost

Sequence Structure: Example 3 BEGIN ELECTRICITYBILL sequence cost = 0.05; BEGIN READMETERS sequence READ current; READ previous; END READMETERS sequence BEGIN CALCULATEBILL sequence used = current – previous; bill = used * cost; END CALCULATEBILL sequence WRITE bill; END ELECTRICITYBILL sequence Electricity Bill Calculate bill Display bill cost = 0.05 Read meters Read current Read previous used = current – previous bill = used*cost

Sequence Structure: Example 4 A ONE DOLLAR shop wants you to write a program to read in the sales price in cents which is not to exceed one dollar. The change from one dollar is then to be calculated and printed as the most compact set of change (i.e. the fewest coins). It is assumed that the denominations are 50¢, 20¢, 10¢, 5¢ and 1¢

Sequence Structure: Example 4 Initialization none Input Definition sales_price Output Definition Denomination: 50¢ - 9 20¢ - 9 10¢ - 9 5¢ - 9 1¢ - 9

Problem Analysis Processing Requirements Read sales_price Calculate change from $1 change = 100 – sales_price Calculate set of change coin50 = change / 50 change = change – (coin50 * 50) coin20 = change / 20 change = change – (coin20 * 20) coin10 = change / 10 change = change – (coin10 * 10) coin5 = change / 5 change = change – (coin5 * 5) coin1 = change Print coin50, coin20, coin10, coin5, coin1

Problem Analysis Processing Control Test Data & Expected Result sales_price must be between 1 and 100 cents inclusive Test Data & Expected Result sales_price change coin50 coin20 coin10 coin5 coin1 40 60 1 14 86 120 error

Sequence Structure: Example 4 Change Read sales_price change = 100 – sales_price Set of change Display change A B

Sequence Structure: Example 4 coin50 = change/50 coin20 = change/20 coin10 = change/10 coin1 = change coin5 = change/5 change= change – (coin50*50) change= change – (coin20* 20) change= change – (coin10*10) change= change – (coin5*5)

Sequence Structure: Example 4 B coin50 coin20 coin10 coin5 coin1

Sequence Structure: Example 4 coin5 = change / 5; change = change - (coin5*5); coin1 = change; END SETOFCHANGE sequence BEGIN DISPLAYCHANGE sequence WRITE coin50; WRITE coin20; WRITE coin10; WRITE coin5; WRITE coin1; END DISPLAYCHANGE sequence END CHANGE sequence Pseudocode BEGIN CHANGE sequence READ sales_price; change = 100 – sales_price; BEGIN SETOFCHANGE sequence coin50 = change/50; change = change - (coin50*50); coin20 = change / 20; change = change - (coin20*20); coin10 = change / 10; change = change-(coin10*10);

Class Exercise Using stepwise Refinement method (sequence control structure) analyse the following problem, draw the structured chart and write the pseudocode: A major department store needs a program to prepare a bill for each customer. Lets assume that each customer purchases only one item type. There will be 4 input values required; customer’s name, itemID, quantity purchased and unit price. Output will be the customer’s bill after a 10% discount is taken before taxes and a 5 percent sales tax is added.