LAB #3


Objectives


Tasks

  1. Type the following program exactly as shown:
    ! This program find the roots of equations of the 2nd degree
    ! with coefficients a, b and c
    program Equation
    implicit none
    real*4 a, b, c, delta
    real*4 root1, root2
    integer*1 count
    
    ! ----------------------------------------------Prompt and Input
    print*, "Solves ax^2 + bx + c = 0"
    print*, "Enter the coefficients a,b,c separated by commas..."
    read*, a, b, c
    
    ! ----------------------------------------------Validate
    
    ! ----------------------------------------------Find delta
    delta = b**2 - 4.* a * c
    
    ! ----------------------------------------------Determine Roots
    if (delta .GT. 0.) then
       root1 = (-b + sqrt(delta)) / (2. * a)
       root2 = (-b - sqrt(delta)) / (2. * a)
       count = 2
    else if (delta .LT. 0.) then
       count = 0
    else
       root1 = -b / (2. * a)
       count = 1
    end if
    
    ! ----------------------------------------------Show Results
    if (count .EQ. 2) then
       write(*,20) " Equation has two roots: ", root1, ", ", root2
    else if (count .EQ. 1) then
       write(*,20) " Equation has one root: ", root1
    else
       write(*,20) " Equation has no real roots!"
    end if
    20 format(1x, A, F10.4, A, F10.4)
    
    ! ----------------------------------------------Verify root1
    if (count .GT. 0) then
       print*, "Verifying the first root by computing the L.H.S.:"
    
    
    
    end if
    
    ! ----------------------------------------------
    end
    
  2. Compile the program as you learned in Lab1. If any compiler errors are reported, edit the program and fix them and repeat until you get a clean compilation.

Questions

  1. When run, the program prompts you to enter the three coefficients of an equation of the 2nd degree. To test the program, supply numbers for which you know the correct answer: The equation (x-1)(x-2)=0 obviously has two roots (1 and 2). If you multiply the left-hand-side (LHS) the coefficients to be entered are: 1, -3, 2.
  2. Re-run the program and test for the other two cases; i.e. when the equation has one (double) root or no (real) roots at all.
  3. Test now for non-integer roots; e.g. (x-0.5)(x-1.5)=0.
  4. We can make the program test itself by having it plug in the found root and evaluate the LHS (it should be zero). Complete the program by having it evaluate the LHS at x = root1. Re-run the previous four test cases and observe the printed value of the LHS.
  5. Supply the input: 1, -10, -100. The roots should be about 16 and -6. What is the printed value of the LHS? Why?
  6. Repeat the last question after changing real*4 to real*8 everywhere in the program. What do you observe?
  7. What if the input was: 0, -3, 2 ? Explain.
  8. Modify the program so as to guard against the case encountered in the last question. Insert some code under the "Validate" comment to insure that when a=0, the program prints an appropriate error message; e.g. "this is not an equation of the 2nd degree", and does not proceed with its computation. Note: You cannot use an END in the middle of the program, nor can you use a construct that we haven't covered yet.
  9. The preceding validation resulted in the program ending when a "bad" input was encountered. A more friendly approach involves outputting the root, if any, of the supplied 1st-degree equation. Modify your program so that it adopts this approach.

Fall2002/HR