軟體實驗:實作以二分法開根號 虞台文. The Lab Problem Write a function to compute by bisection the square root of a positive integer n given by the caller. The function’s.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick How do Calculators Computer Square Roots? rWhen you enter 29 into your calculator and push the square.
Advertisements

Zabin Visram Room CS115 CS126 Searching
軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
RATIONAL AND IRRATIONAL NUMBERS
Welcome to Jeff’s baseball game! Here is how to play. You are asked a math question and you have to get it right or you will repeat the question until.
Perfect Square Roots & Approximating Non-Perfect Square Roots
Bisection Method (Midpoint Method for Equations).
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Lecture 20 Programming with float and double Need for understanding how accurate these variables are.
Root-Finding Algorithm Bisection method Suppose we want to solve the equation f(x) = 0. Given two points a and b such that f(a) and f(b) have opposite.
Branch and Bound Algorithm for Solving Integer Linear Programming
Introduction Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Rational or Irrational? Why?
Roots Rational Numbers Scientific Notation More with Exponents Laws of Exponents
Chapter 6 Finding the Roots of Equations
chap7 Chapter 7 Simple Data Types chap7 2 Objectives No programming language can predefine all the data types that a programmer may.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
Additional Problems.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
軟體實驗: C-Lab5 虞台文. Lab5 Problem Write a program which accepts a text file as input and outputs how many characters, lines, and words the file contains.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
Approximating a Square Root Approximate to the nearest integer. Example 2 The perfect square closest to, but less than, 51 is 49. The perfect square closest.
Even more problems.. Mean (average) I need a program that calculates the average of student test scores. I need a program that calculates the average.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Numerical Methods for Engineering MECN 3500
CS221 Algorithm Basics. What is an algorithm? An algorithm is a list of instructions that transform input information into a desired output. Each instruction.
EXAMPLE 3 Standardized Test Practice SOLUTION From Example 2, you know the interquartile range of the data is 0.9 inch. Find 1.5 times the interquartile.
Dividing Whole Numbers and Decimals. Do Now:  Take out your homework  Silently work on the following problems
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Numerical Methods Solution of Equation.
Problem Session Working in pairs of two, solve the following problem...
軟體實作與計算實驗 1  While-loop flow chart  Decimal to binary representations Lecture 6 While-Loop programming.
軟體實驗 Perfect Number Enumerating 虞台文. Perfect Numbers A perfect number is such that it is equal to the sum of its proper divisors, which are any divisors.
Branch and Bound Algorithms Present by Tina Yang Qianmei Feng.
Notes 2.1 and 2.2 LOOKING FOR SQUARES AND SQUARE ROOTS.
© T Madas. These days no one has the need to manually compute square roots. The algorithm which follows has been put to oblivion by the modern calculator,
1/29/ Bisection Method Computer Engineering Majors Authors: Autar Kaw, Jai Paul
Real Zeros of Polynomial Functions. Solve x 3 – 2x + 1 = 0. How? Can you factor this? Can you use the quadratic formula? Now what if I tell you that one.
A Different Solution  alternatively we can use the following algorithm: 1. if n == 0 done, otherwise I. print the string once II. print the string (n.
軟體實驗: C-Lab7 虞台文. Lab7 Issues Lab 7-1  On-Off bits counting Lab 7-2  ACSII File Verification.
Irrational Numbers (4.2). If the definition of a rational number is a number that can be written in the form m/n, where m and n are integers with n not.
The set of real numbers can be divided into two sets: RATIONAL NUMBERS IRRATIONAL NUMBERS and Numbers that can be written in the form a. b Numbers that.
160 as a product of its prime factors is 2 5 x 5 Use this information to show that 160 has 12 factors.
軟體實作與計算實驗 1  While loop  Positive integer square root  Decimal to binary transition Lecture 6 While Loop.
START HERE Startup: Place each value on the number line where it belongs.
,000 Word Problems Estimation Square Roots Math Vocabulary Miscellaneous ,000.
Square Roots and Irrational Numbers.
Perfect Squares & Estimating Square Roots
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Find an approximation to {image} Use a double Riemann sum with m = n = 2 and the sample point in the lower left corner to approximate the double integral,
Estimating Square Roots of Numbers
Square Roots and Irrational Numbers
REVIEW: Perfect Squares
Estimating Square Roots
Dr. Joe Anderson September 6, 2017
Searching: linear & binary
Terms used in Multiplication
SOLUTION OF NONLINEAR EQUATIONS
Notes Over 9.1 Finding Square Roots of Numbers
Square Roots and Irrational Numbers.
Estimating Square Roots
Objectives Evaluate expressions containing square roots.
0-2 Real Numbers.
Natural Numbers The first counting numbers Does NOT include zero
Lecture 4: Tree Search Strategies
Programming Language  C Control Flow
Solutions for Nonlinear Equations
Presentation transcript:

軟體實驗:實作以二分法開根號 虞台文

The Lab Problem Write a function to compute by bisection the square root of a positive integer n given by the caller. The function’s prototype is double sqrt(int n). The algorithm proceeds as follows: Start with n and 1 as the upper and lower bounds, and then find the midpoint of the two bounds. If the square of the midpoint is equal or very close to n, then returns the midpoint as the answer and the algorithm terminates. If the square of the midpoint is greater than n, then let the upper bound be the midpoint; otherwise let the lower bound be the midpoint. Repeat the process until the estimates of the square root of n is within of each other. Returns the last estimate as the answer and the algorithm terminates. You can also define some functions for help if they make your code clearer. Verify that your code is correct. (sqrt.c)

double sqrt(int n) lowboundupbound 1 n mid mid 2  n return mid mid 2 > n upbound = mid mid 2 < n lowbound = mid mid = (lowbound + upbound)/ 2.0 What is then going on?