nip> // Needed for setw and others #include // Needed for ifstream #include // Needed for string data type #include // Needed for pow #include "Graph.h" // Used to graph t

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Topic Video
Question

Develop a program that takes a set of pairs of data values (e.g. sales and month), fits a straight line to the data, and displays the results in a table and chart. Fitting a line or equation to data is done in many fields including business and scientific research.

 

__________________________________________________________

#include <iostream> // Needed for cout and cin
#include <iomanip>   // Needed for setw and others
#include <fstream>   // Needed for ifstream
#include <string>    // Needed for string data type
#include <cmath>     // Needed for pow

#include "Graph.h"   // Used to graph the results

using namespace std; // To use cout instead of std::cout

// Function prototypes
bool readArrays(ifstream& inFile, double x[], double y[], int size);

int main() {
    // Do "Press return to continue..." on exit
    atexit([] {system("pause"); });

    // Prompt for the name of the data file and store it in a string variable named fileName.
    // Put your code here

    string fileName; // initializing string filename
    cout << "Enter data file name: ";
    cin >> fileName;

    // Define a variable for the data file and open the file.
    ifstream inFile(fileName);

    // If the open failed, report an error/
    if (inFile.fail()) {
        cout << "Open of " << fileName << " failed." << endl;
        return 1; // 1 means program failed.
    }

    // Read the title of the data set into a string variable named title.
    // Put your code here
  


    // Display the title.
    // Put your code here

    // Define an unsigned int variable named n. This will hold how many pairs are in the data file.
    // Put your code here

    // Read n from the file.
    // Put your code here

    // Allocate dynamic memory arrays of doubles named x and y for the x and y values.
    // Put your code here

    // Read the x and y values from the file.
    if (!readArrays(inFile, x, y, n)) {
        cout << "Failed to read data pairs from " << fileName << endl;
        return 1;
    };

    // Close the file.
    // Put your code here

    // Compute the sums needed for the least squares computation.
    double xSum = 0.0, ySum = 0.0, xySum = 0.0, x2Sum = 0.0;
    for (unsigned i = 0; i < n; i++) {
        // Put your code here
    }

    // Compute the slope a and y-intercept b of the line that fits the data.
    // Put your code here

    // Allocate an dynamic memory array of doubles named yFitted for the fitted values.
    // Put your code here

    // Compute fitted values corresponding to the x values.
    // Put your code here

    // Display the results in a tabular form.
    cout << endl;
    cout << setw(8) << "x" << setw(12) << "y(actual)" << setw(12) << "y(fitted)" << endl;
    cout << setw(8) << "----" << setw(12) << "---------" << setw(12) << "---------" << endl;
    for (unsigned i = 0; i < n; i++) {
        // Put your code here
    }

    // Display the equation for the line.
    cout << fixed << setprecision(5);
    cout << "\nThe equation for the line is: y = " << m << "x + " << b << endl << endl;

    // Display the results in a graph.
    Graph graph;
    graph.setWindowTitle(title.c_str());
    graph.setPlot(Graph::PlotType::POINTS, "Actual", n, x, y);
    graph.setPlot(Graph::PlotType::LINES, "Fitted", n, x, yFitted);
    graph.mainLoop();

    // Deallocate the arrays.
    // Put your code here

    return 0; // 0 means program worked
}


// Function definitions

// readArrays - Read the (x,y) pairs of data into x and y arrays from inFile.
// The parameter size is the number of pairs to read.
// Return false if unable to read size pairs from the file.
// Otherwise, return true.
bool readArrays(ifstream& inFile, double x[], double y[], int size) {
    for (int i = 0; i < size; i++) {
        // Read x
        inFile >> x[i];
        // Return false if that failed
        if (!inFile) return false;
        // Read y
        inFile >> y[i];
        // Return false if that failed
        if (!inFile) return false;
    }
    // No failures so return true
    return true;
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY