CS1010 Programming Methodology

Slides:



Advertisements
Similar presentations
PSEUDOCODE & FLOW CHART
Advertisements

Chapter 6: Algorithmic Problem Solving 1 Chapter 6 Algorithmic Problem Solving.
CS1010 Programming Methodology
CS1101: Programming Methodology Aaron Tan.
Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
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.
Data Structures and Algorithms Introduction to Algorithms M. B. Fayek CUFE 2006.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
Algorithms and Pseudocode
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Pseudocode (pronounced SOO-doh-kohd)  is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled.
ALGORITHMS AND FLOWCHARTS
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 C Program Control Part I
Lecture 7: Repeating a Known Number of Times
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 5: Control Structures II
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Lecture 2 Introduction to Programming
Introduction To Flowcharting
CS1010 Programming Methodology
ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm
Repetition-Counter control Loop
Introduction to Computer Programming
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithm and Ambiguity
Computer Science 101 While Statement.
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13.
Control Structures Lecture 7.
Control Structure Senior Lecturer
Unit# 9: Computer Program Development
CS1100 Computational Engineering
Structured Program
Chapter 4 - Program Control
1) C program development 2) Selection structure
Algorithm Discovery and Design
3 Control Statements:.
The while Looping Structure
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Introduction to Algorithms and Programming
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Chapter 2- Visual Basic Schneider
Algorithm and Ambiguity
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.
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
EPSII 59:006 Spring 2004.
Application: Algorithms
Application: Algorithms
Basic Concepts of Algorithm
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
The while Looping Structure
WJEC GCSE Computer Science
The while Looping Structure
CHAPTER 6 Testing and Debugging.
Presentation transcript:

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving http://www.comp.nus.edu.sg/~cs1010/ UNIT 3 Algorithmic Problem Solving

Unit 3: Algorithmic Problem Solving CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Unit 3: Algorithmic Problem Solving Computational Thinking: Coin Change Euclid’s Algorithm Algorithmic Problem Solving Process Algorithm Control Structures Examples of Pseudocodes

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Coin Change Problem statement Given the above coin denominations: 1¢, 5¢, 10¢, 20¢, 50¢, and $1, assuming that you have unlimited supply of them, find the minimum number of coins needed for a given amount. This is called optimisation problem in CS – finding the best among all possible solutions. Examples: 375¢  ? coins 543¢  ? coins Question you may ask: Do I need to report how many coins of each denomination in my solution? No, you don’t have to. (But in the process of computing the solution you will somehow get to know how many coins of each denomination you need.) 

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Contract of a Task The Task Giver The Task Solver The amount (in ¢) The min. no. of coins The Task Giver: Provides the necessary inputs (arguments) to the Task Solver Does not provides unnecessary data to the Task Solver Does not care/need to know how the Task Solver solves the task The Task Solver: Accepts the necessary inputs (arguments) from the Task Giver Returns the result to the Task Giver Does not provides extraneous data to the Task Giver or perform extraneous work

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Euclid’s Algorithm (1/3) First historically documented algorithm by Greek mathematician Euclid in 300 B.C. Also known as Euclidean Algorithm q is not important; r is the one that matters. 1. Let A and B be integers with A > B ≥ 0. 2. If B = 0, then the GCD is A and algorithm ends. 3. Otherwise, find q and r such that A = q.B + r where 0 ≤ r < B 4. Replace A by B, and B by r. Go to step 2. r could be obtained by A modulo B (i.e. remainder of A / B) Assumption on A > B unnecessary We will rewrite the algorithm

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Euclid’s Algorithm (2/3) 1. Let A and B be integers with A > B ≥ 0. 2. If B = 0, then the GCD is A and algorithm ends. 3. Otherwise, find q and r such that A = q.B + r where 0 ≤ r < B 4. Replace A by B, and B by r. Go to step 2. // Pre-cond: A and B are non-negative // integers, but not both zeroes. Algorithm GCD( A, B ) { while (B > 0) { r  A modulo B A  B B  r } return A Rewritten in modern form Pre-condition: What must be true for this Task Solver to work. What the Task Giver must provide to this Task Solver Details of Task Solver unknown to Task Giver What this Task Solver returns to the Task Giver.

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Euclid’s Algorithm (3/3) You might have guess what this algorithm does from its name but let’s trace it with some examples. Very important skill! Let’s trace GCD(12, 42) // Pre-cond: A and B are non-negative // integers, but not both zeroes. Algorithm GCD(A, B) { while (B > 0) { r  A modulo B A  B B  r } return A B (B > 0)? A r 12 42 true 12 42 12 true 6 12 6 So do you know what does the Euclid’s Algorithm do now? true 6 false Result returned: ? 

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Polya’s Problem Solving Process We (roughly)map Polya’s steps to algorithmic problem solving. 1. Understand the Problem Analysis Design Implementation Testing Iterative process 2. Make a Plan 3. Do the Plan 4. Look back

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Algorithmic Problem Solving Determine problem features Analysis Design Implementation Testing Iterative process Write algorithm What is an algorithm? Write code Check for correctness and efficiency

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Algorithm (1/3) An algorithm is a well-defined computational procedure consisting of a set of instructions, that takes some value or set of values as input, and produces some value or set of values as output. Input Algorithm Output ‘Algorithm’ stems from ‘Algoritmi’, the Latin form of al-Khwārizmī, a Persian mathematician, astronomer and geographer. Source: http://en.wikipedia.org/wiki/Algorithm

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Algorithm (2/3) An algorithm has these properties: Exact Terminate Effective General The algorithm must terminate. (Or no solution will be obtained.) Each step must be exact. (Or it will not be precise.) The algorithm must be effective. (i.e. it must solve the problem.) The algorithm must be general. (Within the constraints of the system/language.)

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Algorithm (3/3) Ways of representing an algorithm: Flowchart Pseudocode lynda.com

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Algorithm: Example #1

