Problem Solving and Algorithms

Similar documents
IT 1033: Fundamentals of Programming Loops

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

Course Outline. Introduction to java

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

An Introduction to Computer Problem Solving and the Programming Process

1. Introduction to Programming

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Steps to program development

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

CS302: Self Check Quiz 2

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

4*4*4 2. What is the output of the following flowchart for the values given below: (25 p)

Algorithms and Problem Solving

REPRESENTING ALGORITHMS. REPRESENTING ALGORITHMS IB DP Computer science Standard Level ICS3U

Programming Logic and Design Sixth Edition

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

Chapter 1 Lab Algorithms, Errors, and Testing

PLD Semester Exam Study Guide Dec. 2018

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

PSEUDOCODE AND FLOWCHARTS. Introduction to Programming

ALGORITHMS AND FLOWCHARTS

Control Statements. Musa M. Ameen Computer Engineering Dept.

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Checking Multiple Conditions

BRANCHING if-else statements

Lecture_Module3 Prof Saroj Kaushik, IIT Delhi

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Program Control Flow

Program Control Flow

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

Introduction to Computer Programming

CS111: PROGRAMMING LANGUAGE1. Lecture 2: Algorithmic Problem Solving

Chapter 8 Algorithms 1

conditional statements

Kadi Sarva Vishwavidyalaya, Gandhinagar

Euclid s algorithm, 133

11.3 Function Prototypes

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Review. Relational Operators. The if Statement. CS 151 Review #4

5. Selection: If and Switch Controls

Condition Controlled Loops. Introduction to Programming - Python

Constructing Algorithms and Pseudocoding This document was originally developed by Professor John P. Russo

Controls Structure for Repetition

Chapter 2: Input, Processing, and Output

Outline. Program development cycle. Algorithms development and representation. Examples.

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Chapter 3 Structured Program Development

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Software Development Techniques. December Sample Examination Paper. Time: 3 hours

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Programming Logic Beginning

Programming Logic - Beginning

Java Coding 3. Over & over again!

PROCESS SPECIFICATION

Computer System and programming in C

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

Outline. policies. with some potential answers... MCS 260 Lecture 19 Introduction to Computer Science Jan Verschelde, 24 February 2016

Chapter 4 Computation

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Quiz Determine the output of the following program:

Making Decisions In Python

Control Statements Loops

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Building Java Programs


Chapter Two: Program Design Process and Logic

Fundamentals of Programming Session 7

STUDENT LESSON A12 Iterations

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Chapter 4. Computation. Bjarne Stroustrup.

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Chapter 3. Selections

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Algorithm Discovery and Design

Final Examination Semester 3 / Year 2010

Chapter 2 Working with Data Types and Operators

Chapter Five: Functions

Computer Programming I - Unit 5 Lecture page 1 of 14

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Review for Test 1 (Chapter 1-5)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

4. The is a diagram that graphically depicts the steps that take place in a program. a. Program b. Flowchart c. Algorithm d. Code e.

Handout 4 Conditionals. Boolean Expressions.

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Tutorial Exercise 8 Answer

Chapter 4: Making Decisions

CS 139 Practice Midterm Questions #2

Transcription:

Problem Solving and Algorithms Problem Solving We do it all the time Approaches: Less successful Grope blindly toward a solution Fail to complete a chain or reasoning Successful Begin with what is understood Draw on other information sources Problem Solving in CS Algorithmic thinking SJAllan 2005 Problem Solving and Algorithms 2 Algorithm A detailed sequence of actions (instructions) performed to accomplish some task Named after an Iranian mathematician, Al-Khawarizmi Languages for expressing algorithms Computers and Algorithms A computer carries out an algorithm to process information This algorithm is called a program SJAllan 2005 Problem Solving and Algorithms 3 SJAllan 2005 Problem Solving and Algorithms 4 1

Requirements of an Algorithm Useful Methodology Be well-ordered Be unambiguous Halt in a finite amount of time Be doable Produce a result Assumptions? Top down design Stepwise refinement SJAllan 2005 Problem Solving and Algorithms 5 SJAllan 2005 Problem Solving and Algorithms 6 Steps in Program Development 1. Analyze the problem 2. Outline the solution 3. Develop an algorithm 4. Test the algorithm 5. Code the algorithm 6. Run and test the program 7. Document and maintain the program. Step 1: Analyze the Problem Output Input Given Values Processing SJAllan 2005 Problem Solving and Algorithms 7 SJAllan 2005 Problem Solving and Algorithms 8 2

Perform Step 1 for the following problem statement: Write an algorithm that accepts the length and width of a rectangular city lot, the length and width of the house built on that lot, and computes and displays the time needed to mow the lawn at the rate of 2 square meters per minute. Step 2: Outline the Solution Top-down design Stepwise refinement Consider order What is done first, second, etc.? Is there any repetition? Are there any situations where we need to select from alternate choices? SJAllan 2005 Problem Solving and Algorithms 10 Refer to the results for the following problem statement of Step 1 and perform Step 2: Write an algorithm that accepts the length and width of a rectangular city lot, the length and width of the house built on that lot and computes and displays the time needed to mow the lawn at the rate of 2 square meters per minute. Step 3: Design an Algorithm Constrained by what a computer can do NOTE: As you gain more experience, you may find yourself combining Steps 2 and 3. SJAllan 2005 Problem Solving and Algorithms 12 3

