# 160348 Final Exam - Part 2 Writing Code .pdf - Program 1...

• 18

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 6 out of 18 pages.

Program 1Programming Exercises (6.5)Write the following function to display three numbers in increasing order:Function Headerdef displaySortedNumbers(num1, num2, num3)ParametersThe three parameters (num1,num2,num3) are numbers.Return ValueNoneWrite a test program that prompts the user to enter three numbers and invokes the functionto display them in increasing order.
Page2of19
Page3of19SolutionProgram_1.py123456789101112131415161718192021222324defmain():num1, num2, num3 =eval(input("Enter three integers: "))# Invoke the displaySortedNumbers method to display the# numbers in increasing orderdisplaySortedNumbers(num1, num2, num3)defdisplaySortedNumbers(num1, num2, num3):# Write the code to implement this methodifnum1 > num2:num1, num2 = num2, num1ifnum2 > num3:num2, num3 = num3, num2ifnum1 > num2:num1, num2 = num2, num1print("The sorted numbers are", num1, num2, num3)# Call the main functionmain()
Page4of19Program 2Programming Exercises (6.13)Write a function to compute the following series:࠵?(࠵?) =12+23+ ⋯ +࠵?࠵? + 1Function Headerdef m(i)ParametersThe parameter (i) is a number, and it represents the variable of the aboveequation.Return ValueThe result of the above equation.Note:This function does not print anything.Write a test program that displays the following table:im(i)10.500021.166731.9167...1815.45231916.40232017.3546
Page5of19SolutionProgram_2.py1234567891011131415defmain():print(format("i","<15s"),format("m(i)","<20s"))foriin range(1,20+1):print(format(i,"<15d"),format(m(i),"<20.4f"))defm(n):sum =0foriin range(1, n +1):sum += i / (i +1)returnsummain()
Page6of19Program 3Programming Exercises (6.17)Create the following two functions:Function Headerdef isValid(side1, side2, side3):ParametersThe three parameters (side1, side2, side3) are numbers.Return ValueIt returns True if the sum of any two sides is greater than the third side.Otherwise, it returns False.Note:This function does not print anything.Function Headerdef area(side1, side2, side3):ParametersThe three parameters (side1, side2, side3) are numbers.Return ValueIt returns the area of the triangle using the following formula:࠵?࠵?࠵?࠵? = √࠵? × (࠵? − ࠵?࠵?࠵?࠵?1) × (࠵? − ࠵?࠵?࠵?࠵?2) × (࠵? − ࠵?࠵?࠵?࠵?3)Where p is half the perimeter:࠵? =࠵?࠵?࠵?࠵?1 + ࠵?࠵?࠵?࠵?2 + ࠵?࠵?࠵?࠵?32Note:This function does not print anything.Write a test program that reads three sides for a triangle and computes the area if theinput is valid. Otherwise, it displays that the input is invalid:Enter three sides in double: 1, 3, 1<enter>Input is invalidEnter three sides in double: 1, 1, 1<enter>The area of the triangle is 0.4330127018922193

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 18 pages?

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Subroutine, Control flow, Fahrenheit, Carriage return