Foundation of programming Week 3. Last week ‘How to think like a programmer’ The HTTLAP 6 step approach: Understand the problem – Devise a plan to solve.

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

Working With Algorithm and Flowcharts
Introduction to Programming
PROBLEM SOLVING TECHNIQUES
Flowcharts The following sample flowcharts are easy to customize. Simply click on a word, shape or rule to edit, change color or adjust an element. Contents.
PSEUDOCODE & FLOW CHART
Chapter 1 - An Introduction to Computers and Problem Solving
Subject: Information Technology Grade: 10
Fundamentals of Algorithms MCS - 2 Lecture # 4
Basics of Computer Programming Web Design Section 8-1.
Computer Science 101 Overview of Algorithms. Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2.
Problem Solving for Programming Session 6 Data and Control Abstractions.
Chapter 2- Visual Basic Schneider
Programming Concepts (Part A) Ping Hsu. What is a program? WHITE CAKE RECIPE 1.Preheat oven to 350 degrees F (175 degrees C). 2.Grease and flour a 9x9.
PROBLEM SOLVING What should I wear today? Which sport should I do? Which cell phone is best for me? Which team is probably the winner? Will Euro be raised.
Programming Fundamentals (750113) Ch1. Problem Solving
Problem Solving Chapter 2. What is an algorithm? n A solution to a problem that is: –Precise –Effective –Terminating.
The Program Design Phases
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Introduction to Algorithm – part one Jennifer Elmer Form 3 Computing.
Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.
ALGORITHMS AND FLOWCHARTS
Problem Solving for Programming Session 4 Calculating and Keeping Track of Values.
CSCI-100 Introduction to Computing Algorithms Part I.
Mixtures And Solutions
Chapter 2: Problem Solving
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
DCT 1123 Problem Solving & Algorithms
CPSC 171 Introduction to Computer Science 3 Levels of Understanding Algorithms More Algorithm Discovery and Design.
End Show Writing a computer program involves performing the following tasks. 1. Understanding the problem 2. Developing an Algorithm for the problem 3.
By the end of this session you should be able to...
Chapter 2 Problem Solving On A Computer 2.1 Problem Solving Steps Solving a problem on a computer requires steps similar to those followed when solving.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Chapter 2: General Problem Solving Concepts
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
Introduction to Computing Dr. Nadeem A Khan. Lecture 2.
 In this chapter you will learn about:  Introduction to Problem Solving  Software development method (SDM)  Specification of needs  Problem analysis.
Chapter 7 Problem Solving with Loops
QUALITY MANAGEMENT PRINCIPLES. Objectives 4 Understand usefulness of CASE tools 4 Types of CASE tools 4 Data flow diagrams (DFD)
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Algorithms and Pseudocode
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
Lecture Notes 1/20/05 Pseudocode.  Pseudocode standard which we will follow in this class: - Statements are written in simple English; - Each instruction.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Progression in KS3/4 Algorithms MONDAY 30 TH NOVEMBER SUE SENTANCE.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Low-Level Programming Languages, Pseudocode and Testing Chapter 6.
Algorithms and Flowcharts
Flowcharts The following sample flowcharts are easy to customize. Simply click on a word, shape or rule to edit, change color or adjust an element. Contents.
Learning outcomes 5 Developing Code – Using Flowcharts
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
PROGRAM CONTROL STRUCTURE
Algorithms and Flowcharts
Introduction To Flowcharting
Introduction to Algorithm – part 1
Programming Logic n Techniques
ALGORITHMS AND FLOWCHARTS
Flowcharts The following sample flowcharts are easy to customize. Simply click on a word, shape or rule to edit, change color or adjust an element. Contents.
Control Structure Senior Lecturer
Algorithms & Pseudocode
Programming Fundamentals (750113) Ch1. Problem Solving
ALGORITHMS AND FLOWCHARTS
Speaking Presentation
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Foundation of programming Week 3

Last week ‘How to think like a programmer’ The HTTLAP 6 step approach: Understand the problem – Devise a plan to solve it – Carry out the plan – Assess the result – Reflect on what you have learned – Document the solution Descriptive languages Pseudo Code

Lecture Outlines Program design methods – Top down – Bottom up – Data-structure approaches – Data-flow approaches Logic structures – Sequential – Conditional IF Switch Loops (iterations) More on pseudo-code

