22
ISP 121 ISP 121 Algorithms Algorithms and and Computer Programming Computer Programming

ISP 121 Algorithmsand Computer Programming. Why Know Simple Programming? You can create powerful macros in Access / Excel / Word / ??? to manipulate

Embed Size (px)

Citation preview

ISP 121ISP 121

AlgorithmsAlgorithms

andand

Computer ProgrammingComputer Programming

Why Know Simple Programming?Why Know Simple Programming?

You can create powerful macros in Access You can create powerful macros in Access / Excel / Word / ??? to manipulate data/ Excel / Word / ??? to manipulate data

You can create interactive web pages, You can create interactive web pages, such as the DePaul QRC mapping toolsuch as the DePaul QRC mapping tool

You can write simple programs to analyze You can write simple programs to analyze data and write the next article exposing data and write the next article exposing cheaters in your school districtcheaters in your school district

What is an Algorithm?What is an Algorithm?

Computers require precise instructions to Computers require precise instructions to perform an operationperform an operation

The instructions cannot be ambiguousThe instructions cannot be ambiguous An algorithm is an ordered sequence of An algorithm is an ordered sequence of

instructions that is guaranteed to solve a instructions that is guaranteed to solve a specific problemspecific problem

What is an Algorithm?What is an Algorithm?

Algorithms consist of three basic types of Algorithms consist of three basic types of operations or instructions:operations or instructions: Sequential operations, e.g. Add 1 cup of Sequential operations, e.g. Add 1 cup of

butter; Subtract the amount of the check from butter; Subtract the amount of the check from the current account balance; Set the value of the current account balance; Set the value of x to 1x to 1

Conditional operations, e.g. If the mixture is Conditional operations, e.g. If the mixture is too dry, then add ½ cup water; If the current too dry, then add ½ cup water; If the current account balance < 0, then account overdrawnaccount balance < 0, then account overdrawn

What is an Algorithm?What is an Algorithm?

Algorithms consist of three basic types of Algorithms consist of three basic types of operations or instructions:operations or instructions: Iterative operations, e.g. Repeat the previous Iterative operations, e.g. Repeat the previous

two steps until the mixture has thickened; two steps until the mixture has thickened; Repeat the following five steps until there are Repeat the following five steps until there are no more checks to be processed; Repeat no more checks to be processed; Repeat steps 1,2 and 3 until the value of y is equal to steps 1,2 and 3 until the value of y is equal to +1+1

ExampleExample

Add two values:Add two values: 3254232542

+ 42892+ 42892

How would you describe this operation to someone who has never seen carry arithmetic?

Average Miles Per Gallon (1)Average Miles Per Gallon (1)

Step 1: Get values for gallons used, Step 1: Get values for gallons used, starting mileage, ending mileagestarting mileage, ending mileage

Step 2: Set the value of distance driven to Step 2: Set the value of distance driven to (ending mileage – starting mileage)(ending mileage – starting mileage)

Step 3: Set the value of average miles per Step 3: Set the value of average miles per gallon to (distanced driven / gallons used)gallon to (distanced driven / gallons used)

Step 4: Print the value of average miles Step 4: Print the value of average miles per gallonper gallon

Step 5: StopStep 5: Stop

Average Miles Per Gallon(2)Average Miles Per Gallon(2)

Step 1: Get values for gallons used, Step 1: Get values for gallons used, starting mileage, ending mileagestarting mileage, ending mileage

Step 2: Set the value of distance Step 2: Set the value of distance driven to (ending mileage – starting driven to (ending mileage – starting mileage)mileage)

Step 3: Set the value of average Step 3: Set the value of average miles per gallon to (distanced driven / miles per gallon to (distanced driven / gallons used)gallons used)

Step 4: Print the value of average Step 4: Print the value of average miles per gallon (more on next slide)miles per gallon (more on next slide)

Average Miles Per Gallon(2)Average Miles Per Gallon(2)

