CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

Similar documents
ALGORITHMS AND FLOWCHARTS

PSEUDOCODE AND FLOWCHARTS. Introduction to Programming

Computer System and programming in C

UNDERSTANDING PROBLEMS AND HOW TO SOLVE THEM BY USING COMPUTERS

Class 8 ALGORITHMS AND FLOWCHARTS. The City School

CS111: PROGRAMMING LANGUAGE1. Lecture 2: Algorithmic Problem Solving

CSC 121 Spring 2017 Howard Rosenthal

BIL101E: Introduction to Computers and Information systems Lecture 8

Algorithms: The recipe for computation

Division. Reverse Box Method

Fundamental of Programming (C)

Fundamentals of Programming

PROGRAM DESIGN TOOLS. Algorithms, Flow Charts, Pseudo codes and Decision Tables. Designed by Parul Khurana, LIECA.

FLOW CHART AND PSEUDO CODE

Introduction to Flowcharting

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

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

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

بسم اهلل الرمحن الرحيم

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

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

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

CS1100 Introduction to Programming

An Introduction to Programming with C++ Sixth Edition. Chapter 2 Beginning the Problem-Solving Process

IDENTIFY WAYS OF REPRESENTING ALGORITHMS.

AQA Decision 1 Algorithms. Section 1: Communicating an algorithm

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Chapter Two: Program Design Process and Logic

The Further Mathematics Support Programme

Method & Tools for Program Analysis & Design

IT 1033: Fundamentals of Programming Loops

INTENDED LEARNING OUTCOMES

Fifth-grade students performing at the Approaching Expectations level should show a basic understanding of the mathematical concepts and procedures.

Example 2: Simplify each of the following. Round your answer to the nearest hundredth. a

1. Answer: x or x. Explanation Set up the two equations, then solve each equation. x. Check

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Top Down Design. 2. Design Methodology

Chapter 1 INTRODUCTION TO COMPUTER AND PROGRAMMING

Structured Program Development in C

OA: Operations and Algebraic Thinking

Intro to Computational Programming in C Engineering For Kids!

Computer Science & Engineering 150A Problem Solving Using Computers

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Engineering program development. Edited by Péter Vass

SNS COLLEGE OF ENGINEERING,

INTRODUCTION TO ALGORITHMS

1. Introduction to Programming

Problem Solving and Algorithms

EECS1012. Net-centric Introduction to Computing. Lecture 7 Computational Thinking. Fall 2018, EECS York University. M.S. Brown and Amir H.

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

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

Tribhuvan University Institute of Science and Technology 2065

1. How many white tiles will be in Design 5 of the pattern? Explain your reasoning.

Decision Making and Branching

Computer Fundamentals: Pradeep K. Sinha & Priti Sinha

SNS COLLEGE OF ENGINEERING

Flowchart Structure 25th October 2018

Final Examination Semester 3 / Year 2010

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Beyond Competent (In addition to C)

Montana City School GRADE 5

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

Chapter 3 Structured Program Development

Structured Program Development

IS 0020 Program Design and Software Tools

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

PROGRAM DESIGN TOOLS. Algorithms, Flow Charts, Pseudo codes and Decision Tables. Designed by Parul Khurana, LIECA.

1/24/2012. Chapter 7 Outline. Chapter 7 Outline (cont d.) CS 440: Database Management Systems

Mathematics Scope & Sequence Grade 5 Revised: May 10, 2016

LECTURE 5 Control Structures Part 2

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Minnesota 4 th Grade 2007 Math Strands & Standards

G r a d e 7 M a t h e m a t i c s. Appendix: Models for Computing Decimal Numbers

8 th Grade Math Reference Sheet

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

Chapter 3 Problem Solving and the Computer

Assignment: 1. (Unit-1 Flowchart and Algorithm)

The transition: Each student passes half his store of candies to the right. students with an odd number of candies eat one.

