Programming Fundamentals Introduction to Problem Solving  Programming is a problem solving activity. When you write a program, you are actually writing.

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer programming
Advertisements

1 CS101 Introduction to Computing Lecture 17 Algorithms II.
Chapter 2: Problem Solving
Chapter 2: Problem Solving
Chapter 1 - An Introduction to Computers and Problem Solving
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Fundamentals of Algorithms MCS - 2 Lecture # 4
ITEC113 Algorithms and Programming Techniques
Computer Science 1620 Programming & Problem Solving.
Programming Fundamentals (750113) Ch1. Problem Solving
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
Chapter 3 Planning Your Solution
PRE-PROGRAMMING PHASE
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Fundamentals of C programming
ALGORITHMS AND FLOWCHARTS
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
DCT 1123 Problem Solving & Algorithms
Chapter 2: Problem Solving
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 2: Problem Solving.
Programming Techniques I SCJ1013
Chapter 2: Problem Solving
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
U NDERSTANDING P ROBLEMS AND HOW TO S OLVE THEM BY USING C OMPUTERS.
Muhammad Adnan Talib Lec#4 27 th Sep,2012 Computer Science Department COMSATS Institute of Information Technology Sahiwal Introduction to Computer Programming.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
Programming Concepts Chapter 3.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
Slide 1 Software Development 1. Requirement Specification 2. Analysis 3. Algorithm Design 4. Implementation 5. Testing 6. Documentation.
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.
Module 4 Part 1 Introduction To Software Development : Systems Analysis & Design Introduction To Software Development : Systems Analysis & Design.
กระบวนการแก้ปัญหาด้วย คอมพิวเตอร์ 3 พฤษภาคม :00-17:00.
PROGRAMMING PAPER 2 AS Algorithms.
ITEC113 Algorithms and Programming Techniques
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
Principles of Programming - NI July Chapter 2: Problem Solving In this chapter you will learn about: Introduction to Problem Solving Software development.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
1 Program Planning and Design Important stages before actual program is written.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
 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
CSEB114: Principle of programming
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
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Lecture 3: Developing Procedural Thinking (How to think like a programmer) B Burlingame 16 Feb 2016.
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.
INTRODUCTION TO COMPUTER PROGRAMMING(IT-303) Basics.
Introduction to Flowcharts
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
 Problem Analysis  Coding  Debugging  Testing.
ALGORITHMS AND FLOWCHARTS
2008/09/22: Lecture 6 CMSC 104, Section 0101 John Y. Park
ALGORITHMS AND FLOWCHARTS
Algorithms and Flowcharts
Introduction To Flowcharting
Programming Fundamentals
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
Structured Program
ALGORITHMS AND FLOWCHARTS
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Understanding Problems and how to Solve them by using Computers
Flowcharts and Pseudo Code
Basic Concepts of Algorithm
Presentation transcript:

Programming Fundamentals Introduction to Problem Solving  Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer to solve something for you.  Problem solving is the process of transforming the description of a problem into a solution by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-solving strategies, techniques and tools. 1

Programming Fundamentals Case Study: Yummy Cupcake  Problem: You are required to calculate the amount to be paid by a customer buying cupcakes. 2

Programming Fundamentals Software Development Method (SDM)  For programmer, we solve problems using Software Development Method (SDM), which is as follows: 1.Specify the problem requirements. 2.Analyze the problem. 3.Design the algorithm to solve the problem. 4.Implement the algorithm. 5.Test and verify the completed program. 6.Documentation 3

Programming Fundamentals 1. Requirement Specification  Specifying the problem requirements requires you to state the problem clearly and to gain the understanding of what to be solved and what would be the solution.  When specifying problem requirement, we ask ourselves the following questions:  What the problem is.  What the solution should provide.  What is needed to solve it.  If there are constraints and special conditions. 4

Programming Fundamentals Yummy Cupcake Problem: You are required to calculate the amount to be paid by a customer buying cupcakes.  What the problem is.  What the solution should provide.  What is needed to solve it.  If there are constraints and special conditions. 5

Programming Fundamentals Problem Analysis  Analyzing the problem require us to identify the following:  Input(s) to the problem, their form and the input media to be used  Output(s) expected from the problem, their form and the output media to be used  Special constraints or conditions (if any)  Any formulas or equations to be used 6

Programming Fundamentals Yummy Cupcake  Input?  Quantity of the cupcake purchased (integer)  Price per cupcake (float)  Output?  Total amount to be paid by the customer (float)  Constraint/condition?  Quantity purchased must be more than zero  Price per cupcake must be more than zero (it is not free)  We assume that the price given is the standard price to all cupcakes  Formula/equation?  Amount to pay = quantity of cupcake x price per cupcake 7

