U5 Programming Assignment

py

School

University of the People *

*We aren’t endorsed by this school

Course

CS1102

Subject

Computer Science

Date

Jan 9, 2024

Type

py

Pages

1

Uploaded by HighnessStarlingMaster967

Report
#Part 1 #Encapsulate the following Python code from Section 7.5 in a function named my_sqrt #that takes a as a parameter, chooses a starting value #for x, and returns an estimate of the square root of a. def my_sqrt(a): x = 5.0 while True: y = (x + a/x)/2.0 if y == x: break x = y return x #Part 2 #Write a function named test_sqrt that prints a table like the following using a while #loop, where "diff" is the absolute value of the difference between my_sqrt(a) and math.sqrt(a). import math def test_sqrt(): a = 1 while a <= 25: print('a = ' + str(a) + ' | ' + 'my_sqrt(a) = ' + str(my_sqrt(a)) + ' | ' + 'math.sqrt(a) = ' + str(math.sqrt(a)) + ' | ' + 'diff = ' + str(abs(my_sqrt(a) - math.sqrt(a)))) a += 1 test_sqrt()
Discover more documents: Sign up today!
Unlock a world of knowledge! Explore tailored content for a richer learning experience. Here's what you'll get:
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help