Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
Chapter 2: Problem Solving
Design & Analysis of Algoritms
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
ISBN Chapter 3 Describing Syntax and Semantics.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
CPSC 411, Fall 2008: Set 12 1 CPSC 411 Design and Analysis of Algorithms Set 12: Undecidability Prof. Jennifer Welch Fall 2008.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 2: Algorithm Discovery and Design
CSE 830: Design and Theory of Algorithms
Program Design and Development
Describing Syntax and Semantics
Chapter 2: Algorithm Discovery and Design
Design and Analysis of Algorithms
Group practice in problem design and problem solving
Fundamentals of Python: From First Programs Through Data Structures
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Fundamentals of Python: First Programs
Objectives Understand the basic concepts and definitions relating to testing, like error, fault, failure, test case, test suite, test harness. Explore.
Simple Program Design Third Edition A Step-by-Step Approach
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
หลักการโปรแกรม เพื่อแก้ปัญหาโดยใช้คอมพิวเตอร์
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Introduction to Algorithms By Mr. Venkatadri. M. Two Phases of Programming A typical programming task can be divided into two phases: Problem solving.
Introduction to Software Testing. Types of Software Testing Unit Testing Strategies – Equivalence Class Testing – Boundary Value Testing – Output Testing.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
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.
Data Structures and Algorithms Introduction to Algorithms M. B. Fayek CUFE 2006.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Visual Basic Programming
Chapter 2: General Problem Solving Concepts
CS Data Structures I Chapter 2 Principles of Programming & Software Engineering.
Algorithm Design.
1 Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
Algorithms & FlowchartsLecture 10. Algorithm’s CONCEPT.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
More on Correctness. Prime Factorization Problem: Write a program that computes all the prime factors of a given number Solution (Idea): Factors are less.
Software Development Problem Analysis and Specification Design Implementation (Coding) Testing, Execution and Debugging Maintenance.
An Axiomatic Basis for Computer Programming Robert Stewart.
CS404 Design and Analysis of Algorithms BBy DDr. M V S Peri Sastry BB.E, PhD(BITS-Pilani)
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
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.
INTRODUCTION TO COMPUTER PROGRAMMING(IT-303) Basics.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Introduction to Problem Solving Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
 Problem Analysis  Coding  Debugging  Testing.
