Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

so far we have used void functions....now we will be using functions that return

ID: 3794201 • Letter: S

Question

so far we have used void functions....now we will be using functions that return values. Using the list of numbers provided,,,,,,find the best fit of a streight line through it (regression analysis): to generate the equation y = mx + b (Functions that return values) Use basic header like <iostream>, <fstream>
please comment all the lines...

the data:
35
44
36
12
45
89
96
87
54

Teachers hint:

functions that return values

/*
some suggestions on doing the assignment

THIS IS NOT THE ONLY WAY BUT ONE SUGGESTING THAT MAY
MAKE IT SIMPLE.  

THERE ARE A SERIES OF POD CAST SHOWING HOW TO DO THIS
ASSIGNMENT ON ITUNES U.....UNDER ENGINEERING FOR MCCC
   
*/

using namespace std;

#include <iostream>
#include <fstream>

float x[50], y[50];
int NumOfDataPoints;
float m, b;

float sumX();
float sumY();
float sumXY();
float sumXsq();

int main()
{
   m = ((NumOfDataPoints*sumXY()) - (sumX()*sumY())) / ((NumOfDataPoints*sumXsq()) - (sumX()*sumX()));
   b = ((sumY() - (m*sumX())) / float(NumOfDataPoints));
   cout<<"y = "<<m<<"x"<<"+ "<<b<<endl;
   system("pause");//so you can see the equation printed on the screen above
   //you will have to write all the code for all the
   //functions that return values and creat a function
   //to read data into the arrays.
   return 0;   
}


float sumX()
{
    float ans;
    
    return ans;   
}
      
float sumY()
{
   float ans;
   
   return ans;   
}
      
float sumXY()
{
   float ans;
   
   return ans;     
}
      
float sumXsq()
{
   float ans;
      
   return ans;
}

Explanation / Answer

C++ program


#include <iostream>
#include <fstream>
using namespace std;
// global variable declerations
float x[50], y[50];
int NumOfDataPoints;
float m, b;
// function declerations
float sumX();
float sumY();
float sumXY();
float sumXsq();

int main()
{
ifstream datFile;
datFile.open("data.txt"); // input data is in the file data.txt
int count = 0;
while(!datFile.eof())
{
datFile >> y[count]; // reading the data from the file
x[count] = count;// The x data
count ++; // increasing the count
}
NumOfDataPoints = count; // number of data points is the count
// computing m and b using function calls
m = ((NumOfDataPoints*sumXY()) - (sumX()*sumY())) / ((NumOfDataPoints*sumXsq()) - (sumX()*sumX()));
b = ((sumY() - (m*sumX())) / float(NumOfDataPoints));
cout<<"y = "<<m<<"x"<<"+ "<<b<<endl;
//system("pause");//so you can see the equation printed on the screen above
return 0;   
}


float sumX()
{
float ans = 0.0;
for(int i = 0;i<NumOfDataPoints;i++)// sum of all x
ans = ans + x[i];
return ans;   
}
  
float sumY()
{
float ans = 0.0;
for(int i = 0;i<NumOfDataPoints;i++) // sum of all y
ans = ans +y[i];
return ans;   
}
  
float sumXY()
{
float ans = 0.0;
for(int i = 0;i<NumOfDataPoints;i++) // sum of all (xy)
ans = ans +x[i]*y[i];
return ans;   
}
  
float sumXsq()
{
float ans = 0.0;
for(int i = 0;i<NumOfDataPoints;i++) // sum of all x^2
ans = ans +x[i]*x[i];
return ans;
}

data.txt

35
44
36
12
45
89
96
87
54

OUTPUT

y = 6.7x+ 28.5333