Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program 5 Quadratic Equation with File I / O and Formatted Output You are to modify your Program 3 to do / include the following:

Program 5
Quadratic Equation with File I/O and Formatted Output
You are to modify your 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 the 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 shown in Attachment A. Your program should ensure the output file was successfully opened before attempting to process any data.
Your program should count how many sets of coefficients it processes and output that value to the user at the end of your program.
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
For a=2.000, b=5.000, c=1.000: Two solutions for x: -0.219 and -2.281
For a=2.000, b=4.000, c=2.000: One solution for x: -1.000 For a=5.500, b=8.000, c=2.200: Two solutions for x: -0.368 and -1.086
For a=-2.000, b=5.000, c=-3.000: Two solutions for x: 1.000 and 1.500
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
For a=5.000, b=10.000, c=0.000: Two solutions for x: 0.000 and -2.000
For a=10.000, b=0.000, c=5.000: Can not solve, b^2-4ac 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.
ATTACHMENT B
user01@grace$ pgm5.py
Input the name of the file which has the coefficients of the quadratic equation (a, b, and c), one set per line:
/file~path/pgm5.short.input
Input the name of the output file you wish to create:
pgm5.short.output
This program is being executed by user01 from /home/user01/temp
Its input file is /file~path/pgm5.short.input
Its output file is pgm5.short.output
10 equations processed
user01@grace$ diff pgm5.short.output
/file~path/pgm5.short.output
user01@grace$ pgm5.py
Input the name of the file which has the coefficients of the quadratic equation (a, b, and c), one set per line:
/file~path/pgm5.long.input
Input the name of the output file you wish to create:
pgm5.long.output
This program is being executed by user01 from /home/user01/temp
Its input file is /file~path/pgm5.long.input
Its output file is pgm5.long.output
200 equations processed
# Description: This program3 solves a second-order polynomial using the quadratic equation.
import math
def quad_solver(a, b, c):
# Solves a quadratic equation and prints the solutions.
# Check if a is zero
if a ==0:
print("The coefficient a cannot be zero.")
return # Exit the function gracefully
# Calculate the discriminant
discriminant = b**2-4*a*c
# Handle different cases based on the discriminant
if discriminant 0:
print("The quadratic equation has no real solutions.")
elif discriminant ==0:
# Calculate the single solution
root =-b /(2*a)
print("The quadratic equation has one solution:")
print(f"x ={root}")
else:
# Calculate the two solutions
root1=(-b + math.sqrt(discriminant))/(2*a)
root2=(-b - math.sqrt(discriminant))/(2*a)
print("The possible values for x are:")
print(f"{root1}")
print(f"{root2}")
# Main program loop
while True:
try:
a, b, c = map(float, input("Enter the coefficients of the quadratic equation (a, b, c) separated by commas: ").split(","))
except ValueError:
print("Invalid input. Please enter three numerical values separated by commas.")
continue # Restart the loop if there's an input error
quad_solver(a, b, c) # Call the solver function
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Real Time Analytics In Big Data A Comprehensive Guide For Everyone

Authors: Lennox Mark

1st Edition

B0CPTC9LY9, 979-8869045706

Students also viewed these Databases questions

Question

2. Why does unpredictability matter?

Answered: 1 week ago

Question

Why are value-added activities defined from a customer viewpoint?

Answered: 1 week ago

Question

4 How can you create a better online image for yourself?

Answered: 1 week ago