Programming Fundamentals Designing algorithm  Designing algorithm to solve the problem requires you to develop a list of steps, arranged in a specific logical order which, when executed, produces the solution for a problem.  Using top-down design (also called divide and conquer):  You first list down the major tasks  For each major task, you further divide it into sub- tasks (refinement step)  When you write algorithm, write it from the computer’s point of view. 8

Programming Fundamentals Designing Algorithm cont..  An algorithm must satisfy these requirements:  It may have an input(s)  It must have an output(s)  It should not be ambiguous (there should not be different interpretations to it. Every step in algorithm must be clear as what it is supposed to do)  It must be general (it can be used for different inputs)  It must be correct and it must solve the problem for which it is designed  It must execute and terminate in a finite amount of time  It must be efficient enough so that it can solve the intended problem using the resource currently available on the computer 9

Programming Fundamentals Yummy Cupcake Major Task: 1.Read the quantity of cupcake purchased 2.Read the price per cupcake 3.Calculate total amount to pay 4.Display the total amount to pay However, looking at the above algorithm, we can still further refine step 3, by introducing the formula to calculate the amount to pay. After refinement: 1.Read the quantity of cupcake purchased 2.Read the price per cupcake 3.Total amount to pay = quantity of cupcake x price per cupcake 4.Display the total amount to pay 10

Programming Fundamentals Remember, the order of the steps in algorithm is very important. Consider the following, will the result be the same? 1.Display the total amount to pay 2.Get the quantity of cupcake purchased 3.Total amount to pay = quantity of cupcake x price per cupcake 4.Get the price per cupcake 11

Programming Fundamentals Control Structure  An algorithm can be represented using Pseudocode or Flowchart.  In 1966, two researchers, C. Bohn and G. Jacopini, demonstrated that any algorithm can be described using only 3 control structures: sequence, selection and repetition. 12

Programming Fundamentals Control Structure  Sequence: A series of steps or statements that are executed in the order they are written in an algorithm.  Selection: Defines two courses of action depending on the outcome of a condition. A condition is an expression that is, when computed, evaluated to either true or false.  Repetition: Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied. 13

Programming Fundamentals 14 You may have more than one control structure in one program in order to solve a problem.

Programming Fundamentals Pseudocodes  A pseudocode is a semiformal, English-like language with limited vocabulary that can be used to design and describe algorithms.  Criteria of a good pseudocode:  Easy to understand, precise and clear  Gives the correct solution in all cases  Eventually ends 15

Programming Fundamentals Flowcharts  Flowcharts is a graph used to depict or show a step by step solution using symbols which represent a task.  The symbols used consist of geometrical shapes that are connected by flow lines.  It is an alternative to pseudocoding; whereas a pseudocode description is verbal, a flowchart is graphical in nature. 16

Programming Fundamentals Flowchart Symbols 17 Terminal symbol - indicates the beginning and end points of an algorithm. Process symbol - shows an instruction other than input, output or selection. Input-output symbol - shows an input or an output operation. Disk storage I/O symbol - indicates input from or output to disk storage. Printer output symbol - shows hardcopy printer output.

Programming Fundamentals Flowchart Symbols cont… 18 Selection symbol - shows a selection process for two-way selection. Off-page connector - provides continuation of a logical path on another page. On-page connector - provides continuation of logical path at another point in the same page. Flow lines - indicate the logical sequence of execution steps in the algorithm.

Programming Fundamentals The Sequence control structure  A series of steps or statements that are executed in the order they are written in an algorithm.  The beginning and end of a block of statements can be optionally marked with the keywords begin and end. begin statement 1. statement 2. … statement n. end 19 statement 1 statement 2 … statement n begin end

Programming Fundamentals The Sequence control structure Begin read birth year age = current year – birth year display age End 20 Problem: calculate a person’s age read birth year Age = current year – birth year Display age begin end

Programming Fundamentals The Selection control structure  Defines two courses of action depending on the outcome of a condition. A condition is an expression that is, when computed, evaluated to either true or false.  The keyword used are if and else.  Format: if (condition) then-part else else-part end_if 21 Condition? else- statement(s) then- statement(s) YesNo

Programming Fundamentals The Selection control structure Begin read age if (age is greater than 60) print “Senior Citizen” else print “Junior Citizen” end_if End 22 Begin Read age End age > 60? NO YES print “Senior Citizen” print “Junior Citizen” Begin read age if (age > 60) print “Senior Citizen” else print “Junior Citizen” end_if End