Reading Chapter 5 and 6 (HTTP) EXERCISES Chapter 5 exercises 1 TO 10, pages 127 to 129 Chapter 6 exercises 1 to 14, pages 162 to 166

Top down design

Bottom up design

Data flow approaches Data movement and transformation Top down flow of data Activity, in pairs: – Think about the oyster card – What is the data? – Where does it flow from+to? – How is it transformed? – Try to draw a diagram of this

The four logic structures Sequential – flows straight down Decision – the flow splits into two Loop – the flow repeats a section Case – the flow splits into many streams

Example data frlow x = 10; y =4; sum = x+y; subtract = x-y; product = x*y; X=10 y=4 sum=x+y subtract=x-y product=x*y

The sequential logic structure 1 2

Decision logic structure Yes No 12 IF(cond=TRUE) THEN instruction1 ELSE instruction2 ENDIF

Loop logic structure cond body program yes No 1 2 WHILE(cond=TRUE) DO {instruction1 ; instruction2} ENDWHILE

The case logic structure

switch x { x== condition1 instruction1 x== condition 2 instruction 2 x== condition 3 instruction 3 x== condition 4 instruction 4 }

Discussion which logic will you use? Which logic did you use in your solutions to the following problems? 1.Use a filter coffee machine to make a cup of coffee 2.Hang a picture on the wall 3.Drive a train

Making a cup of coffee 1.Put water in coffee machine; 2.Open coffee holder; 3.Put filter paper in machine; 4.Measure coffee for one cup; 5.Put coffee into filter paper; 6.Shut the coffee holder; 7.Turn machine on; 8.Wait for coffee to filter through; 9.Pour coffee into mug; 10.Turn off the machine;

Analyse our solution Is sugar needed? If (yes) how much? White coffee? If yes add milk?

Make a cup of coffee modified 1.Put water in coffee machine; 2.Open coffee holder; 3.Put filter paper in machine; 4.Measure coffee for one cup; 5.Put coffee into filter paper; 6.Shut the coffee holder; 7.Turn the machine on; 8.Wait for coffee to filter through; 9.Find out how many sugars required; 10.WHILE(sugar added not equal to sugar required) 11. DO 11.1 add one spoon of sugar 11.2 add 1 spoon 12. ENDWHILE 13.IF (white coffee required) 13.1 add milk/cream 14. ENDIF 15.Pour coffee into mug 16.Stir coffee 17.Turn machine off

Making 6 cups of coffee 1.Put water in coffee machine; change to water for 6 cups 2.Open coffee holder; 3.Put filter paper in machine; 4.Measure coffee for one cup; coffee for 6 cups 5.Put coffee into filter paper; 6.Shut the coffee holder; 7.Turn the machine on; 8.Wait for coffee to filter through; 9.Find out how many sugars required; 10.Find out weather milk required 11.WHILE(sugar added not equal to sugar required) 12. DO 12.1 add one spoon of sugar 12.2 add 1 spoon 13. ENDWHILE 14.IF (white coffee required) 14.1 add milk/cream 15. ENDIF 16.Pour coffee into mug 17.Stir coffee 18.Turn machine off Repeated actions

Making a 6 cups of coffee 1.Put waterF for 6 cups in coffee machine 2.Open coffee holder; 3.Put filter paper in machine; 4.Measure coffee for 6 cup; 5.Put coffee into filter paper; 6.Shut the coffee holder; 7.Turn the machine on; 8.Wait for coffee to filter through; 10.While(cups poured not equal to 6) 11.Do 1.Find out how many sugars required; 2.Find out weather milk required 3.WHILE(sugar added not equal to sugar required) 4. DO 12.1 add one spoon of sugar 12.2 add 1 spoon 5. ENDWHILE 6.IF (white coffee required) 14.1 add milk/cream 7. ENDIF 8.Pour coffee into mug 9.Stir coffee 10.Add 1 to the number of cups poured 12.ENDWHILE 13.Turn machine off

Making a pot of coffee 1.Find out how many cups required 2.Put waterF for number cups required n coffee machine 3.Open coffee holder; 4.Put filter paper in machine; 5.Measure coffee for cups required 6.Put coffee into filter paper; 7.Shut the coffee holder; 8.Turn the machine on; 9.Wait for coffee to filter through; 10.While( cups poured not equal to cups required ) 11.Do 1.Find out how many sugars required; 2.Find out weather milk required 3.WHILE(sugar added not equal to sugar required) 4. DO 12.1 add one spoon of sugar 12.2 add 1 spoon 5. ENDWHILE 6.IF (white coffee required) 14.1 add milk/cream 7. ENDIF 8.Pour coffee into mug 9.Stir coffee 10.Add 1 to the number of cups 12.ENDWHILE 13.Turn machine off