Variables Storage locations in memory Address Contents Destructive write Non-destructive read Naming conventions CONTROL STRUCTURES Sequence Selection Repetition SJAllan 2005 Problem Solving and Algorithms 13 SJAllan 2005 Problem Solving and Algorithms 14 Elements of our pseudocode: Keywords Operators Punctuation Pseudo code keywords for: Input Read from a file Get from the keyboard Output Print, Display to the monitor Write to a file Prompts and Labels SJAllan 2005 Problem Solving and Algorithms 15 SJAllan 2005 Problem Solving and Algorithms 16 4

Assigning a value to a variable Initialization and/or Assignment variable = value Set variable to value Initialize variable to value Mathematical Operators + / (binary) (unary) Refer to the results for the following problem statement of Steps 1 and 2 and perform Step 3 : Write an algorithm that accept the length and width of a rectangular city lot, the length and width of the house built on that lot and computes and displays the time needed to mow the lawn at the rate of 2 square meters per minute. SJAllan 2005 Problem Solving and Algorithms 17 Order Of Execution Truth Tables Conditions Boolean values true or false Relational Operators < > == <= >=!= Complex Conditions Logical Operators Value of Condition 1 Value of Condition 2 Condition 1 And Condition 2 And Or Not SJAllan 2005 Problem Solving and Algorithms 19 SJAllan 2005 Problem Solving and Algorithms 20 5

Truth Tables Truth Tables Value of Condition 1 Value of Condition 2 Condition 1 Or Condition 2 Value of Condition 1 Not Condition 1 SJAllan 2005 Problem Solving and Algorithms 21 SJAllan 2005 Problem Solving and Algorithms 22 Selection Structure: two alternatives If condition Then the condition is true Else the condition is false End If Selection Structure: one alternative If condition Then the condition is true End If 6

Selection Structure: multiple alternatives If condition 1 Then condition 1 is true Else If condition 2 Then condition 2 is true Else If condition 3 Then condition 3 is true Selection Structure: multiple alternatives (cont.) Else If condition n Then condition n is true Else none of the previous conditions are true End If The Dollar Dress Store is having a sale where the price of any item previously marked down is reduced by 30%. Design an algorithm that asks for the price of an item and prompts for entry of Yes if the item was previously marked down. The algorithm then calculates the cost of the item, calculates 6.25% sales tax, and prints out a bill. Rah-Rah University has a scholarship available for a single mother who is a fulltime student. Design an algorithm that reads a student s name, marital status (M for married, N for not married), gender (M for male, F for female), student status (F for full-time, P for part-time), and number of children from a file and displays the student s name and an appropriate message concerning eligibility for the scholarship. 7

Repetition Structures Body of the loop Counted Loops Condition-controlled Loops Counted Loop Repeat numeric value Times statement(s) to be repeated End Repeat SJAllan 2005 Problem Solving and Algorithms 29 Design an algorithm that adds the integers from 1 to 100 and displays the sum on screen. Change the algorithm so that it determines the sum of the integers between two numbers entered by the user. 8

Condition-controlled Loop While condition statements to be repeated as long as the condition is true End While Condition-Controlled Loop (cont.) Loop condition Loop Control Variable (LCV) Identify LCV(S) and condition(s) Prime the loop Change value of LCV inside loop Sentinel value SJAllan 2005 Problem Solving and Algorithms 34 Design an algorithm that prompts for and receives pairs of integer values from a user and displays their sum, product, and average on the screen. Entry of a pair of zero values signals the end of data entry. The algorithm also prints the number of pairs entered. Multiple and Nested Structures Multiple Ifs End Ifs Multiple Repeats and/or Whiles If End Ifs and Repeats and/or Whiles If End If inside Then and/or inside Else If End If inside Repeat or While Repeat or While inside Repeat or While Repeat or While inside Then and/or inside Else SJAllan 2005 Problem Solving and Algorithms 36 9

Visualization of Proper Multiple/Nested Structures Revise the previous algorithm so that an asterisk is displayed beside the sum of any pair that is greater than 200. NO! A file of student records contains name, gender (M or F), age (in years), and marital status (S for single or M for married). Design an algorithm that reads through the file until a name of Quit is read and computes the numbers of married men, single men, married women, and single women. Output these counts to a file called StudentSummary. Output the names of any single men over 30 to a file called OlderSingleMen. A company needs a program to process the payroll for its 15 employees.the algorithm reads an employee s number, pay rate, and the number of hours worked. The algorithm computes the employee s pay and prints it along with the input data. According to the company s rules, no employee is paid for more than 60 hours and the maximum hourly rate is $25.00. If more than 35 hours are worked, then payment for the overtime hours worked is calculated at time-and-a-half. If the hours worked or hourly rate for an employee is out of range, then the input data and an appropriate message is printed and no pay is calculated. 10

Step 4: Test the Algorithm Design a set of test data Compute the results by hand Step through the algorithm Play the role of the computer Keep track of the values stored in the variables (hand trace) Compare results to hand-calculated results Step 5: Code the algorithm Programming Languages Machine language High-level languages C++, Java Compilers Syntax SJAllan 2005 Problem Solving and Algorithms 41 SJAllan 2005 Problem Solving and Algorithms 42 Step 6: Run and Test the Program Errors Syntax Errors Run-time Errors Logic Errors Test data Step 7: Document and Maintain the Program Documentation Internal (comments) User (external) Maintenance SJAllan 2005 Problem Solving and Algorithms 43 SJAllan 2005 Problem Solving and Algorithms 44 11