Step 5: If average miles per gallon is Step 5: If average miles per gallon is greater than 25.0 thengreater than 25.0 then Print the message “You are getting good gas Print the message “You are getting good gas

mileage.”mileage.”

ElseElse Print the message “You are NOT getting good Print the message “You are NOT getting good

gas mileage.”gas mileage.” Step 6: StopStep 6: Stop

Average Miles Per Gallon (3)Average Miles Per Gallon (3)

Step 1: Repeat step 2 to step 8 until Step 1: Repeat step 2 to step 8 until Response is NoResponse is No Step 2: Get values for gallons used, starting Step 2: Get values for gallons used, starting

mileage, ending mileagemileage, ending mileage Step 3: Set the value of distance driven to Step 3: Set the value of distance driven to

(ending mileage – starting mileage)(ending mileage – starting mileage) Step 4: Set the value of average miles per Step 4: Set the value of average miles per

gallon to (distanced driven / gallons used)gallon to (distanced driven / gallons used) Step 5: Print the value of average miles per Step 5: Print the value of average miles per

gallongallon

Average Miles Per Gallon (3)Average Miles Per Gallon (3)

Step 6: If average miles per gallon is greater Step 6: If average miles per gallon is greater than 25.0 thenthan 25.0 then• Print the message “You are getting good gas Print the message “You are getting good gas

mileage.”mileage.”

ElseElse• Print the message “You are NOT getting good gas Print the message “You are NOT getting good gas

mileage.”mileage.” Step 7: Print the message “Do you want to do Step 7: Print the message “Do you want to do

this again, Yes or No?”this again, Yes or No?” Step 8: Get a value for Response from the userStep 8: Get a value for Response from the user

Step 9: StopStep 9: Stop

HTMLHTML

Hyper-Text Markup LanguageHyper-Text Markup Language All web pages are made (to varying All web pages are made (to varying

degrees) from HTMLdegrees) from HTML Each HTML command tells the web Each HTML command tells the web

browser what to do next, such as start a browser what to do next, such as start a new paragraph, display an image, or insert new paragraph, display an image, or insert a linka link

<HTML> Begins every HTML document<HEAD> Begins the head section<TITLE>DePaul University</TITLE> Title that appears on browser title bar</HEAD> Ends the head section

<BODY> Begins the body sectionThis is the first line.<BR> Text followed by line Break<P>Start a new paragraph.</P> Begins a new paragraph<H1>First Heading</H1> Level 1 heading (biggest)<H2>A second level heading</H2> Level 2 heading (little smaller)

<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

JavaScriptJavaScript

JavaScript can be added to an HTML JavaScript can be added to an HTML document to provide dynamic featuresdocument to provide dynamic features

JavaScript can be added to the head or JavaScript can be added to the head or body of HTML code (but we will body of HTML code (but we will concentrate on examples in body), can concentrate on examples in body), can assign values to variables, can perform assign values to variables, can perform decision statements, and can perform loop decision statements, and can perform loop statementsstatements

Example - JavaScript in BodyExample - JavaScript in Body

<html><head></head>

<body>

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

</body></html>

Example - JavaScript in BodyExample - JavaScript in Body

<html><head></head>

<body>

<script type="text/javascript">alert("This message is written inside an Alert box")</script>

</body></html>

Example - Using a VariableExample - 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 IfExample - 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…ElseExample - 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 OperatorsLogical Operators

You can use the following logical You can use the following logical operators in IF and WHILE statementsoperators in IF and WHILE statements====

!=!=

>>

>=>=

<<

<=<=

Example - Using a While to CountExample - Using a While to Count<html><body><script type="text/javascript">var i = 0while (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 PromptExample - Using a While to Prompt<html><body><script type="text/javascript">var answeranswer = prompt(“Do you understand quantum physics?”)while (answer == “no”){document.write(“It is actually based on quantum things.")answer = prompt(“Now do you understand quantum physics?”)}document.write(“Congrats!”)</script>

</body></html>