Limit the cups required to six 1.Find out how many cups required 2.IF(more than zero cups required) 1.IF (more than six cup wanted) 1.Limit cupsrequired to six; 2.ENDIF 3.Put waterF for number cups required n coffee machine 4.Open coffee holder; 5.Put filter paper in machine; 6.Measure coffee for cups required 7.Put coffee into filter paper; 8.Shut the coffee holder; 9.Turn the machine on; 10.Wait for coffee to filter through; 11.While( cups poured not equal to cups required ) 12.Do 1.Find out how many sugars required; 2.Find out weather milk required 3.WHILE(sugar added not equal to sugar required) 4. DO 12.1 add one spoon of sugar 12.2 add 1 spoon 5. ENDWHILE 6.IF (white coffee required) 14.1 add milk/cream 7. ENDIF 8.Pour coffee into mug 9.Stir coffee 10.Add 1 to the number of cups 13.ENDWHILE 14.Turn machine off 15.ENDIF

EXERCISE Write a pseudo code for a program to work out the final grade. (Mark >= 80) grade = ‘A’ (70<=Mark<80) grade = ‘B’ (60<=Mark <70) grade= ‘C’ (50<=Mark<60) grade ‘D’ (40<=Mark<50) grade ‘E’ (Mark<40) grade =‘F’

Solution Nested IFs IF (Mark>=80) – grade < -- ‘A’; ELSE IF (Mark>=70) – grade < -- ‘B’; ELSE IF (Mark>=60) – grade < -- ‘C’; ELSE IF (Mark>=50) – grade < -- ‘D’; ELSE IF (Mark>=40) – grade < -- ‘E’; ELSE – grade < -- ‘F’; ENDIF

Exercise2 1)Write a pseudo code program which allow the user to enter the monthly rain full and add it the total rainfall for the year. 2)Change your program to work out the average rainfall for the previous year 3)Add the average rainfall sofar (the year has not finished yet)

Solution: totalRainfall < --- 0; onth < --- 1; WHILE(month <= 12) – Display ‘please enter the month’s rain fall’; – Get monthRainfall; – totalRainfall < --- totalRainfall + monthRainfall; – month < -- month +1; ENDWHILE;

Solution: totalRainfall < --- 0; month < --- 1; WHILE(month <= 12) – Display ‘please enter the month’s rain fall’; – Get monthRainfall – totalRainfall < --- totalRainfall + monthRainfall; – month < -- month +1; ENDWHILE AverageRainfall = totalRainfall /12

Solution (3) totalRainfall < --- 0; Get currentMonth month < --- 1; WHILE(month <= currentmonth) – Display ‘please enter the month’s rain fall’; – Get monthRainfall – totalRainfall < --- totalRainfall + monthRainfall; – month < -- month +1; ENDWHILE AverageRainfallsofar = totalRainfallsofar /currenthmonth; (assuming last minute of the month)

Exercise Write a pseudo-code program that allows the user the to enter the ages all students on the class and work out the average age. The user enters 0 when all ages are entered.

Solution is this correct? 1.Display ‘please enter an age ( 0 to finish); 2.Get value of age; 3.WHILE(age notequal 0) 1.totalAge < -- totalAge + age; 2.numberOfAges < -- numberOfAges + 1; 3.Get value of age; 4.ENDWHILE 5.If(numberOfAges >0) 1.averageAge < -- totalAge / numberOfAges; 2.Display averageAge; 6.ENDIF

Solution add initialisation 1.totalAge < -- 0; // intialise totalAge to zero 2.numberOfAges < -- 0; // initialise numberOfAges to zero 3.Display ‘please enter an age ( 0 to finish); 4.Get value of age; 5.WHILE(age notequal 0) 1.totalAge < -- totalAge + age; 2.numberOfAges < -- numberOfAges + 1; 3.Get value of age; 6.ENDWHILE 7.If(numberOfAges >0) 1.averageAge < -- totalAge / numberOfAges; 2.Display averageAge; 8.ENDIF