Download presentation
Presentation is loading. Please wait.
Published byAntonia Baldwin Modified over 9 years ago
1
CS221 Algorithm Basics
2
What is an algorithm? An algorithm is a list of instructions that transform input information into a desired output. Each instruction is a finite set of steps which can be executed in a defined order on a deterministic machine. The execution of the instructions must terminate in a finite amount of time. Finite number of instructions Executable instructions Finite amount of time
3
Example Algorithm Algorithm: Square 1) Request a value for X 2) Compute the value S = X * X 3) Provide the computed S to the caller How does this algorithm fit the definition?
4
Algorithm Interface Every algorithm has an interface defined by Input information needed for the algorithm to execute Output information produced by the algorithm When creating an algorithm, you should clearly specify the algorithm interface.
5
Problem-Solving Phase Analyze the problem and specify what the solution must do Develop a general solution (algorithm) to solve the problem Verify that your solution really solves the problem
6
Expressing an Algorithm To express an algorithm, it is often useful to write out the steps in great detail using a spoken language. Some algorithms are so simple that it is sufficient to express them using a high level language code. Most algorithms should be expressed using pseudocode. Pseudocode is a mix of code-style formatting and commands with spoken language words (example later)
7
A Good Design Three Properties of a Good Design Shows a good understanding and some analysis of the problem we are trying to solve. Lists inputs/starting point and outputs/desired result. Shows a plan for a set of steps to get from the starting point to the desired result. Gives those steps in enough detail to TEACH them. Offers enough information that a programmer could use it as a guide to write a program for solving the problem.
8
The Summation Problem Problem You need to write a set of instructions that will determine the summation value of a given number. The answer is the sum of every number from 1 up to and including the given number. For example: SummProgram( 4 ) = 10 because 1 + 2 + 3 + 4 = 10
9
Creating an Algorithm An algorithm is a way to instruct your computer to solve a problem or perform a task. The best way to identify an algorithm for a given problem is to outline the steps you would go through if you were asked to solve the problem
10
Creating an Algorithm First, identify the major checkpoints which must be accomplished. Then, break each major checkpoint down into simple sub-tasks which are easily translated into computer code. This approach is known as “Top-Down Design” Begin at the top, with the big picture Fill in the details to correspond with the big picture
11
Main Algorithm Ask the User for the Number Subprogram -> Get the Summation Value of that number Report the Answer to the User
12
Summation Algorithm Set temporary answer to zero. Add the number to the temporary answer Subtract one from number Repeat steps 2 and 3 until the number is decreased to zero
13
PseudoCode 1) Ask User for Number 2) Store Number 3) Find the Summation Value for Number 1) Set Answer to Zero 2) While Number is Greater Than Zero 1) Add Number to Answer 2) Subtract One from Number 4) Report Answer
14
Verify Solution BEFORE Writing Computer Code It is easier to identify and correct problems with your solution at this point when it is still in your own words!
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.