CSC 170 SESSION 5 NOTES Welcome Message Welcome to Session 5 . Course Web Site(s) Here is the link to the Course Web site ( CSC 170 ) . http://www.papademas.net/occ/csc170 Here is the link to the Online Course Web site ( CSC 171 / 172 / 173 ) . http://d2l.oakton.edu List of Topics for this Session’s Lecture Some Linux Commands Using WolframAlpha Web Page Design Hints and Tips Using JavaScript Algorithm Design Hints and Tips Writing Complete Computer Programs Review of Binary Numbers Review of Hexadecimal Numbers Review of Quiz 2 ( CSC 171 / 172 / 173 ) What about FTP? File Transfer Protocol A Review of Machine Problem 1 ( MP1 ) ( Root Searching by the Secant Method ) A Preview of Machine Problem 2 ( MP2 ) ( Root Searching by the Secant Method ) __________________________________________________________________ Some Linux Commands Linux is an operating system with command - driven capability. We use Linux to complete our computer laboratory assignments consisting of machine problems. 1 How would you move a file named lab2.f90 that is currently in the labs folder to the projects folder? The labs directory is in your home directory and the projects folder is within your labs directory. mv ~/labs/lab2.f90 ~/projects Using Mathematica For Homework 1 ( CAS 1 ) hints are provided within this first homework assignment. Web Page Design Hints and Tips Review of HTML Forms Placing a Fieldset About Form Data This example demonstrates how to draw a border with a caption around your data. <html> <body> <fieldset> <legend> Health information: </legend> <form action=""> Height <input type="text" size="3"> Weight <input type="text" size="3"> </form> </fieldset> <p> If there is no border around the input form, your browser is too old. </p> </body> </html> 2 Example Forms Sending an email via a form. <html> <body> <form action="MAILTO:bereans@aol.com" method="post" enctype="text/plain"> <h3>This form sends an e-mail.</h3> Name:<br> <input type="text" name="name" value="yourname" size="20"> <br> Mail:<br> <input type="text" name="mail" value="yourmail" size="20"> <br> Comment:<br> <input type="text" name="comment" value="yourcomment" size="40"> <br><br> <input type="submit" value="Send"> <input type="reset" value="Reset"> </form> </body> </html> 3 Example Forms Here is an example of using checkboxes in a form. <html> <body> <form name="input" action="html_form_action.asp" method="get"> I have a bike: <input type="checkbox" name="vehicle" value="Bike" checked="checked" /> <br /> I have a car: <input type="checkbox" name="vehicle" value="Car" /> <br /> I have an airplane: <input type="checkbox" name="vehicle" value="Airplane" /> <br /><br /> <input type="submit" value="Submit" /> </form> <p> If you click the "Submit" button, you send your input to a new page called html_form_action.asp. </p> </body> </html> 4 Using JavaScript Simple JavaScript ( JavaScript Functions ) Here is an example of calling a function via a button. <html> <head> <script type="text/javascript"> function myfunction() { alert("HELLO"); } </script> </head> <body> 5 <form> <input type="button" onclick="myfunction()" value="Call function"> </form> <p>By pressing the button, a function will be called. The function will alert a message.</p> </body> </html> Function Returning a Value Here is an example of a function returning a value. <html> <head> <script type="text/javascript"> function myFunction() { return ("Hello, have a nice day!"); } </script> </head> <body> <script type="text/javascript"> document.write(myFunction()) </script> 6 <p>The script in the body section calls a function.</p> <p>The function returns a text.</p> </body> </html> Algorithm Design Hints and Tips The Babylonian Algorithm The following algorithm, known root of a number x where x > step 1 Let y 1 . step 2 If x > 2 , let y step 3 Let y = ( y + as the Babylonian Algorithm, can be used to compute the square 0. = x / 2. x / y ) / 2 , which is the average of y and x / y . 2 If y is not close enough to x , then repeat step 3 . Return y . step 4 step 5 This algorithm illustrates the importance of an iterative process. An iterative process is one that continues until a specified condition is met. Here is an example of how the Babylonian Algorithm can be implemented to approximate the square root of the number 11 . Here, x = 11 is the square root of the number that we wish to approximate. Now let y = 1 be our initial approximation. Since x = 11 > 2 , we let y = x / 2 = 11 / 2 = 5.5 . Next compute ( y + x / y ) / 2 = ( 5.5 + 11 / 5.5 ) / 2 = 7.5 / 2 = 3.75 . This new value y = 3.75 represents our next approximation to the square root of 11 . Test to verify that 3.75 is close to the actual value of 11 by squaring 3.75 . 2 Since 3.75 = 14.0625 , which is not yet equal to or even close to 11 , the iterative process continues. Compute now ( y x / y ) / 2 = ( 3.75 + 11 / 3.75 ) / 2 = 7.5 / 2 = 3.3417 . 2 Since 3.3417 = 11.1670 , which is closer to 11 , we can be satisfied with 3.3417 as an approximation to the square root of 11 or we can perform another iteration, which actually yields 3.3167 . Note that 3.3167 2 = 11.0005 , which is very close to 11 . The Modulus Operation The modulus of two numbers m and n is the integer remainder when m is divided by n . In C++ the modulus operation is accomplished via the modulus operator m % n . In FORTRAN and Basic, the modulus operation is accomplished via a function Mod ( m , n ) . Uses of the modulus operation involve bin - packing and reminder analysis. Examples : 7 C++ / Java FORTRAN / Basic MS Excel 12 % 6 Mod (12, 6) =MOD(12,6) ( the above result is 0 , since 6 evenly divides 12 ) C++ / Java FORTRAN / Basic MS Excel 12 % 5 Mod (12, 5) =MOD(12,5) ( the above result is 2 , since 5 does not evenly divide 12 but leaves a remainder of 2 ) Writing Complete Computer Programs Here is a review of selection control structures from MP2 . ( FORTRAN version ) ! 4) increase count and test for failure 1, infinite loop count = count + 1 if (count > MAX_IT) then write(*,*) "Failed to converge with points" write(*,*) "(", a, ", ", ya, ") and (", b, ", ", yb, ")" exit endif ! 5) test for failure 2, non-existent slope if (abs(a-b) < 1e-5) then write(*,*) "Both x values converged to ", a, " with f(x) = ", ya exit endif ! 6) test for failure 3, horizontal slope if (abs(ya-yb) < 1e-5) then write(*,*) "Both y values converged to f(x) = ", ya, " with a = ", & a, " and b = ", b exit endif ! 7) calculate the next x value c = b – yb * (b - a)/(yb - ya) yc = 3.0 * c**2 + sin(c)- exp(c) ! write(*,*) "At count = ", count, " c = ", c 8 ! 8) test for successful search if (abs(yc) < 1e-5) then write(*,*) "Successful search" exit endif Review of Computer Logic ( NOT , AND , OR , XOR ) Computer logic involves conditions and operations. The typical logical operations include: Excel Function Return value NOT function Type = NOT ( logic_condition ) AND function = AND ( condition1 , condition2 ) OR function = OR ( condition1 , condition2 ) XOR function not available the opposite truth value of the logic condition false if one or both of the logic conditions are false, otherwise true true if one or both of the logic conditions are true, otherwise false true if exactly one of the logic conditions is true, otherwise false Programming Languages Type NOT function AND function OR function XOR function C++ / Java Logic Symbol FORTRAN Logic Symbol ! && || not available NOT AND OR not available What is XOR Logic? Introduction to XOR Encryption Consider meeting at a traffic intersection: when one traffic light is " green " the other traffic light must show " red " . XOR encryption works as a bitwise operation. XOR = Exclusive OR 1 = True 0 = False Truth Table Given Logic Conditions (Operands) P and Q P Q Result 1 1 1 1 0 0 0 1 0 0 0 0 For XOR logic, the output is true only if the inputs disagree. P XOR Q 0 1 1 0 9 I have an output of “true” or 1 only when my inputs disagree. Regular OR Operation Truth Table Given Logic Conditions (Operands) P and Q P Q Result 1 1 1 1 0 1 0 1 1 0 0 0 P OR Q 1 1 1 0 For Standard OR logic, the output is true except if both inputs disagree. Review of Hexadecimal Numbers What is 569 base ten as a hexadecimal number? Using FTP What about FTP? File Transfer Protocol To connect to your personal campus network drive: http://myfiles.oakton.edu To connect to the L:\ Samples drive: http://myfiles.oakton.edu Review of Quiz 2 ( CSC 171 / 172 / 173 ) Some stages of the software development process in producing the area of a right triangle. 1. Problem statement Define the area of a right triangle as A = ( 1 / 2 ) * base * height 2. Input / output description Input: the two legs of the right triangle Output: the area of the right triangle 10 3. Hand example To calculate the area of the right triangle ( 6 , 8 , 10 ) we would compute: A = (1/2)*(6)*(8) 4. Algorithm development (i) (ii) (iii) (iv) 5. prompt user for and input the base of the right triangle prompt for and input the height of the right triangle calculate the area display the area to the user Testing Given any positive values of the base and the height, calculate the area of the right triangle. Which of the following are examples of software that would be representative of application software? file utilities operating systems spreadsheets graphics user interface environments word processors data bases Root Searching Techniques ( An Example of Conditional Logic ) Root searching techniques involve locating the root or zeros of a function. Consider a polynomial function ( whose roots or solutions are x = − 3 , x = 2 and x = 5 / 2 ) : f(x ) =(x − 2)(2x − 5)(x + 3) or in an un - factored form: 11 f ( x ) = 2 x 3 - 3 x 2 - 17 x 30 which is of the format: f(x ) =Ax3 + Bx2 + Cx D a cubic equation has up to three zeros or roots ( refer to Descartes Rule of Signs ) We can use Mathematica to define the function ( note that an _ underscore character is used in the definition of the function ) . We can prove that a root exits at, for example, x 3 by typing the following Mathematica command. A value of zero will be returned. f[-3] You can repeat the above command for the other roots x 2 and x 5 / 2 . To graph this function with a typical graphing calculator, we can use: . Y= . . Y1 = 2 x ^ 3 - 3 x ^ 2 - 17 x 30 . . Zoom . . ZStandard . We can also use Mathematica to plot the function. 12 The above graph shows the zeros ( where the graph crosses the x - axis at x 3 , x 2 and x 5 / 2 . 13 We can also use MS Excel to plot the function. 14 Goal: How do we locate a root (zero) using a computer algorithm? Using a graphing calculator, we can view when y is zero using: . 2nd . . . Table . One algorithm we can use is called the root bi - section method, which uses a divide and conquer routine. How does this root bi - section method work? STEP 1 Choose a reasonable interval where a zero could be possibly located. For our example, try using the interval a = 0 to b = 3 . STEP 2 Evaluate the given function f ( x ) at both a = 0 and b = 3 . f ( 0 ) = 30 f(3)=6 STEP 3 evaluate function at x = 0 evaluate the function at x =3 Multiply f ( a ) and f ( b ) . If f ( a ) * f ( b ) 0 that means we possibly do not a root between these points. We say possibly since we may have a root between even though this product is positive. 15 If f ( a ) * f ( b ) 0 that means we do possibly a root between these points. In our example we have no root so far located. STEP 4 If f ( a ) * f ( b ) 0 , subdivide the interval [ a , b ] . ( 0 + 3 ) / 2 1.5 This gives us two zones to consider [ 0 , 1.5 ] and [ 1.5 , 3 ] . Let us consider both zones: [ 0 , 1.5 ] f ( 0 ) * f ( 1.5 ) 0 root not found [ 1.5 , 3 ] f ( 1.5 ) * f ( 3 ) > 0 root not found For the second case, check if the endpoints f ( 1.5 ) and f ( 3 ) - are they roots? ( 1.5 + 3 ) / 2 = 2.25 [ 1.5, 2.25 ] and [ 2.25 , 3 ] [ 1.5 , 2.25 ] f ( 1.5 ) * f ( 2.25 ) = 4.5 * -0.65625 < 0 root found [ 2.25 , 3 ] f ( 2.25 ) * f ( 3 ) = -0.65625 * 6 < 0 another root found 16 STEP 5 Continue the bi - section process until such time as the difference between successive approximations is negligible. __________________________________________________________________ How Do We Learn to Program The typical approach to learning a computer programming language is with the standard flow of information given below. -History of computing -Types of computers -Glossary Terms -Types of Programming Languages -Syntax Rules -Declaring Variables and Data Types -Expressions and Assignments -Sequential Control Structures -Selection Control Structures ( MP2 ) -Repetitive Control Structures -Functions -Arrays -file Processing -Vectors 17 Computer IF Statements ( Conditional Logic ) Computer IF statements are used when a decision has to be made. For example, a computer program involving the root bi - section method uses IF statements to determine whether or not a root has been found. A typical one - way IF statement has this pseudocode form : Remark: One - Way IF Statement IF (condition) THEN Remark: Statements to Execute if the condition is true END IF Examples of If Statements ( One Way ) C++ ( CSC 171 ) if ( x > 0 ) cout << " your number is positive "; FORTRAN ( CSC 172 ) if ( x > 0 ) then write (*,*) " your number is positive " end if Java ( CSC 173 ) if ( x > 0 ) System.out.println(" your number is positive "); A typical two - way IF statement has this pseudocode form : Remark: Two - Way IF Statement IF (condition) THEN Remark: Statements to Execute if the condition is true ELSE Remark: Statements to Execute if the condition is false END IF 18 Examples of If Statements ( Two Way ) C++ ( CSC 171 ) if ( x > 0 ) cout << " your number is positive "; else cout << " your number is negative or zero "; FORTRAN ( CSC 172 ) if ( x > 0 ) then write (*,*) " your number is positive " else write (*,*) " your number is negative or zero " end if Java ( CSC 173 ) if ( x > 0 ) System.out.println(" your number is positive "); else System.out.println(" your number is negative or zero "); A typical multi - path IF statement has this pseudocode form : Remark: Multi - Path IF Statement Block IF (condition 1) THEN Remark: Statements to Execute if the condition 1 is true ELSE IF (condition 2) Remark: Statements to Execute if the condition 2 is true ELSE Remark: Statements to Execute if both condition 1 and condition 2 are false END IF Examples of If Statements ( Multi - Path ) C++ ( CSC 171 ) if ( x > 0 ) cout << " your number is positive "; else if ( x < 0 ) cout << " your number is negative "; else cout << " your number is equal to 0 "; 19 FORTRAN ( CSC 172 ) if ( x > 0 ) then write (*,*) " your number is positive " else if ( x < 0 ) then write (*,*) " your number is negative " else write (*,*) " your number is equal to 0 " end if Java ( CSC 173 ) if ( x > 0 ) System.out.println(" your number is positive "); else if ( x < 0 ) then System.out.println(" your number is negative "); else System.out.println(" your number is equal to 0 "); Note: curly braces { } may be used in some computer languages to execute a block of code at a time once a condition is true. Using IF Blocks If statements are used to make a decision in mathematics especially when we are solving a system of linear equations and determining the character of the solution. We know that a system of linear equations can have: -one unique solution ( lines are intersecting at one point ) -no unique solution ( lines are parallel ) -an infinite number of solutions ( lines are dependent ) Computer If statement blocks are used also when we are solving a system of linear equations using matrix methods, such as the case of using Cramer’s Rule. Cramer’s Rule states that a system of two linear equations in two unknowns and having the form Ax + By=C Dx + Ey=F where A , B , C , D , E and F being real - valued coefficients and x and y being the variables, has a solution given by: x = (C×E − B×F)/(A×E − B×D) y = (A×F − C×D)/(A×E − B×D) 20 If statements help prevent a division by zero error when solving a system of linear equations using Cramer’s Rule. If the expression ( A × E − B × D ) is 0 then the system does not have a unique solution but it instead could have an infinite number of solutions or no solution. [ Example ] ( Cramer’s Rule for a 2 by 2 System of Linear Equations ) Use Cramer’s Rule to solve this system for x and y . 2x + 3y = 8 5x − y = 3 [ Solution ] This system has a unique solution x = 1 , y = 2 ) . Note that the expression ( A × E − B × D ) = ( 2 × ( − 1 ) − 3 × 5 ) = − 17 ≠ 0 and hence the system is determined to have a unique solution. [ Example ] ( Cramer’s Rule for a 2 by 2 System of Linear Equations ) Use Cramer’s Rule to solve this system for x and y . 5x + 3y = 3 10 x + 6 y = 5 [ Solution ] This system has no solution - the lines are parallel. Note that the expression ( A × E − B × D ) = ( 5 × 6 − 3 × 10 ) = 0 and hence the system is determined not to have a unique solution. To write a 2 by 2 Cramer’s Rule computer program we would have the following specifications. Our input is : A,B,C,D,E,F the real - valued coefficients Our test is : If ( A × E − B × D ) == 0 ) we test the Cramer’s Rule denominator 21 Our process is : x = (C×E−B×F)/(A×E − B×D) the x variable y = (A×F−C×D)/(A×E − B×D) the y variable Our output is : write " The x solution is " , x write " The y solution is " , y Cramer’s Rule C++ Version ( CSC 171 ) #include <iostream> using namespace std; int main() { //declare the variables int A = 0, B = 0, C = 0, D = 0, E = 0, F = 0; double Xnum = 0, Ynum = 0, Den = 0; //input the values cout << " please enter the coefficients " << endl; cin >> A >> B >> C >> D >> E >> F; //apply the formulas Xnum = (C * E) - ( B * F); Ynum = (A * F ) - ( C * D); Den = (A * E ) - ( B * D); //test the denominator and display the result if ( Den == 0) { cout << " there is no solution or there is an infinite\n "; cout << " number of solutions\n "; } else { cout << " a unique solution exists " << endl; cout << " x = " << Xnum / Den << endl; cout << " y = " << Ynum / Den << endl; } return 0; } 22 Cramer’s Rule FORTRAN Version ( CSC 172 ) program Cramer !declare the variables integer :: A = 0, B = 0, C = 0, D = 0, E = 0, F = 0 real :: Xnum, Ynum, Den !input the values write (*, *) " please enter the coefficients " read( *, *) A, B, C, D, E, F !apply the formulas Xnum = (C * E) - ( B * F) Ynum = (A * F ) - ( C * D) Den = (A * E ) - ( B * D) !test the denominator and display the result if ( Den == 0 ) then write (*, *) " there is no solution or an infinite number " write (*, *) " of solutions " else write (*, *) " a unique solution exists " write (*, *) " x = ", Xnum / Den write (*, *) " y = ", Ynum / Den endif end program Cramer 23 Cramer’s Rule Java Version ( CSC 173 ) import java.util.Scanner; class Cramer { public static void main( String args[] ) { //declare the variables Scanner sc = new Scanner(System.in); int A = 0, B = 0, C = 0, D = 0, E = 0, F = 0; double Xnum = 0, Ynum = 0, Den = 0; //input the values System.out.println( " please enter the coefficients " ); A = sc.nextInt(); B = sc.nextInt(); C = sc.nextInt(); D = sc.nextInt(); E = sc.nextInt(); F = sc.nextInt(); //apply the formulas Xnum = (C * E) - ( B * F); Ynum = (A * F ) - ( C * D); Den = (A * E ) - ( B * D); //test the denominator if ( Den == 0) { System.out.println( System.out.println( } else { System.out.println( System.out.println( System.out.println( } and display the result " there is no solution or there is " ); " an infinite number of solutions " ); " a unique solution exists " ); " x = " + Xnum / Den ); " y = " + Ynum / Den ); }//end main }//end class 24 Testing the Program Use Cramer’s Rule to solve this system for x and y . 2x + 3y = 8 5x − y = 3 Testing the Program Use Cramer’s Rule to solve this system for x and y . 5x + 3y = 3 10 x + 6 y = 5 25 __________________________________________________________________ A Preview of MP2 ( Root Searching by the Secant Method ) Sample run of the MP2 problem. 26