Programming Fundamentals Pseudocodes: The Selection control structure  Sometimes in certain situation, we may omit the else-part. if (number is odd number) print “This is an odd number” end_if  Nested selection structure: basic selection structure that contains other if/else structure in its then-part or else-part. if (number is equal to 1) print “One” else if (number is equal to 2) print “Two” else if (number is equal to 3) print “Three” else print “Other” end_if 23 Example 1 Example 2

Programming Fundamentals Exercise Draw the flowchart diagram for Example 1 and Example 2 24

Programming Fundamentals The Repetition control structure  Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied.  The keyword used is while.  Format: while (condition) loop-body end_while 25 Condition? Loop Statement(s) yes no

Programming Fundamentals Problem: Write a program that reads and displays the age of 10 people (one after another). 26 For this problem, we need a way to count how many people whose age have been processed (read and displayed). Therefore, we introduce a concept of counter, a variable used to count the number of people whose age have been processed by the program.

Programming Fundamentals Begin number of users giving his age = 1 while (number of users giving his age <= 10) read the age from the user. print the user age. number of user giving his age + 1 end_while End 27 Begin users = 1 while (users <= 10) read age print age. users = users + 1 end_while End Counter initialisation Loop condition Updating counter

Programming Fundamentals 28 Begin End users <= 10? NO YES users = 1 print age read age users =users + 1

Programming Fundamentals Subsequently.. Begin number of users giving his age = 0 while (number of users giving his age < 10) read the age from the user. print the user age. number of user giving his age + 1 end_while End 29 Begin users = 0 while (users < 10) read age print age. users = users + 1 end_while End The loop condition must less than the value it requires to stop You can start the counter with ZERO Be consistent

Programming Fundamentals Little extra…  Now let us put together everything that you have learnt so far.  Problem: Write a program that will calculate and print the age of 10 persons, given their birth year. If the age of the person is above 55, then the program will print “Pencen”, otherwise, the program will print “Kerja lagi”. 30

Programming Fundamentals Begin users = 1 while (users <= 10) begin Read birth year age = current year – birth year print age if age > 55 print “Pencen” else print “Kerja lagi” end_if users = users + 1 end end_while End 31 Note that in this example, we are using all the three control structures: sequence, selection and repetition Example 3

Programming Fundamentals Exercise Draw the flowchart diagram for Example 3 32

Programming Fundamentals Implementation  The process of implementing an algorithm by writing a computer program using a programming language (for example, using C language)  The output of the program must be the solution of the intended problem  The program must not do anything that it is not supposed to do (Think of those many viruses, buffer overflows, trojan horses, etc. that we experience almost daily. All these result from programs doing more than they were intended to do) 33

Programming Fundamentals Testing and Verification  Program testing is the process of executing a program to demonstrate its correctness  Program verification is the process of ensuring that a program meets user- requirement  After the program is compiled, we must execute the program and test/verify it with different inputs before the program can be released to the public or other users (or to the instructor of this class) 34

Programming Fundamentals Documentation  Writing description that explain what the program does.  Can be done in 2 ways:  Writing comments between the line of codes  Creating a separate text file to explain the program  Important not only for other people to use or modify your program, but also for you to understand your own program after a long time (believe me, you will forget the details of your own program after some time...) 35

Programming Fundamentals Documentation cont…  Documentation is so important because:  You may return to this program in future to use the whole of or a part of it again  Other programmer or end user will need some information about your program for reference or maintenance  You may someday have to modify the program, or may discover some errors or weaknesses in your program  Although documentation is listed as the last stage of software development method, it is actually an ongoing process which should be done from the very beginning of the software development process. 36

Programming Fundamentals Exercise time!!! 37

Programming Fundamentals Volume calculation Write a pseudocode and a flowchart for a C++ program that reads the value of height, width and length of a box from the user and prints its volume. 38

Programming Fundamentals Calculating Electricity Bills The unit for electricity usage is kWh. For domestic usage, the monthly rate is 21.8 cents/unit for the first 200 unit, 25.8 cents/unit for the next 800 units and 27.8 cents/unit for each additional units. Given the amount of electricity units (in kWh) used by a customer, calculate the amount of money needs to be paid by the customer to TNB. A bill statement needs to be printed out. Write a pseudocode or a flow chart to solve the above problem. 39

Programming Fundamentals Sum of 1 to 10 Write a pseudocode or flowchart for a program that would compute and print the sum of all integers between 1 and

Programming Fundamentals Summary  This lecture introduced the concept of problem solving : a process of transforming the description of a problem into a solution.  A commonly used method – SDM which consists of 6 steps  3 basic control structures : sequence, selection and repetition structures  Pseudocode and Flow chart T.H.E E.N.D 41