Procedural Programming. Programming Process 1.Understand the problem 2.Outline a general solution 3.Decompose the general solution into manageable component.

Slides:



Advertisements
Similar presentations
CS101: Introduction to Computer 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
 Control structures  Algorithm & flowchart  If statements  While statements.
CS 1400 Course Reader Introduction. What is Programming? Designing an appropriate algorithm Coding that algorithm in a computer language.
Program Design and Development
Pseudocode and Algorithms
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Computer Science 1620 Programming & Problem Solving.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
Chapter 3 Planning Your Solution
Adapted from slides by Marie desJardins
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Computer Programming 12 Lesson 2 - Organizing the Problem By Dan Lunney.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Introduction 01_intro.ppt
Sw development1 Software Development 1.Define the problem (Analysis) 2.Plan the solution 3.Code 4.Test and debug 5.Maintain and Document.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
An ordered sequence of unambiguous and well-defined instructions that performs some task and halts in finite time Let's examine the four parts of this.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
General Programming Introduction to Computing Science and Programming I.
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Chapter 1 Program Development Asserting Java © Rick Mercer.
Chapter 4 Selection Structures: Making Decisions.
Overview of Programming and Problem Solving Textbook Chapter 1 1.
Cosc175/testing1 Testing Software errors are costly GIGO Preventing Errors –Echo checking –Range and limit checking –Defensive programming.
CS1101: Programming Methodology Aaron Tan.
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.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
1 Ch. 1: Software Development (Read) 5 Phases of Software Life Cycle: Problem Analysis and Specification Design Implementation (Coding) Testing, Execution.
1 Program Planning and Design Important stages before actual program is written.
1. 2 Sales tax is calculated by finding the percent of the total purchase.
Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving.
Chapter#3 Part1 Structured Program Development in C++
INTRODUCTION TO PROGRAMMING. Program Development Life Cycle The program development life cycle is a model that describes the stages involved in a program.
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
The Hashemite University Computer Engineering Department
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Concepts of Algorithms CSC-244 Unit Zero Pseudo code, Flowchart and Algorithm Master Prince Computer College Qassim University K.S.A.
Algorithms and Pseudocode
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
MIT App Inventor Lesson 3 Algorithms Variables Procedures.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
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.
PROBLEM SOLVING. What is a Problem? A problem is a situation that needs to be resolved.
1 Structured Programming Arab Academy for Science and Technology CC112 Dr. Sherif Mohamed Tawfik The Course.
Low-Level Programming Languages, Pseudocode and Testing Chapter 6.
CIS 115 All Exercises Devry University (Devry) FOR MORE CLASSES VISIT CIS 115 All Exercises Devry University.
Algorithms and Flowcharts
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.
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Algorithms and Flowcharts
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Algorithm and Ambiguity
1) C program development 2) Selection structure
Introduction to Algorithms and Programming
Problem Solving Skill Area 305.1
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.
Unit 3: Variables in Java
Presentation transcript:

Procedural Programming

Programming Process 1.Understand the problem 2.Outline a general solution 3.Decompose the general solution into manageable component parts 4.Develop a specific solution for each component 5.Test the solution to each component for correctness 6.Implement the solution to each component in a specific programming language

Programming Process 7.Test implementation of each component 8.Assemble all of the components 9.Test the system of components 10.Document the system of components and the user related features of the program 11.Install the program on the target system of computer hardware and software 12.Test the program in the target environment 13.Train support personnel and users 14.Maintain and support the program

A First Example Assume that you have been asked to write a computer program that allows the user to enter 2 numbers from the keyboard, adds, subtracts, multiplies and divides the numbers and displays the results to the screen.

Understand the Problem How can you solve a problem if you don’t know exactly what the problem is? Is there anything about the problem description that is unclear? Can you describe the input and output specifically?

Outline a General Solution InputProcessingOutput 2 integers Num1 Num2 Display instructions Get input Do 4 calculations Display results Sum Difference Product Quotient

Decompose the General Solution Breaking a complex problem into simpler problems is a useful problem solving strategy. We’ll come back to this step when the problems get more complex.

Develop a Specific Solution This is HARD because people tend to “skip” steps that seem obvious. Computers can’t do that. As an illustration, try to list the steps that you take to brush your teeth! Did you miss anything?

Develop a Specific Solution ArithmeticCalculations Display instructions Get Num1 and Num2 Sum = Num1 + Num2 Difference = Num1 – Num2 Product = Num1 * Num2 Quotient = Num1/Num2 Display appropriate labels and Sum, Difference, Product, Quotient End

Test the Solution for Correctness InputExpected Output Num1Num2SumDiff.ProductQuotient Test Data Test Data 2 (Not yet tested)

Test the Solution for Correctness Num1Num2SumDiff.ProductQuotient ArithmeticCalculations Display instructions Get Num1 and Num2 Sum = Num1 + Num2 Difference = Num1 – Num2 Product = Num1 * Num2 Quotient = Num1/Num2 Display appropriate labels and Sum, Difference, Product, Quotient End

Develop a Specific Solution Some key terminology  Algorithm  Pseudocode  Variable  Assignment statement

Another Example Assume that you have been asked by your local elementary school to create a program that tells students how many quarters, dimes, nickels and pennies should be received as change from a purchase under $1. The program should allow a student to enter the purchase price and should display the change.

Another Example Follow the same process  Can you ask clarifying questions?  Can you create an IPO chart?  Can you create an algorithm? Can you do an example? Can you generalize from the example?  Does the algorithm work?

