ISP 121 Algorithmsand 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 cheaters in your school district
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? Algorithms consist 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 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 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? Algorithms consist 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 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 Add two values: 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) Step 5: If average miles per gallon is greater than 25.0 then Print the message “You are getting good gas mileage.” Print the message “You are getting good gas mileage.”Else Print the message “You are NOT getting good gas mileage.” Print the message “You are NOT getting good gas mileage.” Step 6: Stop
Average Miles Per Gallon (3) Step 1: Repeat step 2 to step 8 until Response is No Step 2: Get values for gallons used, starting mileage, ending mileage 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 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 4: Set the value of average miles per gallon to (distanced driven / gallons used) Step 5: Print the value of average miles per gallon 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 Step 6: If average miles per gallon is greater than 25.0 then Print the message “You are getting good gas mileage.”Print the message “You are getting good gas mileage.”Else Print the message “You are NOT getting good gas mileage.”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 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 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 a link
Begins every HTML document Begins the head section DePaul University Title that appears on browser title bar Ends the head section Begins the body section This is the first line. Text followed by line Break Start a new paragraph. Begins a new paragraph First Heading Level 1 heading (biggest) A second level heading Level 2 heading (little smaller) Inserts a horizontal rule (straight line) This line is bold. Bold text This line is italicized Italicized text Insert an image here DePaul CS Page Link to another web page Close the body section Ends every HTML document
JavaScript JavaScript can be added to an HTML document to provide dynamic features JavaScript can be added to the head or 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 document.write("This message is written when the page loads")
Example - JavaScript in Body alert("This message is written inside an Alert box")
Example - Using a Variable var name name = prompt(“Please enter a name”) document.write(name)
Example - Using an If var d = new Date() var time = d.getHours() if (time < 12) { document.write(" Good morning ") } This example demonstrates the If statement. If the time on your browser is less than 12, you will get a "Good morning" greeting.
Example - Using an If…Else var d = new Date() var time = d.getHours() if (time < 12) { document.write(" Good morning ") } else { document.write(" Good afternoon ") } This example demonstrates the If...Else statement. 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.
Logical Operators You can use the following logical operators in IF and WHILE statements ==!=>>=<<=
Example - Using a While to Count var i = 0 while (i <= 5) { document.write("The number is " + i) document.write(" ") i++ } Explanation: i equal to 0. While i is less than, or equal to, 5, the loop will continue to run. i will increase by 1 each time the loop runs.
Example - Using a While to Prompt var answer answer = 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!”)