Algorithm: Example #2 (1/2) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Algorithm: Example #2 (1/2) Find maximum and average of a list of numbers: start sum  count  0 max  0 end of input? Enter num increment count sum  sum + num Yes No num > max? max  num ave  sum/count end Terminator box Process box Decision box print max, ave Flowchart

Algorithm: Example #2 (2/2) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Algorithm: Example #2 (2/2) Find maximum and average of a list of numbers: Pseudocode The need to initialise variables. sum  count  0 // sum = sum of numbers // count = how many numbers are entered? max  0 // max to hold the largest value eventually for each num entered, count  count + 1 sum  sum + num if num > max then max  num ave  sum / count print max, ave The need to indent. Are there any errors in this algorithm?

Algorithm: Pseudocode CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Algorithm: Pseudocode We will write algorithms in pseudocode instead of flowchart as the former is more succinct However, unlike programming languages, there are no standard rules on how pseudocodes should look like General guidelines: Every step must be unambiguous, so that anybody is able to hand trace the pseudocode and follow the logic flow Use a combination of English (but keep it succinct) and commonly understood notations (such as  for assignment in our previous example) Use indentation to show the control structures

Control Structures (1/2) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Control Structures (1/2) An algorithm is a set of instructions, which are followed sequentially by default. However, sometimes we need to change the default sequential flow. We study 3 control structures.

Control Structures (2/2) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Control Structures (2/2) Default Sequence Also called branching Selection Also called loop Repetition True False ? True False ?

Control Structures: Sequence (1/2) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Control Structures: Sequence (1/2) Task: Compute the average of three integers A possible algorithm: enter values for num1, num2, num3 ave  ( num1 + num2 + num3 ) / 3 print ave Variables used: num1 num2 num3 ave Another possible algorithm: enter values for num1, num2, num3 total  ( num1 + num2 + num3 ) ave  total / 3 print ave Variables used: num1 num2 num3 ave total Each box represents a variable. Important concepts: Each variable has a unique name and contains a value.

