Lecture 4 Problem solving with Algorithm, Flowcharts and Data Structure Structured Programming Instructor: Prof. K. T. Tsang.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Chapter 1 - An Introduction to Computers and Problem Solving
Chapter 2 - Problem Solving
Chapter 2 - Problem Solving
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Basics of Computer Programming Web Design Section 8-1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Program Design and Development
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 1 Program Design
Problem Solving Chapter 2. What is an algorithm? n A solution to a problem that is: –Precise –Effective –Terminating.
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
PRE-PROGRAMMING PHASE
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Lecture # 8 ALGORITHMS AND FLOWCHARTS. Algorithms The central concept underlying all computation is that of the algorithm ◦ An algorithm is a step-by-step.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Simple Program Design Third Edition A Step-by-Step Approach
Muhammad Adnan Talib Lec#4 27 th Sep,2012 Computer Science Department COMSATS Institute of Information Technology Sahiwal Introduction to Computer Programming.
Designing and Debugging Batch and Interactive COBOL Programs Chapter 5.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
By the end of this session you should be able to...
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
Practice and Evaluation. Practice Develop a java class called: SumCalculator.java which computes a sum of all integer from 1 to 100 and displays the result.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
I Power Higher Computing Software Development The Software Development Process.
The Programming Process Define the problem* Make or buy software? Design the program * Code (write) the program Test (debug) the program Document the.
Algorithms & Flowchart
Lecture 13: 10/10/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
The basics of the programming process The development of programming languages to improve software development Programming languages that the average user.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
Data Structure Introduction Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '081 Chapter 2 - Problem Solving 2.1 Program Development Cycle 2.2 Programming Tools.
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
Alexandria University Faculty of Science Computer Science Department Introduction to Programming C++
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
How Computers Solve Problems Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
Algorithms and Flowcharts
ICS 3UI - Introduction to Computer Science
ALGORITHMS AND FLOWCHARTS
Software Development.
GC101 Introduction to computers and programs
Basics of Computer Programming
Chapter 2- Visual Basic Schneider
Programming Problem steps must be able to be fully & unambiguously described Problem types; Can be clearly described Cannot be clearly described (e.g.
Introduction To Flowcharting
Basics of Computer Programming
Basics of Computer Programming
Introduction to Computer Programming
Basics of Computer Programming
Designing and Debugging Batch and Interactive COBOL Programs
Unit# 9: Computer Program Development
Problem Solving Techniques
Chapter 2- Visual Basic Schneider
Problem Solving and Algorithm Design
Chapter 2- Visual Basic Schneider
Introduction to Programming
Basic Concepts of Algorithm
Presentation transcript:

Lecture 4 Problem solving with Algorithm, Flowcharts and Data Structure Structured Programming Instructor: Prof. K. T. Tsang

How to solve a problem Understand the problem Find the connection between the given data and the final solution Write down a plan to obtain the solution Carry out your plan Examine the solution obtained

Understand the problem Ask questions What do I know about the problem? What data/information I need? What does the solution looks like? What are the special cases? Have I solved similar problem before?

Strategies to find the solution Look for similarity If I have solved similar problem before, use similar solution. Divide and conquer Break up a large problem into smaller ones or one you already know how to solve. It is always easier to solve smaller or known problems.

Plan for the solution Carefully write down all the steps you need to do in order to solve the problem. Make sure your plan is complete and consistent. Consider all the possible outcomes from the intermediate steps and handle them correctly. In computer problem solving, this plan is called an Algorithm.

Algorithm 算法 A computing procedure (a finite set of well-defined instructions/steps) devised to accomplish some task or to solve some problem (i. e. given an initial state, following the procedure will lead to a desired end-state). This procedure/method is often independent of the computer system or programming language we use to solve the problem. To find simple and efficient algorithms that are easy to implement (i.e. to turn it into computer codes) is an important part of computer science. Often, the computational complexity of the algorithm depends on the use of suitable data structures (an organization of data involved in the problem).

Algorithm -2 Informally, the concept of an algorithm is often illustrated by the example of a recipe, although many algorithms are much more complex. Algorithms often have steps that repeat (iterate) or require decisions (such as logic or comparison). In most higher level programs, algorithms can have complex patterns, each using smaller and smaller sub-methods. In many programming languages, algorithms are implemented as functions or procedures.

Data structure A data structure is a way of storing data in a computer so that it can be used efficiently. Often a carefully chosen data structure will allow a more efficient algorithm to be used. An array-- a sequence of data structure. A binary tree --a simple type of branching linked data structure.

Flowcharts -- a schematic (graphical) representation of a process/procedure. A procedure is breakdown to its sequential steps. Each steps is represented symbolically and their relationship to other steps is shown schematically in a graph. They are commonly used in business/economic presentations to help the audience visualize the content better, or to find flaws in the process. In computer science, flowcharts are used to illustrate the steps in an algorithm.

Flowcharts -- an example A simple flowchart for what to do if a lamp doesn't work Lamp doesn’t work Lamp plugged in? Plug in lamp Replace bulb Bulb burned out? Buy new lamp no yes no

Flowcharts an example to test if N is a prime number Choose 0<i<N Find remainder r=N%i r=0? N is not a prime Choose another i Is there other i<N not tested? N is a prime no yes no

Pseudo-code A description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines or language-specific syntax.

Example of a pseudo-code: a recipe to make a cake Get all ingredients: butter, milk, flour, water, sugar, salt Heat butter to melt Mix melted butter, flour, milk, water, sugar and salt Mix all ingredients until it become a smooth paste Pour paste to a pre-greased container Preheat oven Put container to oven Bake for 30 minutes or until cake is brown outside Take container out and use a tooth-pick to check whether cake is done inside If cake not yet done, re-bake it for 5 minutes and check again until it is fully done

Example of a pseudo-code: to test if n is a prime number For all integer 2=<i<n Begin loop –Find r=n%i –If (r=0) n is not a prime; return or exit; –else continue End loop n is a prime; return or exit;

Example of a pseudo-code: to test if n is a prime number For all integer 2=<i<n Begin loop –Find r=n%i –If (r=0) n is not a prime; return 0; –else continue End loop n is a prime; return 1;

Example of a pseudo-code: find all prime numbers < N For all integers 2<n<N Begin loop –Test if n is a prime number End loop

Top-down design humans can only handle a relatively low level of complexity a program longer than 30/50 lines is already probably too complex top-down design is a method to decrease complexity of programming task by breaking it down into a few simpler relatively independent subtasks (other method names - stepwise refinement and divide and conquer) if a subtask is still complex, further subdivide to produce a hierarchy of tasks each simple subtask should be coded by a separate function or other separated piece of code added benefit - different people (or teams) can work on different subtasks

Example of top-down design Plan a party Invite the peoplePrepare food Make a list of guestCall the people Plan the menuShop for foodCook the food Main module (level 0) Level 1 Go to market Get in a car Select food DrivePark the car Level 2 Level 3 Level 4 Select the date

Example of top-down design Solve ax^2 + bx + c = 0 x1 = (-b+rd)/(2*a); x2 = (-b-rd)/(2*a) rd = sqrt(D) D= b*b -4*a*c

Example of top-down design Find Pythagoras triplet Pick a pair (i,j) such that 0<i<N, 0<j<N Find s2=i*i + j*j Check if s2=k*k, where k>0 is an integer yes no

Procedural Approach for computer problem solving Sketch the overall computation at a high level in human languages, producing a pseudo-code description of the computation (with flowchart) Identify central data structures: what data will be needed throughout the entire computation Identify procedures: sub-problems in the overall computation Determine inputs and output of each procedure Translate pseudo-code into code: sub-problems become calls to methods which solve individual sub-problems Add methods for sub-problems Repeat previous steps for each stub method

Example Here is an example problem that we can use in the discussion of top-down design. read in a sequence of temperatures compute the average temperature determine if the temperatures are strictly increasing determine the largest daily increase in temperature

Solving the example problem by top-down design Sketch pseudo-code. In this case, the problem description can serve as the pseudo-code. prompt user for number of days read temperature for each day find the average temperature and print it determine if temperatures are strictly increasing, if so print message determine largest daily increase and print it

Identify main data structures In this case, the main data used by the entire computation is the sequence of temperatures for each day. We can represent an individual temperature using a floating point number, and the overall sequence can be an array of number. So, this is our central data structure: an array of floating point values.

Identify procedures The name of each method should directly reflect its purpose in the overall computation: for example: promptUserForNumDays readTemperatures findAverageTemperature determineIfStrictlyIncreasing determineLargestDailyIncrease

Programming as problem solving Define the problem - what is the input and output? - any constraints to be satisfied? - what information is essential? Develop a solution - construct the algorithm (steps lead to the answer) - implement the algorithm in a program (computer code) Compile, test and debug the program Document - give description of solution and explain it Maintain the program - test and revise the code as needed - improve the algorithm to make it more efficient/faster

What makes a good program? Correctness –Meets the problem requirements –Produces correct results Easy to read and understand Easy to modify Easy to debug Efficient –Fast –Requires less memory