Write a program to read the coefficients of a series of quadratic equations from a text file and print the associated roots, or appropriate errors if there are no real roots, to another text file. The coefficients of a quadratic are the a, b and c of an expression of the form ax2 + bx + c, and the roots are the values of x that make the value of the expression 0.
If a == 0, the formula describes a line, and you may ignore any roots (just say it isn’t a quadratic and has no solutions), and if (b2-4ac) < 0 it has only complex roots.
Part 1: Write a function to read one set of values from the file, using reference parameters to get the values out of the function and the function’s return value to indicate whether or not the function was able to correctly read three values. The data error you must deal with here is too few values on the line, e.g., the line has an a value only and no b or c. You may assume that the last line in the file is the only one with an error (if any error exists), and your function should return an error code to make the program stop processing after this error. This restriction allows you to use stream extraction to read the file, rather than reading lines and parsing them yourself. If you want to try doing this, that’s O.K., but get it working the easy way first.
You may not use any global variables in this program. Your variables must be declared appropriately within the functions where needed; and passed to other functions as either reference or value parameters as appropriate.
Each correct input line will comprise three real values of the form
[optional sign][digits][decimal point][digits], or
[optional sign][digits] if integer.
The last input line might have fewer values. For example, your data file might look like this:
1 1 1
1.2 -2.3 0.4
-2 -3 -4
+0 -2 8.85
2.345 (error — only one data point)
The file name is quadratic1.txt
andrew.mendonca.967
nitin1 15 Master Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.