Control Structures: Sequence (2/2) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Control Structures: Sequence (2/2) Task: Compute the average of three integers How the program might look like // This program computes the average of 3 integers #include <stdio.h> int main(void) { int num1, num2, num3; float ave; printf("Enter 3 integers: "); scanf("%d %d %d", &num1, &num2, &num3); ave = (num1 + num2 + num3) / 3.0; printf("Average = %.2f\n", ave); return 0; } Unit3_prog1.c Don’t worry about the C syntax; we will discuss it next week. For now, just to show you how the algorithm is translated into the code. The logic remains the same, but you need to write the code according to the rules of the programming language.

Control Structures: Selection (1/3) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Control Structures: Selection (1/3) True False ? Task: Arrange two integers in ascending order (sort) Algorithm A: enter values for num1, num2 // Assign smaller number into final1, // and larger number into final2 if (num1 < num2) then final1  num1 final2  num2 else final1  num2 final2  num1 // Transfer values in final1, final2 back to num1, num2 num1  final1 num2  final2 // Display sorted integers print num1, num2 Variables used: num1 num2 final1 final2

Control Structures: Selection (2/3) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Control Structures: Selection (2/3) True False ? Task: Arrange two integers in ascending order (sort) Algorithm B: enter values for num1, num2 // Swap the values in the variables if necessary if (num2 < num1) then temp  num1 num1  num2 num2  temp // Display sorted integers print num1, num2 Variables used: num1 num2 temp Compare Algorithm A with Algorithm B.

Control Structures: Selection (3/3) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Control Structures: Selection (3/3) True False ? How the program might look like for Algorithm B // This program arranges 2 integers in ascending order #include <stdio.h> int main(void) { int num1, num2, temp; printf("Enter 2 integers: "); scanf("%d %d", &num1, &num2); if (num2 < num1) { temp = num1; num1 = num2; num2 = temp; } printf("Sorted: num1 = %d, num2 = %d\n", num1, num2); return 0; Unit3_prog2.c

Control Structures: Repetition (1/3) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving True False ? Control Structures: Repetition (1/3) Task: Find sum of positive integers up to n (assume n>0) Algorithm: enter value for n // Initialise a counter count to 1, and ans to 0 count  1 ans  0 while (count ≤ n) do ans  ans + count // add count to ans count  count + 1 // increase count by 1 // Display answer print ans Variables used: n count ans Initialisation is very important!

Control Structures: Repetition (2/3) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving True False ? Control Structures: Repetition (2/3) Important to trace pseudocode to check its correctness Algorithm: enter value for n count  1 ans  0 while (count ≤ n) do ans  ans + count count  count + 1 // Display answer print ans Assume user enters 3 for n. count ans (count <= n)? 1 true 2 1 true 3 3 true 4 6 false Output: 6

Control Structures: Repetition (3/3) CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving True False ? Control Structures: Repetition (3/3) How the program might look like // Computes sum of positive integers up to n #include <stdio.h> int main(void) { int n; // upper limit int count = 1, ans = 0; // initialisation printf("Enter n: "); scanf("%d", &n); while (count <= n) { ans += count; count++; } printf("Sum = %d\n", ans); return 0; Unit3_prog3.c

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Coin Change Problem statement Given the above coin denominations: 1¢, 5¢, 10¢, 20¢, 50¢, and $1, assuming that you have unlimited supply of them, find the minimum number of coins needed for a given amount. Can you write out the pseudocode of your algorithm? What must the Task Giver send to the Task Solver? What must the Task Solver return to the Task Giver? Algorithm CoinChange(amt) { . . . return coins }

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving Knowing the algorithmic problem solving process Knowing the properties of an algorithm Knowing the three control structures Knowing how to write algorithms in pseudocode Knowing how to trace algorithms to verify their correctness

CS1010 Programming Methodology Aaron Tan, NUS Algorithmic Problem Solving End of File