Answered step by step
Verified Expert Solution
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
Quadratic Equation with File IO and Formatted Output
You are to modify your Program to doinclude the following:
Read the coefficients of the quadratic, coefficients per linethe 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, 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 b c: Two solutions for x: and
For a b c: One solution for x: For a b c: Two solutions for x: and
For a b c: Two solutions for x: and
For a b c: Can not solve, bac negative.
For a b c: Not a quadratic equation if a
For a b c: Two solutions for x: and
For a b c: Can not solve, bac negative.
ERROR: Input must have coefficients, line read had For a b c: Can not solve, bac negative.
ATTACHMENT B
user@grace$ pgmpy
Input the name of the file which has the coefficients of the quadratic equation a b and c one set per line:
file~pathpgmshort.input
Input the name of the output file you wish to create:
pgmshort.output
This program is being executed by user from homeusertemp
Its input file is file~pathpgmshort.input
Its output file is pgmshort.output
equations processed
user@grace$ diff pgmshort.output
file~pathpgmshort.output
user@grace$ pgmpy
Input the name of the file which has the coefficients of the quadratic equation a b and c one set per line:
file~pathpgmlong.input
Input the name of the output file you wish to create:
pgmlong.output
This program is being executed by user from homeusertemp
Its input file is file~pathpgmlong.input
Its output file is pgmlong.output
equations processed
# Description: This program solves a secondorder polynomial using the quadratic equation.
import math
def quadsolvera b c:
# Solves a quadratic equation and prints the solutions.
# Check if a is zero
if a :
printThe coefficient a cannot be zero."
return # Exit the function gracefully
# Calculate the discriminant
discriminant bac
# Handle different cases based on the discriminant
if discriminant :
printThe quadratic equation has no real solutions."
elif discriminant :
# Calculate the single solution
root b a
printThe quadratic equation has one solution:"
printfx root
else:
# Calculate the two solutions
rootb math.sqrtdiscriminanta
rootb math.sqrtdiscriminanta
printThe possible values for x are:"
printfroot
printfroot
# Main program loop
while True:
try:
a b c mapfloat inputEnter the coefficients of the quadratic equation a b c separated by commas: split
except ValueError:
printInvalid input. Please enter three numerical values separated by commas."
continue # Restart the loop if there's an input error
quadsolvera b c # Call the solver function
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started