05-CH10-17 (1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Programs areAlgorithms

programs: algorithms that have been


specialized to a specific set of conditions
and assumptions
– usually written in a specific programming
language
– intended to have the computer complete a
task or solve a problem
•The words program and algorithm are used
interchangeably (but should not be!)
Algorithm we have learned
Converting Binary to Decimal
1. If there is a 1, write the place value for its
position in decimal.
2. If there is a 0, write nothing.
3. Add up the place values.
What are algorithms we use in our daily lives?
Probably
a "bad"
algorithm

From "How to Solve it" by George Polya


Algorithms vs. Heuristic Processes
• not all processes given in the book are
algorithms
• the process to find information on the web
using a search engine was not an
algorithm
– not systematic
– not guaranteed to find it (process could fail)
– called a heuristic process:
– helpful procedure for finding a
result
– often an approximation for a
solution
5 Steps for Creating an Algorithm
1. Input Specified
What is the input / data to be transformed
during the computation to produce the
output
• What data do you need to begin to get
the result you want?
• Input precision requires that you know
what kind of data, how much and what
form the data should be
5 Steps for Creating an Algorithm
2. Output Specified
The output is the data resulting from the
computation (your intended result)
• Frequently the name of the algorithm
contains the output:
– “Algorithm to compute batting average”
• Output precision also requires that you
know what kind of data, how much and
what form the output should be (or
even if there will be any output at all!)
5 Steps for Creating an Algorithm
3. Definiteness
Algorithms must specify every step and the
order the steps must be taken in the process
• Definiteness means specifying the
sequence of operations for turning
input into output (process)
• Details of each step must be spelled
out (including how to handle errors)
5 Steps for Creating an Algorithm
4. Effectiveness
For an algorithm to be effective, each of its
steps must be doable
• The agent must be able to perform
each step without any outside help or
extraordinary powers
5 Steps for Creating an Algorithm
5. Finiteness
The algorithm must stop, eventually!
• Stopping may mean that you get the
expected output OR you get a
response that no solution is possible
• Finiteness is not usually an issue for
non-computer algorithms
• Computer algorithms often repeat
instructions with different data and
finiteness may be a problem
• Infinite Loop problem
Algorithm Fact #1
1. Algorithms can be specified at different
levels of detail
– Algorithms use functions to simplify the
algorithmic description
– These functions (such as add) may have
their own algorithms associated with them
Algorithm Fact #2
2. Algorithms always build on functionality
previously defined and known to the user
– Assume the use of familiar functions and
algorithms
– For example, “divide 71 by 2”
Algorithm Fact #3
3. Different algorithms can solve the same
problem differently, and the different
solutions can take different amounts of
time (or space)
– But the solutions equal
Algorithms
Algorithm examples:

What does it accomplish?


1. Stand up
2. Assign yourself the number 1
3. Find someone standing
4. Add your number to that person's number and store the result
5. One sits down, one stays standing
6. If you are still standing go back to step 3

http://video.google.com/videoplay?docid=8760693941184809872#

14
Flowcharts
• Visual representation of sequence of
events to be performed in an algorithm
• Tool to review, capture and communicate
logic flow of a program
Flowcharts
1. All steps should be listed logical order
2. Be clear, neat and easy to flow
3. Usual direction flow is left to right or top to bottom
4. Flow lines: Used to connect steps
A. Start = 1 line out
B. End = 1 line in
C. Input, Output, Process = 1 line in and 1 line out
D. Decision = 1 line in, always 2 lines out
5. Brief statements (pseudocode) inside symbols
A. Pseudocode is a short, description statement for the
computer to accomplish
B. Non-standard and subjective
Flowchart
Algorithm (pseudocode)
1. Enter www.facebook.com into your browser. (I/O)
2. facebook login page loads (PROCESS)
3. facebook login page is displayed (I/O)
4. User enters ID and password(I/O)
5. ID and Password are validated (DECISION)
a. If not valid
1) Determine error (PROCESS)
2) Display Login Page with error message (I/O)
b. If valid
1) Find User’s Home Page (PROCESS)
2) Display User’s Home Page (I/O)
Flowchart
Algorithm (pseudocode)
1. Enter www.facebook.com into your
browser. (I/O)
2. facebook login page loads
(PROCESS)
3. facebook login page is displayed
(I/O)
4. User enters ID and password(I/O)
5. ID and Password are validated
(DECISION)
a. If not valid
1) Determine error (PROCESS)
2) Display Login Page with error
message (I/O)
b. If valid
1) Find User’s Home Page (PROCESS)
2) Display User’s Home Page (I/O)
Problems

