# Algorithms

Document Sample

```					    ISP 121
Algorithms
and
Computer Programming
Why Know Simple Programming?
 You  can create powerful macros in Access
/ Excel / Word / ??? to manipulate data
 You can create interactive web pages,
such as the DePaul QRC mapping tool
 You can write simple programs to analyze
data and write the next article exposing
What is an Algorithm?
 Computers   require precise instructions to
perform an operation
 The instructions cannot be ambiguous
 An algorithm is an ordered sequence of
instructions that is guaranteed to solve a
specific problem
What is an Algorithm?
 Algorithmsconsist of three basic types of
operations or instructions:
   Sequential operations, e.g. Add 1 cup of
butter; Subtract the amount of the check from
the current account balance; Set the value of
x to 1
   Conditional operations, e.g. If the mixture is
too dry, then add ½ cup water; If the current
account balance < 0, then account overdrawn
What is an Algorithm?
 Algorithmsconsist of three basic types of
operations or instructions:
   Iterative operations, e.g. Repeat the previous
two steps until the mixture has thickened;
Repeat the following five steps until there are
no more checks to be processed; Repeat
steps 1,2 and 3 until the value of y is equal to
+1
Example
32542
+ 42892

How would you describe this operation to someone who has never seen
carry arithmetic?
Average Miles Per Gallon (1)
 Step  1: Get values for gallons used,
starting mileage, ending mileage
 Step 2: Set the value of distance driven to
(ending mileage – starting mileage)
 Step 3: Set the value of average miles per
gallon to (distanced driven / gallons used)
 Step 4: Print the value of average miles
per gallon
 Step 5: Stop
Average Miles Per Gallon(2)
 Step  1: Get values for gallons used,
starting mileage, ending mileage
 Step 2: Set the value of distance
driven to (ending mileage – starting
mileage)
 Step 3: Set the value of average
miles per gallon to (distanced driven /
gallons used)
 Step 4: Print the value of average
miles per gallon (more on next slide)
Average Miles Per Gallon(2)
 Step5: If average miles per gallon is
greater than 25.0 then
 Print the message “You are getting good gas
mileage.”
Else
 Print the message “You are NOT getting good

gas mileage.”
 Step   6: Stop
Average Miles Per Gallon (3)
1: Repeat step 2 to step 8 until
 Step
Response is No
   Step 2: Get values for gallons used, starting
mileage, ending mileage
   Step 3: Set the value of distance driven to
(ending mileage – starting mileage)
   Step 4: Set the value of average miles per
gallon to (distanced driven / gallons used)
   Step 5: Print the value of average miles per
gallon
Average Miles Per Gallon (3)
   Step 6: If average miles per gallon is greater
than 25.0 then
• Print the message “You are getting good gas
mileage.”
Else
• Print the message “You are NOT getting good gas
mileage.”
   Step 7: Print the message “Do you want to do
this again, Yes or No?”
   Step 8: Get a value for Response from the
user
 Step   9: Stop
HTML
 Hyper-Text  Markup Language
 All web pages are made (to varying
degrees) from HTML
 Each HTML command tells the web
browser what to do next, such as start a
new paragraph, display an image, or insert
<HTML>                               Begins every HTML document
<TITLE>DePaul University
</TITLE>                             Title that appears on browser title bar

<BODY>                               Begins the body section
This is the first line.<BR>          Text followed by line Break
<P>Start a new paragraph.</P>        Begins a new paragraph

<HR>                                 Inserts a horizontal rule (straight line)
<B>This line is bold.</B><BR>        Bold text
<I>This line is italicized</I><BR>   Italicized text
<IMG SRC=“\images\banner.gif”>       Insert an image here

<A HREF=“http://www.cs.depaul.edu”>
DePaul CS Page</A>             Link to another web page

</BODY>                              Close the body section
</HTML>                              Ends every HTML document
JavaScript
 JavaScript can be added to an HTML
document to provide dynamic features
body of HTML code (but we will
concentrate on examples in body), can
assign values to variables, can perform
decision statements, and can perform loop
statements
Example - JavaScript in Body
<html>

<body>

<script type="text/javascript">
document.write("This message is written when the page loads")
</script>

</body>
</html>
Example - JavaScript in Body
<html>

<body>

<script type="text/javascript">
</script>

</body>
</html>
Example - Using a Variable
<html>
<body>

<script type="text/javascript">
var name

name = prompt(“Please enter a name”)
document.write(name)

</script>
</body>
</html>
Example - Using an If
<html>
<body>

<script type="text/javascript">
var d = new Date()
var time = d.getHours()

if (time < 12)
{
document.write("<b>Good morning</b>")
}
</script>

<p>This example demonstrates the If statement.</p>

<p>If the time on your browser is less than 12,
you will get a "Good morning" greeting.</p>
</body>
</html>
Example - Using an If…Else
<html>
<body>
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time < 12)
{
document.write("<b>Good morning</b>")
}
else
{
document.write("<b>Good afternoon</b>")
}
</script>
<p>This example demonstrates the If...Else statement.</p>
<p>If the time on your browser is less than 12,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.</p>
</body>
</html>
Logical Operators
 Youcan use the following logical
operators in IF and WHILE statements
==
!=
>
>=
<
<=
Example - Using a While to Count
<html>
<body>
<script type="text/javascript">
var i = 0
while (i <= 5)
{
document.write("The number is " + i)
document.write("<br>")
i++
}
</script>
<p>Explanation:</p>
<p><b>i</b> equal to 0.</p>
<p>While <b>i</b> is less than , or equal to, 5, the loop will continue to run.</p>
<p><b>i</b> will increase by 1 each time the loop runs.</p>

</body>
</html>
Example - Using a While to Prompt
<html>
<body>
<script type="text/javascript">
answer = prompt(“Do you understand quantum physics?”)
{
document.write(“It is actually based on quantum things.")
answer = prompt(“Now do you understand quantum physics?”)
}
document.write(“Congrats!”)
</script>

</body>
</html>

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 1 posted: 6/3/2012 language: pages: 22