Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.

Slides:



Advertisements
Similar presentations
Finding Complex Roots of Quadratics
Advertisements

You will learn about: Complex Numbers Operations with complex numbers Complex conjugates and division Complex solutions of quadratic equations Why: The.
Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.
Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes.
Chapter 3 Program Design And Branching Structures.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Mathematics for Computing Lecture 4: Algorithms and flowcharts Dr Andrew Purkiss-Trew Cancer Research UK
 Control structures  Algorithm & flowchart  If statements  While statements.
1.5 Quadratic Equations Start p 145 graph and model for #131 & discuss.
Chapter 4. Loops and Character Manipulation Loops in FORTRAN are constructs that permits us to execute a sequence of statements more than once. Type of.
Solving Quadratic Equations Algebraically Lesson 2.2.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Lesson 1-6 Solving Quadratic Equations. Objective:
Complex Number A number consisting of a real and imaginary part. Usually written in the following form (where a and b are real numbers): Example: Solve.
The Quadratic Formula..
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Notes Packet 10: Solving Quadratic Equations by the Quadratic Formula.
Solving Quadratic Equations Section 1.3
6.5 – Solving Equations with Quadratic Techniques.
Conditional Control Flow Constructs. Sequential Control Flow Execution order follows the textual order straight line flow Many simple problems can not.
Solving Quadratic Equations Using Completing the Square and the Quadratic Formula.
Goals: To solve quadratic equations by using the Quadratic Formula.
Solving Quadratic Equations
Using square roots to solve quadratic equations. 2x² = 8 22 x² = 4 The opposite of squaring a number is taking its square root √ 4= ± 2.
Solving Quadratic Equations. Solving by Factoring.
Chapter 10 Section 3 Solving Quadratic Equations by the Quadratic Formula.
Branches and Program Design
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
What you will learn How to solve a quadratic equation using the quadratic formula How to classify the solutions of a quadratic equation based on the.
MATLAB 及其工程应用 MATLAB and It’s Engineering Application 主讲教师: 胡章芳
The Quadratic Formula Students will be able to solve quadratic equations by using the quadratic formula.
Quadratic Functions (4) What is the discriminant What is the discriminant Using the discriminant Using the discriminant.
CHAPTER 4.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
Quadratic Equations. PROGRAM QuadraticEquations_1 IMPLICIT NONE REAL A, B, C, Discriminant, Root_1, Root_2 ! Get the coefficients PRINT *, "Enter the.
Solving Quadratic Formula using the discriminant.
Getting Started The objective is to be able to solve any quadratic equation by using the quadratic formula. Quadratic Equation - An equation in x that.
Chapter 4: Polynomial and Rational Functions. 4-2 Quadratic Equations For a quadratic equation in the form ax 2 + bx + c = 0 The quadratic Formula is.
PreCalculus Section 1.6 Solve quadratic equations by: a. Factoring b. Completing the square c. Quadratic formula d. Programmed calculator Any equation.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Solving Quadratic Equations by Using the Quadratic Formula (9-5) Objective: Solve quadratic equations by using the Quadratic Formula. Use the discriminant.
Math 20-1 Chapter 4 Quadratic Equations
3.4 Chapter 3 Quadratic Equations. x 2 = 49 Solve the following Quadratic equations: 2x 2 – 8 = 40.
Lesson 6.5: The Quadratic Formula and the Discriminant, pg. 313 Goals: To solve quadratic equations by using the Quadratic Formula. To use the discriminant.
SOLVE QUADRATIC EQUATIONS BY USING THE QUADRATIC FORMULA. USE THE DISCRIMINANT TO DETERMINE THE NUMBER AND TYPE OF ROOTS OF A QUADRATIC EQUATION. 5.6 The.
College Algebra B Unit 8 Seminar Kojis J. Brown Square Root Property Completing the Square Quadratic Equation Discriminant.
1.2 Quadratic Equations. Quadratic Equation A quadratic equation is an equation equivalent to one of the form ax² + bx + c = 0 where a, b, and c are real.
Section 2.5 – Quadratic Equations
Program design Program Design Process has 2 phases:
4.6 Quadratic formula.
Chapter 4 Quadratic Equations
Solving quadratics methods
Do Now – Take out your homework. Take You have 5 minutes.
Algorithms and Flowcharts
Complex integers? Here a and b are integers.
4.6 Quadratic formula.
Math 20-1 Chapter 4 Quadratic Equations
Worksheet Key 9 11/14/2018 8:58 PM Quadratic Formula.
Quadratic Equations by Dr. Terri
Quadratic Equations and Functions
Review: Simplify.
Loop Construct.
Chapter 3 Quadratic Equations
REPETITION Why Repetition?
Presentation transcript:

Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex. To avoid programming errors, a formal program design procedure is needed, which is based on the technique known as top-down design.

