18
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 definition more closely an ordered sequence means that you can number the steps (it's socks then shoes!) unambiguous and well-defined instructions means that each instruction is clear, do-able, and can be done without difficulty performs some task halts in finite time (algorithms terminate!)

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

Embed Size (px)

Citation preview

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 definition more closely an ordered sequence means that you can number the steps (it's socks then shoes!)

unambiguous and well-defined instructions means that each instruction is clear, do-able, and can be done without difficulty

performs some task halts in finite time (algorithms terminate!)

Three Categories of Algorithmic OperationsAlgorithmic operations are ordered in that there is a first instruction, a second instruction etc. However, this is not enough. An algorithm must have the ability to alter the order of its instructions. An instruction that alters the order of an algorithm is called a control structure

Three Categories of Algorithmic Operations: sequential operations - instructions are executed in order

conditional  ("question asking") operations - a control structure that asks a true/false question and then selects the next instruction based on the answer

iterative operations (loops) - a control structure that repeats the execution of a block of instructions

Unfortunately not every problem or task has a "good" algorithmic solution. There are  unsolvable problems - no algorithm can exist to solve the problem (Halting Problem)

"hard" (intractable) problems - algorithm takes too long to solve the problem (Traveling Salesman Problem)

problems with no known algorithmic solution

How to represent algorithmsUse natural languages

too verbose too "context-sensitive"- relies on experience of

reader Use formal programming languages

too low level requires us to deal with complicated syntax of

programming language Pseudo-Code - natural language constructs

modeled to look like statements available in many programming languages

Pseudo-Code is simply a numbered list of instructions to perform some task. In this course we will enforce three standards for good pseudo code Number each instruction. This is to enforce the notion of an ordered sequence of ... operations. Furthermore we introduce a dot notation (e.g. 3.1 come after 3 but before 4) to number subordinate operations for conditional and iterative operations

Each instruction should be unambiguous (that is the computing agent, in this case the reader, is capable of carrying out the instruction) and effectively computable (do-able).

Completeness. Nothing is left out.

Example #1Computing Sales Tax : Pseudo-code the task of computing the final price of an item after figuring in sales tax. Note the three types of instructions: input (get), process/calculate (=) and output (display)

get price of item get sales tax rate sales tax = price of time times sales tax rate final prince = price of item plus sales tax display final price halt

Variables: price of item, sales tax rate, sales tax, final price

Example #2Computing Weekly Wages: Gross pay depends on the pay rate and the number of hours worked per week. However, if you work more than 40 hours, you get paid time-and-a-half for all hours worked over 40. Pseudo-code the task of computing gross pay given pay rate and hours worked.

get hours worked get pay rate if hours worked ≤ 40 then

gross pay = pay rate times hours worked else

gross pay = pay rate times 40 plus 1.5 times pay rate times (hours worked minus 40) display gross pay halt

variables:  hours worked, ray rate, gross pay

Computation/Assignment  

set the value of "variable" to :"arithmetic expression" or "variable" equals "expression" or "variable" = "expression"

Input/Output

get "variable", "variable", ... display "variable", "variable", ...

Conditional (dot notation used for numbering subordinate statements)

if  "condition"

(subordinate) statement 1 etc ...

else

(subordinate) statement 2 etc ...

Iterative (dot notation used for numbering subordinate statements)

while "condition"

(subordinate) statement 1 etc ...

1 Request the number of students; 2 Get the grade for each student; 3 Process the average grade for the

whole school;

Application start; 02   Declare an integer variable numberOfStudents; 03   Declare an integer variable counter; 04   Declare an integer variable sum; 05   Declare a decimal variable average; 06    07   Set the numberOfStudents value to 0; 08   Set the counter value to 0; 09   Set the sum value to 0; 10    11   Request the number of students (numberOfStudents); 12    13   If the entered number of students is 0 then 14         Exit the application 15   Else 16     While the counter is less or equal to the number of students 17         Get the student grade 18         Add the student grade to the sum variable 19         Increment the counter by 1. 20         Get the next student information 21    22   When the student information is entered, set the average variable to the result of the division (sum

divided by the number of students). 23   Show the result. 24 Application end.

Get into your assigned group As a group you will take 5 minutes and assign group members to

a task. As a group you will choose what your Algorithm will accomplish.

What is your program. Take 15 min. Task choice1. Time Keeper: 1 person will use Microsoft Excel to create a

timeline for groups completion and implement each completed task on a Power Point Presentation.

2. Designer: 2 students will create 2 flow charts in Microsoft Excel first flowchart describes each group members role and responsibility. 2nd flowchart will consist of description of Pseudocode.

3. Coders: 2 students will create the Pseudocode and Algorithm.

Friday we will present and implement code. We will view C++ and JAVA