Algorithms... series of instructions that rigorously defines the solution to a problem.
Types of Algorithms:
- Simple
- Conditional
- Loops
- Arrays
Formats in which algorithms are represented?
- Narrative ~ written in plain English text. Example: Instructions on a recipe card.
- Pseudocode ~ English-like text and code. Example: Read num (input a number)
- Structured ~ Codes. The only format that is executable.
- Flowchart ~ graphical (or pictorial) representation of an algorithm.
When writing algorithms, there are some words that have special meanings. These are known as reserved words. Listed below are a few of the reserved words used in algorithms.
Print - display on screen. Used in output statements
Input - enter via keyboard. Used in input statements
Input key - press any key to continue. Used to terminate an algorithm; called the terminator
name - a reserved word; cannot be used as a variable name
width - a reserved word; cannot be used as a variable name
add - a reserved word; cannot be used as a variable name
date - a reserved word; cannot be used as a variable name
Algorithms fall under the first phase in computer program development (Problem Solving Phase)
Steps in the problem solving phase:
- Define the problem
- Find a solution
- Evaluate alternate solutions
- Choose the most efficient solution and write an algorihtm
- Test the algorithm for correctness
TO define the problem, programmers use a tool called a Defining Diagram which is a three column table that separates a problem into input, processing, and output. This is commonly referred to as an IPO table. (Input/Processing/Output)
Example:
I
|
P
|
O
|
Num1
Num2
|
1.
Read Num1, Num2
2.
Calculate Sum
3.
Print Sum
|
Sum
|
In the Input (I) section, anything that the user has to enter goes here
In the Processing (P) section, anything that the computer has to do goes here
In the Output (O) section, the solution values that will be displayed goes here
You try one: Prepare an IPO table to solve the problem:
A technician makes $90.00 per day and 1.5 times that value for overtime hours. Write an algorithm that prompts the user to enter the amount of days and overtime hours worked by the technician, and calculate the week salary for the technician. Your algorithm should display the total week salary with suitable labels.
After you've completed the IPO table, give it a try to write the algorithm. It's a simple algorithm. Submit your solution via email for bonus points on your lowest quiz grade. Value - 2 bonus points
No comments:
Post a Comment