Another Example CalculatingChange 1.Display instructions 2.Get Price 3.Change = 100 – Price 4.Display Change 5.NumQtrs = integer part of (Change/25) 6.Change = Change – NumQtrs * 25 7.NumDimes = integer part of (Change/10) 8.Change = Change – NumDimes * 10 9.NumNickels = integer part of (Change/5) 10.Change = Change – NumNickels * 5 11.NumPennies = Change 12.Display labels and NumQtrs, NumDimes, NumNickels, NumPennies End

Another Example Pri ce Chan ge Num- Qtrs Nu m- Dim es Num - Nick els Num - Penn ies CalculatingChange Display instructions Get Price Change = 100 – Price Display Change NumQtrs = integer part of (Change/25) Change = Change – NumQtrs * 25 NumDimes = integer part of (Change/10) Change = Change – NumDimes * 10 NumNickels = integer part of (Change/5) Change = Change – NumNickels * 5 NumPennies = Change Display labels and NumQtrs, NumDimes, NumNickels, NumPennies End

Practice Example Assume that you have been asked to write a program to determine the sales tax on an item and display the tax and the total due to the user. The user will enter the price of the item in dollars and cents. The tax rate is 5%.

Practice Example Assume that you have been asked by a friend, who is a beginning programming student, to write a program that converts 4 bit unsigned binary numbers into the decimal equivalent.

Control Structures Control structures are program building blocks that determine the order in which statements in a program are executed The structure theorem states that all programming problems can be solved by using 3 control structures  Sequence  Selection  Repetition

Selection Allows programs to “select” a set of instructions to execute based on the presence (or absence) of a condition Most programming languages have 2 selection statements  If statement ( if in C++)  Case statement (switch in C++)

If Statement Example If Price > 100 Then Display Error Message Else Change = 100 – Price Display Change … End if

If Statement in General If condition Then Statements to execute when true Else Statements to execute when false End If

Selection Problem Assume that you have been asked to write a program to find the largest of a set of 3 numbers. The user will enter 3 integers between 0 and 100. The program will display appropriate messages and display the largest of the 3 numbers.

Selection Problem The first 2 steps are exactly the same  Can you ask clarifying questions to make sure you understand the problem?  Can you create an IPO chart to outline a general solution? Start the algorithm development step the same way too  Can you describe in English the processing steps that you take when you order 3 numbers?

Selection Problem FindLargest (Version1) Display instructions Get Num1, Num2 and Num3 If Num1 > Num2 Then Largest = Num1 Else Largest = Num2 End If If Num3 > Largest Then Largest = Num3 End If Display label and Largest End

Selection Problem FindLargest (Version 2) Display instructions Get Num1, Num2 and Num3 If Num1 > Num2 Then If Num3 > Num1 Then Largest = Num3 Else Largest = Num1 End If Else If Num3 > Num2 Then Largest = Num3 Else Largest = Num2 End If Display label and Largest End

Selection Problem FindLargest (Version 3) 1.Display instructions 2.Get Num1, Num2 and Num3 3.If Num1 > Num2 and Num1 > Num3 Then Largest = Num1 4.Else If Num1 > Num2 and Num3 > Num1 Then Largest = Num3 5.Else If Num2 > Num1 and Num2 > Num3 Then Largest = Num2 6.Else If Num2 > Num1 and Num3 > Num2 Then Largest = Num3 7.End If 8.Display label and Largest End

Selection Problem Num 1 Num 2 Num 3 Largest FindLargest (Test Set 3) Display instructions Get Num1, Num2 and Num3 If Num1 > Num2 and Num1 > Num3 Then Largest = Num1 Else If Num1 > Num2 and Num3 > Num1 Then Largest = Num3 Else If Num2 > Num1 and Num2 > Num3 Then Largest = Num2 Else If Num2 > Num1 and Num3 > Num2 Then Largest = Num3 End If Display label and Largest End 5 5 7

Let’s Generalize An if statement is a selection statement Many if statements look like this If condition Then Statements to execute when true Else Statements to execute when false End If But, there are all kinds of “variations”  This one doesn’t have an “else” part If condition Then Statements to execute when true End If

Let’s Generalize But, there are all kinds of variations  This one has more than one “else” part If condition1 Then Statements to execute if condition1 is true Else If condition2 Then Statements to execute if condition2 is true Else If condition3 Then Statements to execute if condition3 is true … Else Statements to execute if all conditions are false End If

Let’s Generalize But, there are all kinds of variations  This one is “nested” If condition1 Then Statements to execute if condition1 is true Else If condition2 Then Statements to execute if condition2 is true and condition 1 is false Else Statements to execute if condition2 is false and condition1 is false End If

Let’s Generalize Conditions can be more complex too  Most programming languages have 2 logical operators that can be used to make compound conditions And  Gender = M and Age < 25  X > 1 and x < 10 Or  Gender = F or Age >= 25  X 10

Let’s Generalize Many programming languages have another kind of selection statement – the case statement Case Statements  Are very different in structure and function in different programming languages.  You’ll see specific examples when we look at examples of actual procedural programs.

Practice Problem Assume that you’ve been asked to write a program that calculates an employee’s weekly pay. The user enters the pay rate and the hours worked. The program displays regular pay, overtime pay and weekly pay.

Practice Problem Assume that you’ve been asked to write a program that determines the letter grade received by a student in a course. The total number of points in the term is 400. Earning 90% of the points gives students an A, 80% is a B, 70% is a C, 60% is a D and less than 60% is an F.