Introduction to Programming (Lab Tutorial 8)

Document Sample
Introduction to Programming (Lab Tutorial 8) Powered By Docstoc
					            ENGI 1020
Introduction to Programming
       (Lab Tutorial 8)

  Memorial University of Newfoundland
           Dr. Lihong Zhang

                                    1
Lab Tutorial 8


         Some Mistakes in Assignment 7
           while (selection>=0 && selection <=3); //range of values
           do {
             ……
           }

             i) while (…) {                 ii) do {
                     …                            …
                }                             } while (…) ;




  ENGI 1020 – Introduction to Programming                     2
Lab Tutorial 8




           “break” and “continue”

            while (…) {                     while (…) {
                   …                               …
                   break;                          continue;
                   …                               …
            }                               }




  ENGI 1020 – Introduction to Programming                      3
Lab Tutorial 8




      cout << "X = " << thickness << "m" << endl;
      << endl << endl;
   ⇒ cout << "X = " << thickness << "m" << endl <<
      endl << endl;
      cout << "X = " << X < < " m " << endl;
   ⇒ cout << "X = " << X << " m " << endl;
      k = [x*H]/[A*(T2 - T1)];
   ⇒ k = (x*H)/(A*(T2 - T1));


  ENGI 1020 – Introduction to Programming       4
Lab Tutorial 8


                           Assignment-8
     Newton's method to find the roots of an equation f(x) = 0
     The procedure is to repeatedly compute a new estimate of the
     root, y, using a previous estimate of the root, x, and the
     following computation: y = x - f(x)/f '(x),
     where f '(x) is the derivative of f(x)
     The user should be prompted for an initial estimate of the
     root, tolerance, and maxIterations (in that order). The values
     entered should be echoed as output. To test your program you
     can use -1.5 as a first guess for the root; tolerance = 0.0001;
     and maxIterations = 10
     In the program the functions f(x) and f '(x) must be computed
     by separate value-returning functions
  ENGI 1020 – Introduction to Programming                     5
Lab Tutorial 8


                               Some Hints

        f(x) = x2e-x-2
        f ′(x) = 2xe-x - x2e-x
        i) y = x - f(x)/f '(x)
        ii) x = y
        In the case of f '(x) = 0, your program should
        print the (exact) warning message, "Method fails,
        division by zero." and stop.


  ENGI 1020 – Introduction to Programming             6
Lab Tutorial 8                              Example Format

  Please enter initial guess: -1.5
  Please enter tolerance: 0.0001
  Please enter maximum iterations: 10

  Using the following data
  Initial guess: -1.5
  Tolerance: 0.0001, and
  Maximum iterations: 10.

  Stopped after 5 iterations.
  The best estimate of root is -0.901201
  ENGI 1020 – Introduction to Programming         7