► 3.1 Top-down design techniques program to be solved design an algorithm (step-by-step procedure) for finding solution substak pseudocode Fortan statement unit test each program final progam pseudocode Fortan statement Fortan statement Fortan statement Fortan statement unit test each program

► 3.2 Pseudocode and Flowcharts  Stands forms to describe the algorithm  Pseudocode: hybrid mixture of English & Fortran  Flowchart: graphical description

► 3.3 Control Constructs: Branches  Block IF construct  Logical IF statement if (logical expression) then statement block end if logical expression statement block.false..true. if (logical expression) statement

 ELSE and ELSE IF clauses if (logical expression 1) then statement block 1 else if (logical expression 2) then statement block 2 else statement block n end if logical expression 1 statement block 1.false..true. logical expression 2.false..true. statement block 2

 Example using Block if: Solve ax 2 +bx+c=0 for x given a, b, and c.  Design the algorithm: ► Read the input data a, b, and c ► Calculate the roots ► Write out the roots

 Flowchart start read a, b, c echo a, b, c b**2-4.*a*c < 0b**2-4.*a*c = 0 write 'The equation has complex roots.' write 'The equation has two identical read roots.' write 'The equation has two distinct real roots.' calculate real_part, imag_part calcaulate x1calcaulate x1, x2 write real + i imag read – i imag write x1write x1, x2 stop.false..true.

progam roots ! Purpose: ! This program solves for the roots of a quadratic equation of the form ! a*x**2 + b*x + c = 0. It calculates the answers regardless of the type of ! roots that the equation possesses. implicit none ! Declare the variables used in this program real :: a ! Coefficient of x**2 term of equation real :: b ! Coefficient of x term of equation real :: c ! Constant term of equation real :: discriminant ! Discriminant of the equation real :: imag_part ! Imaginary part of equation (for complex roots) real :: real_part ! Real part of equation (for complex roots) real :: x1 ! First solution of equation (for real roots) real :: x2 ! First solution of equation (for real roots) ! Prompt the user for the coefficients of the equation write(*,*) 'This program solves for the roots of a quadratic ' write(*,*) 'equation of the form a*x**2 + b*x + c = 0. ' write(*,*) 'Enter the coefficients a, b, and c: ' read(*,*) a, b, c ! Echo back coefficients write(*,*) 'The coefficients a, b, and c are: ', a, b, c ! Calculate discriminant discriminant = b** *a*c >Continued on next page...

...< ! Solve for the roots, depending upon the value of the discriminant if (discriminant > 0.0) then x1 = (-b + sqrt(discriminant)) / (2.0*a) x2 = (-b - sqrt(discriminant)) / (2.0*a) write(*,*) 'This equation has two real roots:' write(*,*) 'x1 = ', x1 write(*,*) 'x2 = ', x2 else if (discriminant == 0.0) then x1 = (-b) / (2.0*a) write(*,*) 'This equation has two identical real roots:' write(*,*) 'x1 = x2 = ', x1 else ! there are complex roots, so... real_part = (-b) / (2.0*a) imag_part = sqrt(abs(discriminant)) / (2.0*a) write(*,*) 'This equation has complex roots:' write(*,*) 'x1 = ', real_part, ' +i ', imag_part write(*,*) 'x2 = ', real_part, ' -i ', imag_part end if end program

 "Name" Block IF Constructs [name:] if (logical expression 1) then statement block 1 else if (logical expression 2) then [name] statement block 2 else [name] statement block n end if [name]

 "Nested" Block IF Constructs  A better program is: if (test 1) then statement block if (test 2) then statement block if (test 3) then statement block end if outer: if (test 1) then statement block middle: if (test 2) then statement block inner: if (test 3) then statement block end if inner end if middle end if outer

 Example Using Block IF and Nested Block IF ► Block IF if (x > 95) then write(*,*) 'Grade is A' else if (x > 86) then write(*,*) 'Grade is B' else if (x > 76) then write(*,*) 'Grade is C' else if (x > 66) then write(*,*) 'Grade is D' else write(*,*) 'Grade is E' end if 95 < x A 86 < x ≤ 95 B 76 < x ≤ 86 C 66 < x ≤ 76 D 0 < x ≤ 66 0 < x ≤ 66E

