1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Pie
3 years ago
7

Given a double variable named x that has been declared and given a value, let's use a binary search technique to assign an estim

ate of its square root to another double variable, root that has also been declared. Let's assume that x's value is greater than 1.0 -- that will simplify things a bit. Here's the general idea:
Since x>1, we know its square root must be between 1 and x itself. So declare two other variables of type double (a and b say) and initialize them to 1 and x respectively. So we know the square root must be between a and b. Our strategy is to change a and b and make them closer and closer to each other but alway make sure that the root we're looking for is between them. (Such a condition that must always hold is called an invariant.)

To do this we will have a loop that at each step finds the midpoint of a and b. It then squares this midpoint value and if the square of the midpoint is less than x we know that the root of x must be bigger than this midpoint: so we assign the midpoint to a (making a bigger and shrinking our a and b interval by half!)-- and we still can be sure that the root is between a and b. Of course if the midpoint's square is greater than x we do the opposite: we assign b the value of midpoint.

But when to stop the loop? In this exercise, just stop when the interval between a and b is less than 0.00001 and assign root the midpoint of a and b then.

We call this a binary search also because at each stage we cut the interval under consideration in half. Efficient as this method is, old Isaac Newton discovered an algorithm that is even more efficient and that's what the library function sqrt uses.

I have this:

double a=1, b=x;
double mid;

while(a-b>0.00001){
mid=(a+b)/2;
mid=mid*mid;
if (mid a=mid;
else if(mid>x)
b=mid;
else
root=mid;
}
Computers and Technology
1 answer:
lord [1]3 years ago
5 0

Answer:now

Explanation:

That’s tough

You might be interested in
10 points if right...
aivan3 [116]

email because with chat it might wake up the kids and wbsite doesnt make sense. neither does blog so with email you can give personal messages and pictures.

7 0
3 years ago
Read 2 more answers
Building a String Library
maw [93]
Code:

def myAppend( str, ch ):
# Return a new string that is like str but with
# character ch added at the end
return str + ch

def myCount( str, ch ):
# Return the number of times character ch appears
# in str.

# initiaalizing count with 0
count = 0

# iterating over every characters present in str
for character in str:
# incrementing count by 1 if character == ch
if character == ch:
count += 1

# returning count
return count


def myExtend( str1, str2 ):
# Return a new string that contains the elements of
# str1 followed by the elements of str2, in the same
# order they appear in str2.

# concatenating both strings and returning its result
return str1 + str2

def myMin( str ):
# Return the character in str with the lowest ASCII code.

# If str is empty, print "Empty string: no min value"
# and return None.
if str == "":
print("Empty string: no min value")
return None

# storing first character from str in char
char = str[0]

# iterating over every characters present in str
for character in str:
# if current character is lower than char then
# assigning char with current character
if character < char:
char = character
# returning char
return char


def myInsert( str, i, ch ):
# Return a new string like str except that ch has been
# inserted at the ith position. I.e., the string is now
# one character longer than before.

# Print "Invalid index" if
# i is greater than the length of str and return None.

if i > len(str):
print("Invalid index")
return None

# str[:i] gives substring starting from 0 and upto ith position
# str[i:] gives substring starting from i and till last position
# returning the concatenated result of all three
return str[:i]+ch+str[i:]

def myPop( str, i ):
# Return two results:
# 1. a new string that is like str but with the ith
# element removed;
# 2. the value that was removed.
# Print "Invalid index" if i is greater than or
# equal to len(str), and return str unchanged and None
if i >= len(str):
print("Invalid index")
return str, None

# finding new string without ith character
new_str = str[:i] + str[i+1:]

# returning new_str and popped character
return new_str, str[i]

def myFind( str, ch ):
# Return the index of the first (leftmost) occurrence of
# ch in str, if any. Return -1 if ch does not occur in str.

# finding length of the string
length = len(str)

# iterating over every characters present in str
for i in range(length):
# returning position i at which character was found
if str[i]==ch:
return i
# returning -1 otherwise
return -1


def myRFind( str, ch ):
# Return the index of the last (rightmost) occurrence of
# ch in str, if any. Return -1 if ch does not occur in str.

# finding length of the string
length = len(str)

# iterating over every characters present in str from right side
for i in range(length-1, 0, -1):
# returning position i at which character was found
if str[i]==ch:
return i
# returning -1 otherwise
return -1

def myRemove( str, ch ):
# Return a new string with the first occurrence of ch
# removed. If there is none, return str.

# returning str if ch is not present in str
if ch not in str:
return str

# finding position of first occurence of ch in str
pos = 0

for char in str:
# stopping loop if both character matches
if char == ch:
break
# incrementing pos by 1
pos += 1

# returning strig excluding first occurence of ch
return str[:pos] + str[pos+1:]

def myRemoveAll( str, ch ):
# Return a new string with all occurrences of ch.
# removed. If there are none, return str.

# creating an empty string
string = ""

# iterating over each and every character of str
for char in str:
# if char is not matching with ch then adding it to string
if char!=ch:
string += char
# returning string
return string

def myReverse( str ):
# Return a new string like str but with the characters
# in the reverse order.

return str[::-1]
6 0
3 years ago
An intruder can wreak havoc on a network by simply plugging in an infected USB flash drive.
Bas_tet [7]

Answer:

A. True

Explanation:

1. I took the test

2. Flash drive ports are connected to the rest of the computer, so putting in an infected flash drive into the port will cause the bug to spread throughout the whole device.

3 0
3 years ago
Imagine cashing or taking money using the Automated Teller Machine (ATM) and the banking hall, which of these two ways would you
Brut [27]

Imagine cashing or taking money using the Automated Teller Machine (ATM) and the banking hall, the two ways are When it involves convenience, not anything can beat ATMs.

<h3>What are the benefits and drawbacks of the usage of an ATM card?</h3>

No credit score allowed: A debit card is related for your financial institution account. There isn't anyt any opportunity of creating any transaction on credit score. All transactions and withdrawals are restricted to the stability to be had for your account. Difficult to dispute fraudulent use: It is less difficult to fraudulently use your debit card.

Automated Teller Machine (ATM) – It is a digital system that is operated with the aid of using clients and lets in acting for monetary transactions consisting of switch funds, coins withdrawals, and deposits. It extensively utilized for acquiring account facts at any time and with out the assist of financial institution staff.

Read more about the Automated Teller Machine:

brainly.com/question/19185661

#SPJ1

3 0
2 years ago
Mail is an important method of __________ between a company and it outside world.
Ipatiy [6.2K]

I would guess communication, but I might be wrong.

5 0
3 years ago
Read 2 more answers
Other questions:
  • what font size should generally be used for the slide title to ensure that it is set off from the content
    6·1 answer
  • A four-year old laptop will not boot and presents error messages on screen. You have verified with the laptop technical support
    11·1 answer
  • convert the following c code to mips. assume the address of base array is associated with $s0, n is associated with $s1, positio
    14·1 answer
  • The _ are the devices that allow a computer system to communicate and interact with the outside world as well as store informati
    11·1 answer
  • What is the purpose of the Excel Function Reference?
    9·1 answer
  • Choose the term that best completes each sentence.
    6·2 answers
  • What is a focus of Accenture's point of view on blockchain in the marketplace?
    12·1 answer
  • Moore's law benefits
    12·1 answer
  • A program with a graphical user inter-face for displaying HTML files, used to navigate the World Wide Web.
    8·1 answer
  • 1. Name the three kinds of Eraser in Photoshop. <br>​
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!