Program 5 Quadratic Equation with File I/O and Formatted Output You are to modify your Program...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Program 5 Quadratic Equation with File I/O and Formatted Output You are to modify your Program 2 and/or Program 3 to do/include the following: Read the coefficients of the quadratic, 3 coefficients per line (the coefficients will be separated by at least one space, possibly more), from an input file whose name is specified by the user. You should check for existence of the input file and terminate with an error message if the input file cannot be opened. Your program should continue processing sets of coefficients until the end of the input file is reached. Your program should output, to an output file whose name is specified by the user, 1 line per set of coefficients processed. Your output lines should be formatted EXACTLY as those shown on "Attachment A". Your program should make sure that the output file was successfully opened before attempting to process any data. Your program should count how many sets of coefficients it processes and at the end of your program, output that value to the user. Your script file should look similar to the script file shown in "Attachment B", of course including a "cat" of your program at the beginning. Run your program for the two data files as shown in "Attachment B". Include any other test cases which you think are needed. You may find the following UNIX commands helpful in completing this assignment: whoami pwd diff ATTACHMENT A 1.000: Two solutions for x: 2.000: One solution for x: 2.200: Two solutions for x: 5.000, c= -3.000: Two solutions for x: For a= 2.000, b= 5.000, c= and -2.281 For a= 2.000, b= 4.000, c= For a= 5.500, b= 8.000, c= and -1.086 For a= -2.000, b= and 1.500 -0.219 -1.000 -0.368 1.000 For a= 1.000, b= 2.000, c= 3.000: Can not solve, b^2 - 4ac negative. For a= 0.000, b= 5.000, c= 10.000: Not a quadratic equation if a = 0 0.000 For a= 5.000, b= 10.000, c= 0.000: Two solutions for x: and -2.000 For a= 10.000, b= 0.000, c= 5.000: Can not solve, b^2 negative. ERROR: Input must have 3 coefficients, line read had 2 For a= 6.457, b= 7.735, c=100.787: Can not solve, b^2 - 4ac negative. 4ac ATTACHMENT B wbeeche001@grace$ pgm5.pl Input the name of the file which has the coefficients of the quadratic equation (a, b, and c), one set per line: /home/wbeech0001/LCC/cit145/data/pgm5.short.input Input the name of the output file you wish to create: pgm5.short.output This program is being executed by wbeeche001 from /home/wbeeche001/LCC/cis145/working/pgm5 Its input file is /home/wbeeche001/LCC/cit145/data/pgm5.short.input Its output file is pgm5.short.output 10 equations processed wbeeche001@grace$ diff pgm5.short.output /home/wbeeche001/LCC/cis144/data/pgm5.short.output wbeeche001@grace$ pgm5.pl Input the name of the file which has the coefficients of the quadratic equation (a, b, and c), one set per line: /home/wbeech0001/LCC/cit144/data/pgm5.long.input Input the name of the output file you wish to create: pgm5.long.output This program is being executed by wbeech0001 from /home/wbeeche001/LCC/cis145/working/pgm5 Its input file is /home/wbeeche001/LCC/cit144/data/pgm5.long.input Its output file is pgm5.long.output 200 equations processed wbeech0001@grace$ 1 import math 2 3 print("Equa. Form: ax**2 + bx + c = 0") 4 5 # Read the coefficients of the quadratic equation from the user on one line, separated by commas 6 coefficients = input("Enter the coefficients a, b, and c, separated by commas: ") 7 a, b, c = coefficients.split(",") 8 float(a) 9 10 11 12 13 14 15 16 17 9 9 8 12 | 18 19 20 22 23 24 25 26 27 a = b float (b) C = float (c) 29 30 31 32 # Check if a is zero if a == 0: print("The coefficient a cannot be zero.") exit() # Check if the discriminant is negative d = (b**2) (4* a * c) if d < 0: print("The program cannot take the square root of a negative number.") exit() # Calculate the roots of the quadratic equation if d == 0: # If the discriminant is zero, then there is one repeated root root = (-b) / (2* a) print (f"The root is {root}.") 28 else: # If the discriminant is positive, then there are two real roots root1 = (-b math.sqrt(d)) / (2 * a) root2 (-b + math.sqrt(d)) / (2 * a) print (f"The roots are {root1} and {root2}.") = Program 5 Quadratic Equation with File I/O and Formatted Output You are to modify your Program 2 and/or Program 3 to do/include the following: Read the coefficients of the quadratic, 3 coefficients per line (the coefficients will be separated by at least one space, possibly more), from an input file whose name is specified by the user. You should check for existence of the input file and terminate with an error message if the input file cannot be opened. Your program should continue processing sets of coefficients until the end of the input file is reached. Your program should output, to an output file whose name is specified by the user, 1 line per set of coefficients processed. Your output lines should be formatted EXACTLY as those shown on "Attachment A". Your program should make sure that the output file was successfully opened before attempting to process any data. Your program should count how many sets of coefficients it processes and at the end of your program, output that value to the user. Your script file should look similar to the script file shown in "Attachment B", of course including a "cat" of your program at the beginning. Run your program for the two data files as shown in "Attachment B". Include any other test cases which you think are needed. You may find the following UNIX commands helpful in completing this assignment: whoami pwd diff ATTACHMENT A 1.000: Two solutions for x: 2.000: One solution for x: 2.200: Two solutions for x: 5.000, c= -3.000: Two solutions for x: For a= 2.000, b= 5.000, c= and -2.281 For a= 2.000, b= 4.000, c= For a= 5.500, b= 8.000, c= and -1.086 For a= -2.000, b= and 1.500 -0.219 -1.000 -0.368 1.000 For a= 1.000, b= 2.000, c= 3.000: Can not solve, b^2 - 4ac negative. For a= 0.000, b= 5.000, c= 10.000: Not a quadratic equation if a = 0 0.000 For a= 5.000, b= 10.000, c= 0.000: Two solutions for x: and -2.000 For a= 10.000, b= 0.000, c= 5.000: Can not solve, b^2 negative. ERROR: Input must have 3 coefficients, line read had 2 For a= 6.457, b= 7.735, c=100.787: Can not solve, b^2 - 4ac negative. 4ac ATTACHMENT B wbeeche001@grace$ pgm5.pl Input the name of the file which has the coefficients of the quadratic equation (a, b, and c), one set per line: /home/wbeech0001/LCC/cit145/data/pgm5.short.input Input the name of the output file you wish to create: pgm5.short.output This program is being executed by wbeeche001 from /home/wbeeche001/LCC/cis145/working/pgm5 Its input file is /home/wbeeche001/LCC/cit145/data/pgm5.short.input Its output file is pgm5.short.output 10 equations processed wbeeche001@grace$ diff pgm5.short.output /home/wbeeche001/LCC/cis144/data/pgm5.short.output wbeeche001@grace$ pgm5.pl Input the name of the file which has the coefficients of the quadratic equation (a, b, and c), one set per line: /home/wbeech0001/LCC/cit144/data/pgm5.long.input Input the name of the output file you wish to create: pgm5.long.output This program is being executed by wbeech0001 from /home/wbeeche001/LCC/cis145/working/pgm5 Its input file is /home/wbeeche001/LCC/cit144/data/pgm5.long.input Its output file is pgm5.long.output 200 equations processed wbeech0001@grace$ 1 import math 2 3 print("Equa. Form: ax**2 + bx + c = 0") 4 5 # Read the coefficients of the quadratic equation from the user on one line, separated by commas 6 coefficients = input("Enter the coefficients a, b, and c, separated by commas: ") 7 a, b, c = coefficients.split(",") 8 float(a) 9 10 11 12 13 14 15 16 17 9 9 8 12 | 18 19 20 22 23 24 25 26 27 a = b float (b) C = float (c) 29 30 31 32 # Check if a is zero if a == 0: print("The coefficient a cannot be zero.") exit() # Check if the discriminant is negative d = (b**2) (4* a * c) if d < 0: print("The program cannot take the square root of a negative number.") exit() # Calculate the roots of the quadratic equation if d == 0: # If the discriminant is zero, then there is one repeated root root = (-b) / (2* a) print (f"The root is {root}.") 28 else: # If the discriminant is positive, then there are two real roots root1 = (-b math.sqrt(d)) / (2 * a) root2 (-b + math.sqrt(d)) / (2 * a) print (f"The roots are {root1} and {root2}.") =
Expert Answer:
Answer rating: 100% (QA)
To accomplish this task youll need to do the following steps Read coefficients from an input file sp... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
How do sociological theories of deviance and social control elucidate the construction of criminality, the enforcement of laws, and the stigmatization of marginalized groups within society ?
-
Construct the current assets section of the balance sheet shown here from the following data: Yearly Credit Sales b. Inventory Turnover c. Current Liabilities d. Current Ratio e. c. Quick Ratio...
-
You are required to write a Python program that will manage character (heroes and villain) information. Character (hero and villain) information will be stored in a text file that will be read in...
-
The voltage held by a voltage regulator follows a normal random variable with a mean that equals 200 volts and a standard deviation that equals 5 volts. A regulator meets the specifications if the...
-
The power output of the Sun is 3.77 x 1026 W. How much mass is converted to energy in the Sun each second?
-
The average distance between Mars and Earth is about 1.3 108 miles. How long would it take TV pictures transmitted from the Viking space vehicle on Mars' surface to reach Earth? (1 mile = 1.61 km.)
-
Because both radial readings on the top of a vertical motor are horizontal, how can you differentiate them?
-
A large piece of equipment acquired on 1 January 20X5 by Kapadia Company has four major components for depreciation. Details regarding each component are given in the schedule below: Required: 1....
-
What was the existing capital structure for Apple inc? Do you believe it was optimum? Use Hamada's equation to determine the optimum. Should Apple take on more debt, repurchase stock, or have a...
-
The beam shown in Figure P567 carries a repeated, reversed load of 400 N applied at section C. Compute the resulting design factor if the beam is made from SAE 1340 OQT 1300. Data in Figure P567. 12...
-
A bullet is fired through a wooden board with a thickness of 8.0cm. The bullet hits the board perpendicular to it, and with avelocity of +350 m/s. The bullet then emerges on the other side ofthe...
-
Search and locate a current international issue (an issue that affects more than one culture). 1. Describe the issue 2. Define and discuss the two or more cultures involved in the issue 3. Explain...
-
As the Chief of Police of a department of 100 police officers, your city manager has asked you to develop a policy for dealing with alcohol abuse.Would your policy emphasize discipline or...
-
Describe the advantages to an organization of having employees who are fluent in using both rapport talk and report talk. Use specific examples to show when and how each style benefits the...
-
In which stage of the developmental model might using the internet be especially beneficial for shy people? A) initiating B) intensifying C) Experimenting D) integrating E) bonding
-
Determine the logging and auditing deficiencies of the current environment. Then, create official policies for the following: Logging (Think about what you want to capture and whether it is...
-
- A particle of mass I initially at rest decays into 2 pices each of mass m. What is the speed of each piece as if flice off ?
-
1. Which of the four major types of information systems do you think is the most valuable to an organization? 2. How do you critically associate the ideas of business agility and business efficiency...
-
Patty Bayan is a single taxpayer living at 543 Space Drive, Houston, TX 77099. Her Social Security number is 466-33-1234. For 2012, Patty has no dependents, and her W-2 from her job at a local...
-
While preparing Massie Miller's 2012 Schedule A, you review the following list of possible charitable deductions provided by Massie: Cash contribution to a family whose house burned...
-
Kathy Kennedy (age 44) is a single taxpayer and she lives at 212 North Pine Way, Payson, AZ 85541. Her Social Security number is 467-98-9784. Kathy's earnings and income tax withholding as the...
-
Palm Ltd acquired a business that used a large number of assets that worked in combination to produce a product saleable in offshore markets. The assets of the business include a computer program...
-
Cellar Ltd is in the business of bottling wine, particularly for small wineries that cannot afford sophisticated technical equipment and prefer to concentrate on the growing of the grapes. The white...
-
A response to the IASB's Exposure Draft 2009/5 Fair Value Measurements, stated: In practical terms we doubt that an asset measured on any other basis than its intended use will provide more useful...
Study smarter with the SolutionInn App