1.Change Fahrenheit to Celsius: (F − 32) ×


5/9 = C

2.Calculate pay given hours worked and


pay per hour.
1. Change Fahrenheit to Celsius: (F − 32) × 5/9 = C

CODING:
<script>
var fahrenheit, celcius;
fahrenheit = ????;
celcius = (fahrenheit - 32)* 5/9;
alert (celcius);
</script>
FtoC.html
2.Calculate pay given hours worked and pay per hour.

CODING:
<script>
var hours, payRate, totalPay;
hours = ????;
payrate = ???
totalPay = hours * payRate;
alert (totalPay);
</script>
pay1.html
2.Calculate pay given
hours worked and pay
per hour.
OVERTIME?????
Fundamental Concepts Expressed inJavaScript
(Chapter17)

CS170
Computer Applications for Business
JavaScript – Chapter17
• Programming Concepts • Expressions
• Names (including rules for • Conditionals
identifiers and case sensitivity), • Conditional Statements
values, and variables • Compound Statements
• Initialization and Declaration • An Expression and Its
• Data types: numbers, string Syntax
literals, Booleans (Three Basic • Keywords (reserved words
Data Types of JavaScript) – you do not have to
• Assignment Statements memorize all of them),
• Operators: Assignment, escape mechanisms
Arithmetic, Relational, Logical, • Embedding JS in HTML,
operator overload document.write(),
prompt() and alert()
Parts of HTMLCode/Webpage
<head>
<script>
// Place your JavaScript here
</script>
Script tags can go
</head>
in either the
<head>,
<body> <body>, or both
<script>
// Place your JavaScript here
</script>
</body>
Basic Syntax
• A program is simply a list of statements

• The statement terminator in JavaScript is


the semicolon ;

• Case sensitive

• Syntax vs. Convention


• “Rules of the Road”
• General agreement
A variable is:
Variables and Their Rules
• A a memory location that holds a value
• The name is separable from the value
Syntax
• Must begin with a letter, then any sequence of
letters, numerals, the underscore symbol (_), or
dollar sign ($)
• No spaces
Convention
• Be descriptive
• First letter is lowercase
• taxAmount
• camel case for more than 1 word
Variable Names = Identifiers
Tell whether the following names are legal or illegal? If
illegal, why?

first_name

Show me Camel Case Example with three word variable


Variable Names = Identifiers
Tell whether the following names are legal or illegal? If
illegal, why?

Legal
Illegal starts with a number.
Legal

Illegal contains a symbol


other than _ or $.
first_name Legal
Illegal space between
words.
Good Practice
Create Meaningful Identifiers for the following:

e f

area
Good Practice
Create Meaningful Identifiers for the following:

e height f width

area
area
Good Practice
Create Meaningful Identifiers for the following:

e blue_height f blue_width

area blue_area
Variable Declaration Statement
The var command declares/creates a variable
var age; age
var area;
var radius;
 Can declare (optional) multiple variables at one
time, separated with commas
var area, radius; area radius

 No values have been assigned


Three Basic Data Types ofJavaScript
There are three types of data in the
JavaScript programs used in this course:
1. Numbers
2. Strings
3. Booleans
Assigning a value to a variable-
variable must be declared first.
Syntax (must be in this order)
Variable Assignment Symbol (=) Value ;
var taxRate, balanceDue;
taxRate = 0.088;
balanceDue = 0;

Optionally, can declare and assign at the


same time, or create and assign multiple
var balanceDue = 0;
var taxRate = .088, balanceDue = 0;
Rules for WritingNumbers
• Numbers written in decimal form
• No “units”
• No punctuation

– Percentage is 0.33, not 33%;


– Currency is 10.89, not $10.89
– No commas 1000000 not 1,000,000
– or 1500.99 not 1,500.99
Rules for Writing Strings
1. Strings are for storing text
"You miss 100% of the shots you don’t take."
2. A string is always surrounded by single (')
or double (") quotes
3. If we want to include one within the other:
"Guide to B&B\'s"
4. Empty string is a string with zero
characters("")
5. Strings can be initialize at declaration
Arithmetic Operators
• Expressions are built of variables and
arithmetic operators
• Expressions are similar to algebraic
formulas
addition (+) subtraction(–)
multiplication (*) division (/ )
• Multiplication must be given explicitly with
the asterisk (*) multiply operator: a * b
• The modulus (mod) operation (%) divides
two integers and returns the remainder
• PEMDAS and Parentheses ( )

You might also like