Member Avatar for andrew.mendonca.967
andrew.mendonca.967

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.

If a == 0, no solution, and if (b2-4ac) < 0 it has only complex roots.

Function 1:
- Read one set of values from the file, using reference parameters to get the values out of the function
- Return whether or not the function was able to correctly read three values. The data error is too few values on the line, e.g., the line has an a value only and no b or c. The last line in the file is the only one with an error (if any error exists)
- 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.

Function 2:
- Calculate the roots, taking the coefficients through value parameters and giving back the roots (if they exist) via reference parameters
- Return roots (0), no solution (-1) or complex roots (-2). Do not call this function if you have a data error on input.

Function 3:
- Print a reasonably formatted table (one row per call from the main loop) of the coefficients, and either the roots or (different) messages indicating the various error conditions.
- Print appropriate error messages in the cases of a data error on input and either no solution or complex roots from the calculation function.
- Your table must have a reasonable title line (or lines) with legends describing what things are below it in the columns, and this table's title line must be printed inside the print function.
- The function must know if it is being called for the first time (or not) to print the title line. You may not pass this information into the print function from main(). The print function MUST do this itself.
- Always print the coefficients unless you have a read error. - Align the values in the columns in a reasonable way

Function 4 (main):
- Prompt for the file names (hold the names in C-strings)
- Open the input and output files
- Check for file open errors appropriately
- Loop over the input file reading coefficients, calculating roots, and printing results to the output file until either end-of-file or error on input
- Close all the files and exit.
- You may make no assumptions about how many coefficients are in the input file (e.g., you may not hold the values in arrays and process them after reading them all).

Note:
- No global variables allowed in this program.
- Variables must be declared appropriately within the functions where needed; and passed to other functions as either reference or value parameters as appropriate.
- Your functions will indicate any problem they encounter by returning a value to main(), where the error must be handled appropriately.
- Your functions outside main() may do only the task assigned to them, and must do that entire task.
- For example, you may not check for a == 0 within main and only call the calculation function if a is not zero.

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)

These data are available as the file quadratic1.txt

Here is my solution to the homework problem:

#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
#include<cmath>
using namespace std;

int quadValues(double&, double&, double&);
int calcRoots(double, double, double, double&, double&);
void numTable(ofstream&, double, double, double, double&, double&);

// Read each set of values from the input file.
int quadValues(double &x, double &y, double &z)
{
    int value;  
    double root1, root2;
    string fileName;
    ifstream inputFile;
    ofstream outputFile;

    // Get the name of the input file from the user.
    cout << "Enter the name of the file: ";
    cin >> fileName;

    // Open the file.
    inputFile.open(fileName.c_str());

    // If successfully opened, process the file data.
    if(inputFile)
    {
        // Indicate whether function read three values.
        while(!inputFile.eof())
        {
            inputFile >> x >> y >> z;
            if(!inputFile)
            {
                return 1;
            }
        }    
        // Close the file.
        inputFile.close();
    }
    else
    {
        // Display the error message
        cout << "There was an error opening the input file.\n";
    }
}

// Calculate the roots
int calcRoots(double a, double b, double c, double &root1, double &root2)
{
    // If a is 0, there is no solution.
    if(a == 0)
    {
        return -1;
    }
    // If discriminant is less than 0, there are complex roots.
    else if((b*b-4*a*c) < 0)
    {
        return -2;
    }
    // Otherwise, return the real numbers.
    else
    {    
        root1 = (-b+sqrt((b*b)-(4*a*c)))/(2*a);
        root2 = (-b+sqrt((b*b)-(4*a*c)))/(2*a);   
        return 0;
    }
}

// Print results on a formatted table
void numTable(ofstream &outputFile, double a, double b, double c, double &root1, double &root2)
{
    static int spaces = 10;
    ifstream inputFile;

    // Open the output file named quadratic table.txt.
    outputFile.open("quadratic result.txt");
    // Write the output to the file.
    outputFile << "a" << setw(spaces) << "b" << setw(spaces) << "c" << setw(spaces)
    << "Root 1" << setw(spaces) << "Root 2" << setw(spaces) << "Errors" << endl;
    outputFile << "---------------------------------------" 
    << "--------------" << endl;
    outputFile << right << a << setw(spaces) << b << setw(spaces) 
    << c << setw(spaces) << root1 << setw(spaces) << root2 << endl;
    if(!inputFile)
    {
        cout << "Not enough values" << endl;
    }
    if(a == 0)
    {
        cout << "No solution" << endl;
    }
    else if((b*b-4*a*c) < 0)
    {
        cout << "Complex Roots" << endl;
    }
    // Close the output file.
    outputFile.close();
}

// Call every function.
int main()
{
    double x, y, z, root1, root2;
    ofstream outputFile;

    quadValues(x, y, z);
    calcRoots(x, y, z, root1, root2);
    numTable(outputFile, x, y, z, root1, root2);

    return 0;
}

Here is the output I got:

a         b         c    Root 1    Root 2    Errors
-----------------------------------------------------
2.345        -2      8.851.48856e+2695.2843e-308

I want to go over this program one function at a time. For the first function, which is quadValues(), I don't think I'm suppose to be calling the other two functions. In this function, is this the correct way to read one set of values from the file and return an error code if there are too few values on a line? If not, is there something in that function I need to add/change?

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.