Complexity Analysis (Part I)
Algorithms and Problem Solving
GC211Data Structure Lecture2 Sara Alhajjam.
ALGORITHMS AND FLOWCHARTS
Programming Problem steps must be able to be fully & unambiguously described Problem types; Can be clearly described Cannot be clearly described (e.g.
Algorithm Analysis CSE 2011 Winter September 2018.
Unit# 9: Computer Program Development
Objective of This Course
Algorithms and Problem Solving
Discrete Mathematics CS 2610
Complexity Analysis (Part I)
CHAPTER 6 Testing and Debugging.
Presentation transcript:

Introduction to Problem Solving

Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving at an algorithm –Verification and analysis of the algorithm –Encoding the algorithm as a program (in a programming language) –Testing the program Each step iterative and the whole process also iterative

Problem Definition & Analysis Understanding the problem is half the solution A precise solution requires a precise definition This step leads to clear definition of the problem The definition states WHAT the problem to be solved rather than HOW the problem to be solved Analysis done to get a complete and consistent specification Specification precisely and unambiguously states –Constraints on the inputs –Desired Properties of the outputs

High Level Strategy This is a crucial and most difficult step Most creative part of the whole process No standard recipe for arriving at a strategy There do exist many standard techniques proved successful –Divide and Conquer –Dynamic Programming –Greedy Approach –Backtracking –Local Search Compare alternate techniques to arrive at the best

Algorithm Development Algorithm precise and complete description of high level strategy The high level strategy, in general, sketchy and usable only by humans Many more details needed which are added in this phase Leads to a precise sequence of instructions Instructions specify the various data objects and operations The data objects are at a very high level closer to the problem domain Various constructs like sequencing, conditionals and looping are used to control the flow of execution of the instruction

Algorithm Analysis Algorithm analyzed for correctness and efficiency They are precise and detailed enough for these analysis Correctness analysis: –to ensure the algorithm solves the given problem –This involves a mathematical proof that algorithm satisfies the specification; termination proofs Efficiency analysis: – to determine the amount of time or number of operations and amount of memory required for executing the algorithm This explores possible alternate designs of data and control structures to select the best possible one

Programming (coding algorithms) Writing programs in a programming language is the last step No doubt it is important and one need to pay attention and care But it is somewhat straightforward It requires mastery over the programming language This step is programming language dependent So in the development of a solution, only this step need to change for a different programming language This step is called implementation or coding

Testing the Program In this phase, the program is compiled to generate machine code that can run on a specific machine Errors could be introduced in the programming process or by the compiler Hence it is essential that the generated code is run with specific set of inputs to see whether it produces the right outputs Syntax errors eliminated in the step Some logical errors may also be caught in this step It also gives an idea about time and space requirements for executing the program

Problem Solving Strategies Arriving at a strategy and an algorithm is the most crucial and difficult step Crucial because behavior of the final code is dependent on this Difficult because it is a creative step Though many standard techniques are available no general recipe to ensure success New problems may require newer strategies Problem solving skills can be developed only with experience Main emphasis of the course: to expose you to various problem solving strategies by way of examples The programming languages is for concreteness and execution of your ideas

Illustrative Examples Problem: Given a set of students examination marks, (range 0 to 100), count the number of students that passed the examination and those passed with distinction; –pass mark: >=50, distinction mark: >=80 Study the problem and analyze Is the problem definition clear?

The Strategy 1. Keep two counters one for pass and the other for distinction 2. Read the marks one by one 3.compare each mark with 50 and 80 and increase the appropriate counters 4. Print the final results

The algorithm Algorithm pass_count: Input: List of marks Output: pass_count, distinction_count 1. initialize p_count, d_count to zero 2. Do while (there is next_mark) steps 2.1 and If next_mark => 50 then increment p_count 2.2 If next_mark => 80 then increment d_count end pass_count

Observations The algorithm is a sequence of precise instructions –Involves variables for storing input, intermediate and output data –uses high level operations and instructions –Data types closer to the problem domain What does the algorithm do for marks that do not lie between 0 and 100 ? Rewrite the algorithm

Correctness of the Solution Is the solution correct? Show that –if an input satisfies the input constraints –then output produced satisfies required properties Input Constraints –List of integers lying between 0 and 100 Required Property –p_count contains the no. of marks >= 50 –d_count contains the no. of marks >= 80 Termination is an implicit requirement

How to establish correctness Establish that if input constraint is satisfied then the program will terminate producing the output that satisfies the desired properties How to establish? –Testing? – How many inputs will convince you? –5, 10, 100 – in general infinite Testing establish presence of bugs never their absence

Mathematical Argument Prove the correctness using mathematical arguments Proof of Correctness involves two-Step argument – Loop Invariants – Loop Termination

Loop invariants A condition (logical expression) involving program variables –It holds initially –If it holds before start of iteration, it holds at the end; –The condition remains invariant under iteration Loop invariant for our example p_count and d_count hold the total number of pass and distinction marks in the marks read so far Loop invariants holds at every iteration if it holds initially In particular, it holds at the end Input constraints imply loop invariant initially Loop invariant at the end, implies output condition

Loop Termination Non termination is an important source of incorrectness. Correctness proof includes termination proof An integer valued expression called bound function that reduces in each iteration When the bound function reaches 0, loop terminates For our example, the bound function is: length of the input list yet to be processed

Efficiency Analysis How many number of operations? –In each iteration of the loop, constant number of comparisons Can we improve this? –If the number is less than 50, there is no need for comparing it with 80. Rewrite the algorithm

Encoding the algorithm How to implement the algorithm? More or less straightforward: –counter variables are integer variables –read and print statements –the loop into a `do statement’ – But wait! –How to terminate the loop? We need to have a protocol for communication –Input the total no. of marks –Input a blank line or line with a special number –What could be the special number?

Program pd_count implicit none integer, parameter:: p_mark = 50 integer, parameter:: d_mark = 80 integer:: p_count,d_count integer:: N, mark, index ! N - the total number of marks to be processed ! mark - temporary variable to store the mark being processed p_count = 0; d_count = 0; read *, N; do index = 1, N read *, mark if (mark > = p_mark) then p_count = p_count + 1 if (mark >= d_mark) then d_count = d_count +1 endif endif end do print *, p_count,d_count

Some Observations 1.Note the use of constant identifiers p_mark and d_mark rather than actual values 50, 80. This is preferable when the pass mark or distinction marks change. 2.variables and constants have meaningful names conveying the intent. Programs need to be maintained. Human understanding is essential 3.All variables need to be initialized 4.Always use implicit none to catch typographical errors

Testing Finally Compile the program and test Why test? We have already verified! What inputs should be given? Inputs that exercise boundary conditions –Total Number of marks: 0, 1 –Actual Marks: 49, 50, 51, 79, 80, 81 Representative Inputs: –Total Number of inputs: 0, 25, 100 –Actual Marks: -34, 0, 25, 67, 92, 120