► Nested Block IF aif: if (x > 95) then write(*,*) 'Grade is A' else aif bif: if (x > 86) then write(*,*) 'Grade is B' else bif cif: if (x > 76) then write(*,*) 'Grade is C' else cif dif: if (x > 66) then write(*,*) 'Grade is F' else dif write(*,*) 'Grade is F' end if dif end if cif end if bif end if aif

 CASE construct [name:] select case (integer case_expression) case (case_selector_1) [name] statement block case (case_selector_2) [name] statement block... case default [name] statement block end select [name]

► 3.4 Control Constructs: Loops ► while loops ► iterative loops (counting loops)  WHILE Loop do statement block if (logical_expression) exit statement block end do logical expression statement block.false..true. statement block do while (logical_expression) statement block end do logical expression statement block.false..true.

 Examle: Recall in a set of numbers and calculate the mean and standard deviation of the data x<0 n=n+1 sum_x=sum_x+x sum_x2=sum_x2+x.false..true. read x x_bar = sum_x/n std_dev = sqrt((n*sum_x2-sum_x**2) / (n*(n-1))

progam stats_2 ! Purpose: ! To calculate mean and standard deviation of input data set ! containing an arbitrary number of input values. ! implicit none integer :: n = 0 ! The number of input samples real :: std_dev ! The standard deviation of the input samples real :: sum_x = 0 ! The sum of the input values real :: sum_x2 = 0 ! The sum of the squares of the input values real :: x = 0 ! An input data value real :: x_bar ! The average of the input samples ! While loop to read input values do ! Read in next value write(*,*) 'Enter number:' read(*,*) x write(*,*) 'The number is ', x ! Test of loop exit if (x < 0) exit ! Otherwise, accumulate sums n = n + 1 sum_x = sum_x + x sum_x2 = sum_x2 + x**2 end do >Continued on next page...

...< ! Check to see if we have enough input data if (n < 2) then write(*,*) 'At least 2 values must be entered!' else x_bar = sum_x / real(n) std_dev = sqrt((real(n)*sum_x2-sum_x**2)/(real(n)*real(n-1)) write(*,*) 'The mean of this data set is:', x_bar write(*,*) 'The standard deviation is: ', std_dev write(*,*) 'The number of data points is:', n end if end program

 Iterative Loop (Counting loop) do index = istart, iend[, incr] statement block end do index*incr ≤ iend*incr statements.false..true. index = index+incr index=istart 1.istart, iend, incr can be contatns, variables or expression of integer. 2.If incr is not specified, incr=1.

progam stats_3 ! Purpose: ! To calculate mean and standard deviation of input data set, ! where each input value can be positive, negative or zero. ! implicit none integer :: i ! Loop index integer :: n = 0 ! The number of input samples real :: std_dev ! The standard deviation of the input samples real :: sum_x = 0 ! The sum of the input values real :: sum_x2 = 0 ! The sum of the squares of the input values real :: x = 0 ! An input data value real :: x_bar ! The average of the input samples ! Get the number of data to input write(*,*) 'Enter number of points: ' read(*,*) n ! Check to see if we have enough input data if (n < 2) then write(*,*) 'At least 2 values must be entered.' else ! Loop to read input values do i = 1, n write(*,*) 'Enter number: ' read(*,*) x write(*,*) 'The number is ', x sum_x = sum_x + x sum_x2 = sum_x2 + x**2 >Continued on next page...

...< ! Calculate statistics x_bar = sum_x / real(n) std_dev = sqrt((real(n)*sum_x2-sum_x**2)/(real(n)*real(n-1)) write(*,*) 'The mean of this data set is:', x_bar write(*,*) 'The standard deviation is: ', std_dev write(*,*) 'The number of data points is:', n end program

► Examples do i = 1, end do do i = 1, 10, 2... end do do i = 1, 10, end do do i = 3, -3, end do i = 1, 2, 3,..., 10 i = 1, 3, 5, 7, 9 Initially, 1*(-1) > 10*(-1) so exits i = 3, 1, -1, -3

 Notes: do i = 1, 4... i = 2 end do do i = 1, 5... if (i >= 3) exit... end do do i = 1, end do goto 100 will produce infinite loops branch out of a do loop is ok but, branch into the loop is illegal

► CYCLE statement ► EXIT statement ► Nesting loops (loop within loop) do i = 1, 5 if (i == 3) cycle write(*,*) i end do do i = 1, 5 if (i == 3) exit write(*,*) i end do do i = 1, 3 do j = 1, 4 write(*,*) i, j end do the output will be 1, 2, 4, 5 the output will be 1,