1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
int main(void)
{
float xmax, xmin, xtotal, ymax, ymin, ytotal, slope,x,y,xytotal,xy,loopn,xsquared,xsqtotal,xmean,ymean, yin,xrange,yrange;
char choice;
while(1)
{
system("cls");
xtotal = 0;
ytotal = 0;
xytotal = 0;
loopn = 1;
cout << "Please enter in your first X and Y values. \nTo stop entering data, please press Ctrl-Z and hit Enter.\n\n";
cout << "Value of x = ";
cin >> x;
cout <<"\n";
cout << "Value of y = ";
cin >>y;
cout <<"\n";
xy = x*y;
xsquared = pow(x,2);
xsqtotal = xsqtotal+xsquared;
xytotal = xytotal+xy;
xtotal = xtotal+x;
ytotal = ytotal+y;
xmax = x;
xmin = x;
ymax = y;
ymin = y;
while(!cin.eof())
{
cout << "Please enter your next X and Y values.\n\n";
cout << "Value of x = ";
cin >> x;
cout <<"\n";
cout << "Value of y = ";
cin >>y;
cout <<"\n\n";
if (x>xmax)
{
xmax = x;
}
if (x<xmin)
{
xmin = x;
}
if (y>ymax)
{
ymax = y;
}
if (y<ymin)
{
ymin = y;
}
xtotal = xtotal+x;
ytotal = ytotal+y;
xy = x*y;
xsquared = pow(x,2);
xsqtotal = xsqtotal+xsquared;
xytotal = xytotal+xy;
loopn++;
}
xmean = xtotal/loopn;
ymean = ytotal/loopn;
slope = ((xytotal)-(xtotal*ymean))/((xsqtotal)-(xtotal*xmean));
yin = ymean-(slope*xmean);
xrange = xmax-xmin;
yrange = ymax-ymin;
cout << "The equation of the line which best fits the given data is: \n\ny= "<<slope<<"x + "<<yin<<"\n\n";
cout << "The range of x is = "<<xrange<<"\n\n";
cout << "The range of y is = "<<yrange<<"\n\n";
cout << "Would you like to key in another set of data? <y/n>\n";
choice = getchar();
if (choice=='n')
break;
else
continue;
}
}
|