/****************************************************/ /* */ /* CSCI 121: Principles of Computer Programming */ /* Programming Project #8 */ /* Due Monday, March 21st */ /* Programmed by Ian Giese */ /* */ /****************************************************/ #include #include #include #include using namespace std; double calcSlope(int n, double sumXY, double sumX, double sumY, double sumX2) { double slope; slope = (((n * sumXY) - (sumX * sumY))/((n * sumX2) - pow(sumX, 2))); return slope; } double calcIntercept(double yBar, double slope, double xBar) { double yInt; yInt = yBar - (slope * xBar); return yInt; } void doReportLine(ofstream &output, double price, double maintCost, double slope, double yInt) { double predCost, error; predCost = (slope * price) + yInt; error = maintCost - predCost; output << fixed << showpoint << setprecision(2) << right; output << setw(8) << price << setw(14) << maintCost << setw(14) << predCost << setw(11) << error << endl; } void doHeadings(ofstream &output, double yInt, double slope) { output << "Lake Side Software - Equipment Maintenance Estimation" << endl << endl << right << showpoint << setprecision(4) << "Linear Regression Coefficients: m = " << setw(9) << slope << setw(36) << "b = " << setw(9) << yInt << endl << endl << "Summary of Original Data" << endl << endl << left << "Purchase Maintenance Predicted" << endl << " Price Cost Cost Error" << endl << "-------- ----------- --------- -------" << endl; } int main() { double preCost[101], preMaint[101]; double sumX, sumX2, sumY, sumXY, xBar, yBar, slope, yInt; int counter; ifstream input("assign8.txt"); ofstream output("output8.txt"); counter = 0; do { input >> preCost[counter] >> preMaint[counter] >> ws; counter++; } while(!input.eof()); int j = 0; do { sumX += preCost[j]; sumX2 += (preCost[j] * preCost[j]); sumY += preMaint[j]; sumXY += (preCost[j] * preMaint[j]); j++; } while(j < counter); xBar = sumX / counter; yBar = sumY / counter; slope = calcSlope(counter, sumXY, sumX, sumY, sumX2); yInt = calcIntercept(yBar, slope, xBar); doHeadings(output, yInt, slope); for(int j = 0; j < counter; j++) doReportLine(output, preCost[j], preMaint[j], slope, yInt); return 0; }