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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include "sort.h" //quicksort functions
#include "arrayio.h" //fill functions
#include "linreg.h"
#include "fileio.h"
#include <iostream>
#include <fstream>
using namespace std;
//add function prototypes in separate header files
int main()
{
const int SIZE1 = 10;
double xarray[SIZE1] = {0};
double yarray[SIZE1] = {0};
double sumX = 0.0;
double sumY = 0.0;
double sumXY = 0.0;
double sumX2 = 0.0;
double sumY2 = 0.0;
double gradient = 0.0;
double yintercept = 0.0;
double coefficient = 0.0;
char command = '\0';
do
{
cout << "STATPACK 3.0\tStatistics Program\n------------\t------------------\n";
cout << "Commands:\nf - fill arrays\nd - display arrays\ns - sort each array separately\nx - sort both arrays by x (preserves (x,y) coordinates)\n";
cout << "l - linear regression\nr - read x, y arrays from file\nw - write x, y arrays to file\nc - write calculations to file\n";
cout << "q - quit\n";
cin >> command;
switch (tolower(command))
{
case 'f':
//fill array with random data
cout << "Fill array " << endl << endl;
fillarray(SIZE1, xarray);
fillarray(SIZE1, yarray);
break;
case 'd':
cout << "Display array " << endl << endl;
cout << " x:" << endl;
showarray(SIZE1, xarray);
cout << " y:" << endl;
showarray(SIZE1, yarray);
break;
case 's':
cout << "Sort arrays " << endl << endl;
quickSort(xarray, 0, SIZE1-1);
quickSort(yarray, 0, SIZE1-1);
break;
case 'x':
cout << "Sort arrays by x " << endl << endl;
quickSortx(xarray, yarray, 0, SIZE1-1);
break;
case 'l':
sumX = sumofX(SIZE1, xarray);
cout << "Sum of x: " << sumX << endl;
sumY = sumofY(SIZE1, yarray);
cout << "Sum of y: " << sumY << endl;
sumXY = sumofXY(SIZE1, xarray, yarray);
cout << "Sum of xy: " << sumXY << endl;
sumX2 = sumofsquaresX(SIZE1, xarray, xarray);
cout << "Sum of squares(x): " << sumX2 << endl;
sumY2 = sumofsquaresY(SIZE1, yarray, yarray);
cout << "Sum of squares(y): " << sumY2 << endl;
gradient = m(SIZE1, sumXY, sumX, sumY, sumX2);
cout << "Gradient 'm': " << gradient << endl;
yintercept = yint(SIZE1, sumXY, sumX, sumY, sumX2);
cout << "Y-intercept 'c': " << yintercept << endl;
cout << "y = " << gradient << "x + " << yintercept << endl;
coefficient = coe(SIZE1, sumXY, sumX, sumY, sumX2, sumY2);
cout << "Correlation coefficient 'r': " << coefficient << endl;
cout << endl;
break;
case 'r':
cout << "Read x, y arrays from file" << endl << endl;
readarray(SIZE1, xarray, yarray);
break;
case 'w':
cout << "Write x, y arrays to file" << endl << endl;
writearray(SIZE1, xarray, yarray);
break;
case 'c':
/* calculations regarding sum of x, sum of squares of x, etc. included in this case statement
so that calculations can be posted to file immediately without having to go through 'l' case first */
sumX = sumofX(SIZE1, xarray);
sumY = sumofY(SIZE1, yarray);
sumXY = sumofXY(SIZE1, xarray, yarray);
sumX2 = sumofsquaresX(SIZE1, xarray, xarray);
sumY2 = sumofsquaresY(SIZE1, yarray, yarray);
gradient = m(SIZE1, sumXY, sumX, sumY, sumX2);
yintercept = yint(SIZE1, sumXY, sumX, sumY, sumX2);
coefficient = coe(SIZE1, sumXY, sumX, sumY, sumX2, sumY2);
cout << "Write calculations to file" << endl;
writecalculations(SIZE1, sumX, sumY, sumXY, sumX2, sumY2);
break;
case 'q':
cout << "exiting...";
break;
default:
cout << "Valid commands are s, f, q\n";
}
}while (toupper(command) != 'Q');
cout << "Bye... " << endl;
return 0;
}
|