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.

Slides:



Advertisements
Similar presentations
Repetition Control Structures
Advertisements

Programming Types of Testing.
Program Design Tool. 6 Basic Computer Operations Receive information Put out information Perform arithmetic Assign a value to variable (memory location)
Selection control structures
TEL 104 / MKK Fundamental Programming: Lecture 3
1 TEL 104 / MKK Fundamental Programming: Lecture 4.
Program Design and Development
Repetition Control Structures
Pseudocode.
Pseudocode Algorithms Using Sequence, Selection, and Repetition.
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
Pseudocode.
Chapter 1 Pseudocode & Flowcharts
DCT 1123 Problem Solving & Algorithms
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Sw development1 Software Development 1.Define the problem (Analysis) 2.Plan the solution 3.Code 4.Test and debug 5.Maintain and Document.
12-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Combination of Sequence, Selection and Repetition
Pseudocode algorithms using sequence, selection and repetition
DCT 1123 Problem Solving & Algorithms
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.
Describing Process Specifications and Structured Decisions Systems Analysis and Design, 7e Kendall & Kendall 9 © 2008 Pearson Prentice Hall.
Input, Output, and Processing
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.
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
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
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Writing Program Code in BASIC Write a program to prompt for and accept values into TWO variables, numx and numy. The program should square the value stored.
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.
Pseudocode Algorithms Using Sequence, Selection, and Repetition
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
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.
Chapter 3: Assignment, Formatting, and Interactive Input.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
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.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
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.
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.
CS 240 Computer Programming 1
CIS 115 All Exercises Devry University (Devry) FOR MORE CLASSES VISIT CIS 115 All Exercises Devry University.
Chapter 2: Input, Processing, and Output
For Monday Read WebCT quiz 18.
Program Design Introduction to Computer Programming By:
Pseudocode algorithms using sequence, selection and repetition
For Wednesday No new reading No quiz.
CHAPTER 1 Introduction to Structured Programming.
Problem Solving Skill Area 305.1
Flowcharts and Pseudo Code
CHAPTER 6 Testing and Debugging.
Chapter 5 Desk Checking/Dry Running.
Introduction to Pseudocode
Presentation transcript:

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 the source data provided to the problem 2.Output: a list of the outputs required 3.Processing: a list of actions needed to produce the required outputs.

Example 1. Add three numbers  A program is required to read three numbers, add them together and print their total.

Solution: 1.Underline the nouns and adjectives used in the specification  establish the input, output component and any object that are required. A program is required to read three numbers, add them together and print their total.

Defining diagram InputProcessingOutput Number1 Number2 Number3 total

2.Underline the verbs and adverbs used in the specification  establish the action required. A program is required to read three numbers, add them together and print their total.

Defining diagram InputProcessingOutput Number1 Number2 Number3 Read three numbers Add numbers together Print total number total

3.Writing down the processing steps in an algorithm, Read three numbers Add numbers together Print total number

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

Example 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.

Step 1 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 InputProcessingOutput Max_temp Min_temp Avg_temp

Step 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.

Defining diagram InputProcessingOutput 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

Example 3. 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

Step 1 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.

Defining diagram InputProcessingOutput Block_lenght Block_width House_lenght House_width Mowing_time

Step 2 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.

Defining diagram InputProcessingOutput Block_lenght Block_width House_lenght House_width Prompt for block measurements Get block measurements Prompt for house measurements Get house measurements Calculate mowing area Calculate mowing time Mowing_time

Solution Algorithm 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=house_lenght*house_width Mowing_area=block_area-house_area Mowing_time=mowing_area/2 Output mowing_time to screen END

Desk Checking

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

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

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

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

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

2. Establish the expected result for each test case Data Set 1Data Set 2 Total60123

3. Set up a table of relevant variable names, and pass each test data set statement by statement. Statement number number1number2number3total First Pass Print Second Pass Print

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

Desk Check of Example 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 1.Choose two sets input test data. Set 1: 30, 10 and Set 2: 40, 20 Data Set 1Data Set 2 Max_temp3040 Min_temp1020

2. Establish the expected result for each test case Data Set 1Data Set 2 Avg_temp2030

3. Set up a table of relevant variable names, and pass each test data set statement by statement. Statement number Max_tempMin_tempAvg_temp First Pass 1, utput Second Pass 1, 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 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

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

Calculate_mowing_time 1 Prompt operator for block_lenght, block_width 2 Get block_length, block_width 3 block_area = block_lenght*block_width 4 Prompt operator for house_lenght, house_width 5 Get house_lenght, house_width 6 house_area=house_lenght*house_width 7 Mowing_area=block_area-house_area 8 Mowing_time=mowing_area/2 9 Output mowing_time to screen END Assignment 2 Review:

Desk Checking 1.Input data: Data Set 1Data Set 2 Block_lenght3040 Block_widht3020 House_lenght20 House_width2010

2. Expected result: Data Set 1Data Set 2 Mowing_time250 minutes300 minutes

3. Set up a table of relevant variable names, and pass each test data set statement by statement. Statement number Block_lenghtBlock_widthHouse_lenghtHouse_widthBlock_are a House_areaMowing_are a Mowing_time First Pass 1, , Output Second Pass 1, , Output

4. Check the expected results match the actual results.

What about This? Calculate_mowing_time 1 Prompt operator for block_lenght, block_width 2 Get block_length, block_width 3 block_area = block_lenght*block_width 4 Prompt operator for house_lenght, house_width 5 Get house_lenght, house_width 6 house_area=house_lenght*house_width 7 Mowing_area=block_area-house_area 8 Mowing_time=mowing_area/2 9 Output mowing_time to screen END

Desk Checking 1.Input data: Data Set 1Data Set 2 Block_lenght3040 Block_widht3020 House_lenght20 House_width2010

2. Expected result: Data Set 1Data Set 2 Mowing_time250 minutes300 minutes

3. Desk Check Table Statement number Block_lenghtBlock_widthHouse_leng ht House_widt h Block_areaHouse_areaMowing_are a Mowing_tim e First Pass 1, , Output Second Pass 1, , Output

4. Check the expected results match the actual results. We found that: –The calculation for the house_area is incorrect, the result is zero, which cannot be right. –The algorithm needs to be adjusted: house_area=block_lenght * block_width –Is changed to: house_area=house_lenght * house_width

Example 5. Process Customer Record A program is required to read a customer‘s name, a purchase amount and a tax code. The tax code has been validated and will be one of the following: 0baby needs tax (0%) 1Vegetables and food tax (3%) 2drink sales tax (5%) 3special sales tax (7%) The program must then compute the sales tax and the total amount due, and print the customer‘s name, purchase amount, sales tax and total amount due.

Defining diagram InputProcessingOutput Cust_name Purch_amt Tax_code Read customer details Compute sales tax Compute total amount Print customer details Cust_name Purch_amt Sales_tax total_amt

Solution Algorithm Process_customer_record 1Read cust_name, purch_amt,tax_code 2IF tax_code = 0 THEN sales_tax = 0 ELSE IF tax_code = 1 THEN sales_tax = purch_amt * 0.03 ELSE IF tax_code = 2 THEN sales_tax = purch_amt * 0.05 ELSE sales_tax = purch_amt * 0.07 ENDIF 3total_amt = purch_amt + sales_tax 4Print cust_name, purch_amt, sales_tax, total_amt END

Desk Checking Data Set 1Data Set 2 Purch_amt$ 10.00$ Tax_code02 1. Input Data

2. Expected Results Data Set 1Data Set 2 Sales_tax0$1.00 Total_amt$10.00$21.00

3. Desk Check Table Statement number Purch_amtTax_codeSales_taxTotal_amt First Pass 1$ print Second Pass 1$ $ $ print