Algebra 2 Semester 1 (#2221)

Logical Expressions and Control Statements

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Yimin Math Centre. Year 10 Term 2 Homework. 3.1 Graphs in the number plane The minimum and maximum value of a quadratic function...

Question 1. [5 points] Circle and briefly explain the error(s) in the following code:

Algorithms (continued)

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3. Algorithm Design & Problem Solving. Programming. Databases

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Introduction to Programming

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

Control Statements. If Statement if statement tests a particular condition

5th Grade Mathematics Mathematics CC

4. Use a loop to print the first 25 Fibonacci numbers. Do you need to store these values in a data structure such as an array or list?

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Repetition Algorithms

Sequence of Grade 5 Modules Aligned with the Standards

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Programming Logic Beginning

Programming Logic - Beginning

4 th Grade MCA3 Standards, Benchmarks, Test Specifications & Sampler Questions

Investigating Transformations With DESMOS

Transcription:

CS 199 Computer Programming Spring 2018 Lecture 2 Problem Solving

ALGORITHMS AND FLOWCHARTS A typical programming task can be divided into two phases: Problem solving phase produce an ordered sequence of steps that describe solution of problem this sequence of steps is called an algorithm Implementation phase implement the program in some programming language

Steps in Problem Solving First produce a general algorithm (one can use pseudocode) Refine the algorithm successively to get step by step detailed algorithm that is very close to a computer language. Pseudocode is an artificial and informal language that helps programmers develop algorithms. Pseudocode is very similar to everyday English.

Pseudocode & Algorithm Example 1: Write an algorithm to determine a student s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. Pseudocode: Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print FAIL else Print PASS Detailed Algorithm Step 1: Input M1,M2,M3,M4 Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print FAIL else Print PASS

Flowchart A flowchart is a schematic representation of an algorithm or a process. A flowchart gives a step-by-step procedure for solution of a problem.

Basic Flowchart Shapes and Definitions Terminal Project / Task Input / Output The start or end of a workflow. Process or action. Data: Inputs to, and outputs from, a process. Connector Decision Off Page Connector Used to connect one part of a flowchart to another. Decision point in a process or workflow. Connector used to connect one page of a flowchart to another. Module

Terminal An oval flow chart shape indicates the start or end of the process Usually containing the word Start or End. Terminal

Project/Task The Process Symbol represents any process, function, or action and is the most frequently used symbol in flowcharting Examples include Add 1 Turn the motor on Turn the light off Rotate the part Project / Task

Input/Output The Input/Output Symbol represents data that is available for input or resulting from processing (i.e. customer database records) Examples include Type in the weight Check the balance Time the operation Input / Output

Connector The Connector Symbol represents the exit to, or entry from, another part of the same flow chart. It is usually used to break a flow line that will be continued elsewhere. START A Connector A END The A connector indicates that the second flowchart segment begins where the first segment ends.

Decision The Decision Symbol is a junction where a decision must be made. A single entry may have any number of alternative solutions, but only one can be chosen Examples include Is this number larger than 10? Does the weight meet specifications? Has the count been reached? Decision Decision point in a process or workflow.

Off Page Connector Off-page Connector Symbols are used to indicate the flow chart continues on another page. Often the page number is placed in the shape for easy reference Off Page Connector Connector used to connect one page of a flowchart to another.

Module The position of the module symbol indicates the point the module is executed. A separate flowchart can be constructed for the module. START Read Input. Module Call calc_pay function. Display results. END 13

Three Flowchart Structures Sequence Decision Repetition 14

Sequence Structure A series of actions are performed in sequence 15

Decision Structure A sequence of actions executed based on condition. NO x < y? YES NO x < y? YES Calculate a as x plus y. Calculate a as x times 2. Calculate a as x times 2. 16

Repetition Structure A sequence of actions executed many times. x < y? YES Add 1 to x 1 is added to variable x until x > y 17

Example 1 Write an algorithm and draw a flowchart that will read the two sides of a rectangle and calculate its area. Pseudocode Input the width (W) and Length (L) of a rectangle Calculate the area (A) by multiplying L with W Print A

Example 1(cont.) Algorithm Step 1: Input W,L Step 2: A L x W Step 3: Print A START Input W, L A L x W Print A STOP

Example 2 Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation 2 ax bx c 0 2 Hint: d = sqrt ( b 4ac), and the roots are: x1 = ( b + d)/2a and x2 = ( b d)/2a

Example 2 (cont.) Algorithm: Step 1: Input a, b, c Step 2: d sqrt ( ) b b 4 a c Step 3: x1 ( b + d) / (2 x a) Step 4: x2 ( b d) / (2 x a) Step 5: Print x1, x2 START Input a, b, c d sqrt(b x b 4 x a x c) x 1 ( b + d) / (2 x a) X 2 ( b d) / (2 x a) Print x 1,x 2 STOP

Example 3 Write an algorithm to determine a student s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. 22

Example 3 (cont.) START Input M1,M2,M3,M4 Step 1: Input M1,M2,M3,M4 Step 2: GRADE (M1+M2+M3+M4)/4 GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then N IS GRADE<50 Y FAIL Print else PRINT PASS PRINT FAIL PASS Print STOP endif 23

Example 4 Count from 1 to 100 by odd numbers. Before attempting to draw the flowchart, determine what you want the output to be. What is the first block (always)?

Step 1 The output will be 1, 3, 5, 7, 9 99. The Start block is always first. START

Step 2 The program begins with the number 1. START INPUT 1

Step 3 The number 2 will be added to 1 so that the program will continue to count by odd numbers. START INPUT 1 ADD 2

Step 4 Add a decision block so that the program will continue counting until the value is greater than 100. START INPUT 1 ADD 2 Is number >100? NO

Step 5 Once the number is greater than 100, the program ends. START INPUT 1 ADD 2 Is number >100? NO YES END

Example 4 We want to create a flowchart that prints out the word Honour if the number input is 70, if the number is less than 40 print out the word Fail, otherwise print out the word Pass. 30

Example 4 (cont.) START Read A Yes A = 70 No No A < 40 Yes Honour Pass Fail END 31

Example 5 Express an algorithm to get two numbers from the user (dividend and divisor), testing to make sure that the divisor number is not zero, and displaying their quotient using a flowchart. 32

Example 5 (cont.) Step 1 Declare variables dividend, divisor, quotient Step 2 Prompt user to get dividend Step 3 Store values in dividend variable Step 4 Prompt user to get divisor Step 5 Store value in divisor variable Step 6 Display dividend and divisor Step 7 - Loop Selection: If divisor is equal to zero Display error message, divisor must be non-zero and go back to step 4 Step 8 - Calculate quotient as dividend/divisor Step 9 - Display quotient 33

34

Example 6 Write and algorithm and draw a flowchart to a) read an employee name (NAME), overtime hours worked (OVERTIME), hours absent (ABSENT) and b) determine the bonus payment (PAYMENT). Bonus Schedule OVERTIME (2/3)*ABSENT >40 hours >30 but 40 hours >20 but 30 hours >10 but 20 hours 10 hours Bonus Paid $50 $40 $30 $20 $10

Feedback request Please mail questions and constructive comments to marwa.elmenyawi@bhit.bu.edu.eg Your feedback will be most appreciated On style, contents, detail, examples, clarity, conceptual problems, exercises, missing information, depth, etc.

The next lecture Will talk about how to write your first program in Fortran.