CS1010 Programming Methodology

Slides:



Advertisements
Similar presentations
PSEUDOCODE & FLOW CHART
Advertisements

Chapter 6: Algorithmic Problem Solving 1 Chapter 6 Algorithmic Problem Solving.
Algorithms and Problem Solving
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Program Design and Development
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Adapted from slides by Marie desJardins
CS1101: Programming Methodology Aaron Tan.
February 24, 2015Applied Discrete Mathematics Week 4: Number Theory 1 Modular Arithmetic Let a be an integer and m be a positive integer. We denote by.
The University of Texas – Pan American
CIS Computer Programming Logic
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Introduction to Computer & C Computers Computers are programmable machines capable of performing calculations Examples of special-purpose computers are.
CS1101: Programming Methodology
By the end of this session you should be able to...
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
WEEK 4 Class Activities Lecturer’s slides.
UNIT 14 Functions with Pointer Parameters.
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.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Data Structures and Algorithms Introduction to Algorithms M. B. Fayek CUFE 2006.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
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)
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Chapter 8 Algorithms.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
Application: Algorithms Lecture 20 Section 3.8 Wed, Feb 21, 2007.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
The Hashemite University Computer Engineering Department
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
Invitation to Computer Science 5 th Edition Chapter 2 The Algorithmic Foundations of Computer Science.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
CS1010: Programming Methodology
Introduction to Computing Systems and Programming Programming.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
CS1010 Programming Methodology
Chapter 4 – C Program Control
Chapter 4 C Program Control Part I
Lecture 7: Repeating a Known Number of Times
GC211Data Structure Lecture2 Sara Alhajjam.
CS1010 Programming Methodology
Computer Science 101 While Statement.
Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13.
CS1100 Computational Engineering
Algorithm Discovery and Design
Introduction to Algorithms and Programming
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
Application: Algorithms
Application: Algorithms
Presentation transcript:

CS1010 Programming Methodology http://www.comp.nus.edu.sg/~cs1010/ UNIT 2 Algorithmic Problem Solving

Unit 2: Algorithmic Problem Solving CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Unit 2: Algorithmic Problem Solving Problem Solving Process Algorithm Control Structures Examples of Pseudocodes Euclid’s Algorithm

Problem Solving Process CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Problem Solving Process Analysis Design Implementation Testing Iterative process

Problem Solving Process © NUS CS1010 (AY2014/5 Semester 1) Problem Solving Process Analysis Design Implementation Testing Iterative process

Problem Solving Process CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Algorithmic Problem Solving Process Determine problem features Analysis Design Implementation Testing Iterative process Write algorithm Produce code What is an algorithm? Check for correctness and efficiency

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) Algorithm (3/3) Ways of representing an algorithm: Flowchart Pseudocode lynda.com

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Algorithm: Example #1

Algorithm: Example #2 (1/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) Algorithm: Pseudocode We will write algorithms in pseudocode instead of flowchart as the former is more succinct However, 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 (keep it succinct) and commonly understood notations (such as  for assignment in our previous example)

Control Structures (1/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) Control Structures (2/2) Default Sequence Also called branching Selection Also called loop Repetition True False ? True False ?

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Data Representation Internal representation: bits (binary digits) 0 and 1 1 byte = 8 bits In programming, we need variables to hold data. A variable has an associated data type and occupies memory space. In the following slides, variables are shown as boxes. Some data types in C (list is not exhaustive) Integers: int, short, long (int is most common) Real numbers: float, double Characters: char Self-reading: Lesson 1.4 in reference book

Control Structures: Sequence (1/2) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) 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; } Unit2_prog1.c

Control Structures: Selection (1/3) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) 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; Unit2_prog2.c

Control Structures: Repetition (1/3) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) 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 © NUS CS1010 (AY2014/5 Semester 1) 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; Unit2_prog3.c

Euclid’s Algorithm (1/3) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Euclid’s Algorithm (1/3) To compute the greatest common divisor (GCD) of two integers First documented algorithm by Greek mathematician Euclid in 300 B.C. Also known as Euclidean Algorithm 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.

Euclid’s Algorithm (2/3) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Euclid’s Algorithm (2/3) q is not important; r is the one that matters. r could be obtained by A modulo B (i.e. remainder of A / B) 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. Assumption on A > B unnecessary We will rewrite the algorithm

Euclid’s Algorithm (3/3) CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Euclid’s Algorithm (3/3) Euclid’s algorithm rewritten in modern form // Assume 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 } result is A Let’s trace GCD(12, 42) B (B > 0)? A r 12 42 true 12 42 12 true 6 12 6 true 6 false Result: 6

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) Summary In this unit, you have learned about The process of algorithmic problem solving The properties of an algorithm The three control structures How to write algorithms in pseudocode Tracing algorithms to verify their correctness

CS1010 Programming Methodology © NUS CS1010 (AY2014/5 Semester 1) End of File

CS1010 (AY2014/5 Semester 1)