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. 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) 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 least squares line is determined as follows:   The function does not return any value to the calling statement. void WriteFile (double, double); Write a function that saves the formula of the least squares line to a text file. 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. See the example output for the file contents.   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.  Data1.txt  1,0 2,9 3,8 4,7 5,6 6,5 7,4 8,3 9,2

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter11: Nonlinear Programming
Section11.3: Convex And Concave Functions
Problem 13P
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.

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)

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 least squares line is determined as follows:

 

The function does not return any value to the calling statement.

void WriteFile (double, double);

Write a function that saves the formula of the least squares line to a text file. 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. See the example output for the file contents.

 

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.

 Data1.txt 

1,0

2,9

3,8

4,7

5,6

6,5

7,4

8,3

9,2

0,1

The least squares line is determined as follows:
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:
ΝΣsy- (Σ (Σ)
NE2² – (Ex)²
(Σ) (Σ)-(Σ ) (Στ)
ΝΣ22 - (Σ)>
do
The function does not return any value to the calling
statement.
void WriteFile (double, double);
Write a function that saves the formula of the least
squares line to a text file. 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. See the example output for the file contents.
Output.txt - Notepad
Eile Edit Format View Help
The least squares line is y=-0.030303x+4.636364
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
Transcribed Image Text:The least squares line is determined as follows: 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: ΝΣsy- (Σ (Σ) NE2² – (Ex)² (Σ) (Σ)-(Σ ) (Στ) ΝΣ22 - (Σ)> do The function does not return any value to the calling statement. void WriteFile (double, double); Write a function that saves the formula of the least squares line to a text file. 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. See the example output for the file contents. Output.txt - Notepad Eile Edit Format View Help The least squares line is y=-0.030303x+4.636364 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
(Σ) /Σα") -(Σ ) (Σ)
ΝΣ2? - (Σ ε)>
|
The function does not return any value to the calling
statement.
void WriteFile (double, double);
Write a function that saves the formula of the least
squares line to a text file. 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 =
%3D
a1. See the example output for the file contents.
Output.t - Notepad
File Edit Format View Help
The least squares line is y=-0.030303x+4.636364
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.
Additional resources for assignment
Data1.txt (1 KB; 05-Nov-2020 14:53 )
Transcribed Image Text:(Σ) /Σα") -(Σ ) (Σ) ΝΣ2? - (Σ ε)> | The function does not return any value to the calling statement. void WriteFile (double, double); Write a function that saves the formula of the least squares line to a text file. 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 = %3D a1. See the example output for the file contents. Output.t - Notepad File Edit Format View Help The least squares line is y=-0.030303x+4.636364 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. Additional resources for assignment Data1.txt (1 KB; 05-Nov-2020 14:53 )
Expert Solution
trending now

Trending now

This is a popular 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-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole