Write a C/C++ program that calculates the least squares line for two sets of values. The program consists of several functions which are described below.   The first function you need to write: int ReadFile (int *, int *); Write a function that reads the contents of a text file (Data1.txt) into two separate integer arrays. The function receives two integer pointers to arrays as parameters. The contents of the text file that is attached is structured as follows: Each line in the file contains two values that are separated by a comma. The values on the left of the comma, are stored in one array and the values on the right side is stored in the other array. The number of lines that are read from the file and stored in the arrays are returned to the calling statement. Tip: a single fscanf can read into multiple variables. Ex. the statement fscanf(fp, "%s %s\n", name,surname) will read the line John Doe and save the name and surname separately. (Note the space between %s and %s indicates that there is a space in the data between the two strings to be captured)   The second function you need to write: void LeastSquaresLine (int *, int *, double *, double *, int); Write a function that determines the elements a0 and a1 of the least squares line for the contents of two arrays. The function receives two integer pointers for arrays (x and y), two reference parameters for the return of a0 and a1, and the length of the arrays. *The calculations you will need to determine the Least squares line is attached in the image below.*   The third function you need to write: void WriteFile (double, double); Write a function that saves the formula of the least squares line to a text file(Output.txt). The function receives two real values for a0 and a1 after which  the formula is saved in the form y = mx + c where m = a0 and c = a1.  The example of the output file is attached in the image below: If the file can not be opened for writing an appropriate error message is to be displayed. The function does not return any value to the calling statement   int main (); Implement the above functions in the main function to complete the program.   Summary: You need to write 3 functions(int ReadFile (int *, int *); void LeastSquaresLine (int *, int *, double *, double *, int); void WriteFile (double, double);) and call them in your main function. The ReadFile reads the Data1.txt file. The LeastSquaresLine function uses the data it read from ReadFile to calculate the least square line with the calculations shown in the image below. The WriteFile function creates a new file called Output.txt and rewrites the least square line in the Output.txt file like shown in the output example in the image below. The Data1.txt file you need to use is shown below, you need to copy that values into your notepad on your computer and save it as Data1.txt. Save the Data1.txt file in the same file you save your code in order for it to work.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Topic Video
Question

Write a C/C++ program that calculates the least squares line for two sets of values. The program consists of several functions which are described below.

 

The first function you need to write:

int ReadFile (int *, int *);

Write a function that reads the contents of a text file (Data1.txt) into two separate integer arrays. The function receives two integer pointers to arrays as parameters.

The contents of the text file that is attached is structured as follows: Each line in the file contains two values that are separated by a comma. The values on the left of the comma, are stored in one array and the values on the right side is stored in the other array.

The number of lines that are read from the file and stored in the arrays are returned to the calling statement.

Tip: a single fscanf can read into multiple variables. Ex. the statement fscanf(fp, "%s %s\n", name,surname) will read the line John Doe and save the name and surname separately. (Note the space between %s and %s indicates that there is a space in the data between the two strings to be captured)

 

The second function you need to write:

void LeastSquaresLine (int *, int *, double *, double *, int);

Write a function that determines the elements a0 and a1 of the least squares line for the contents of two arrays. The function receives two integer pointers for arrays (x and y), two reference parameters for the return of a0 and a1, and the length of the arrays.

*The calculations you will need to determine the Least squares line is attached in the image below.*

 

The third function you need to write:

void WriteFile (double, double);

Write a function that saves the formula of the least squares line to a text file(Output.txt). The function receives two real values for a0 and a1 after which  the formula is saved in the form y = mx + c where m = a0 and c = a1. 

The example of the output file is attached in the image below:

If the file can not be opened for writing an appropriate error message is to be displayed. The function does not return any value to the calling statement

 

int main ();

Implement the above functions in the main function to complete the program.

 

Summary:

You need to write 3 functions(int ReadFile (int *, int *); void LeastSquaresLine (int *, int *, double *, double *, int); void WriteFile (double, double);) and call them in your main function. The ReadFile reads the Data1.txt file. The LeastSquaresLine function uses the data it read from ReadFile to calculate the least square line with the calculations shown in the image below. The WriteFile function creates a new file called Output.txt and rewrites the least square line in the Output.txt file like shown in the output example in the image below.

The Data1.txt file you need to use is shown below, you need to copy that values into your notepad on your computer and save it as Data1.txt. Save the Data1.txt file in the same file you save your code in order for it to work. 

Data1.txt:

 

Given a set of N data points,
(xi, Yi), i = 1, 2, 3, ..., N,
we can calculate the least squares line y = aox+a1
with:
ΝΣ1Y -(Σ ) (Σ)
NEa2 – (Ex)2
(Σ) (Σ?)-(Σ (Σε)
NE1² – (Ex)²
ao
-
-
Transcribed Image Text:Given a set of N data points, (xi, Yi), i = 1, 2, 3, ..., N, we can calculate the least squares line y = aox+a1 with: ΝΣ1Y -(Σ ) (Σ) NEa2 – (Ex)2 (Σ) (Σ?)-(Σ (Σε) NE1² – (Ex)² ao - -
| Output.bxt - Notepad
Eile Edit Format View Help
The least squares line is y=-0.030303x+4.636364
< >
Transcribed Image Text:| Output.bxt - Notepad Eile Edit Format View Help The least squares line is y=-0.030303x+4.636364 < >
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

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-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education