// Dackral Phillips // COMP-6700 // Assignment 6A // 8/3/2K1 // For this assignment, the user is to enter a raw estimate. This program then takes that estimate and // calculates the projected estimate total, as well as the 70% upper and lower prediction ranges as // well as the 90% upper and lower prediction ranges for the values input. // Without further delay, let's dive in and code: Necessary Libraries #include #include #include #include #include #define PI 3.14159 // Make nodes in which to store numbers class Node { public: Node() { init(); } Node(int Value) { init(Value); } void init() { value = 0; next = NULL; } void init(int Value) { value = Value; next = NULL; } Node* Next() { return next; } void setNext(Node *nextPtr) { next = nextPtr; } int GetValue(); private: int value; Node *next; }; int Node::GetValue() { return value; } class LinkedList { public: LinkedList() { init(); } ~LinkedList(); void init() { items = 0; mean = 0; first = NULL; step = NULL; } void add(int Value); void Reset(); double Mean(); double Square(); int ReturnVal(); private: int items; double mean; Node *first; Node *step; }; void LinkedList::add(int Value) { Node *temp; items++; temp = new Node(Value); temp->setNext(first); first = temp; } void LinkedList::Reset() { step = first; } int LinkedList::ReturnVal() { int Value = -1; if (step != NULL) { Value = step->GetValue(); step = step->Next(); } return Value; } double LinkedList::Mean() { Node *temp; Node *trail; trail = first; while (first != NULL) { temp = first; first = first->Next(); mean = mean + temp->GetValue(); } mean = mean / items; first = trail; return mean; } double LinkedList::Square() { double denominator = 0; Node *temp; Node *trail; trail = first; while (first != NULL) { temp = first; first = first->Next(); denominator = denominator + (temp->GetValue() * temp->GetValue()); } first = trail; return denominator; } LinkedList::~LinkedList() { Node *temp; while (first != NULL) { temp = first; first = first->Next(); temp ->init(); delete temp; temp = NULL; } } // Function declarations double beta1(int n, LinkedList &estimate, LinkedList &actual, double xavg, double yavg); double beta0(double yavg, double b1, double xavg); double integrate(double lower, double upper, double n, double t); double tgamma(double n); double gamma(double n); double tfunction(double t, double n); double simpsons(double lower, double upper, double n, double t); double stddev(int n, LinkedList &estimate, LinkedList &actual, double b0, double b1); double range(int n, LinkedList &estimate, LinkedList &actual, double b0, double b1, double projectedEstimate, double xavg, double t); int main() { LinkedList estimate; LinkedList actual; int n = 10; int rawEstimate = 0; double projectedEstimate = 0; double seventyUPI = 0; double seventyLPI = 0; double ninetyUPI = 0; double ninetyLPI = 0; double t70 = 20; double t90 = 20; double degrees = n - 2; int tails = 2; double result = 0; double xavg = 0; double yavg = 0; double b1 = 0; double b0 = 0; char again = 'y'; estimate.add(130); estimate.add(650); estimate.add(99); estimate.add(150); estimate.add(128); estimate.add(302); estimate.add(95); estimate.add(945); estimate.add(368); estimate.add(961); actual.add(186); actual.add(699); actual.add(132); actual.add(272); actual.add(291); actual.add(331); actual.add(199); actual.add(1890); actual.add(788); actual.add(1601); xavg = estimate.Mean(); yavg = actual.Mean(); b1 = beta1(n, estimate, actual, xavg, yavg); b0 = beta0(yavg, b1, xavg); result = integrate(0, t70, degrees, t70); result = result * 2; while ((result < .69) || (result > .71)) { if (result < .69) { t70 = (t70 / 2) + t70; } else if (result > .71) { t70 = (t70 / 2); } result = integrate(0, t70, degrees, t70); result = result * 2; } result = integrate(0, t90, degrees, t90); result = result * 2; while ((result < .89) || (result > .91)) { if (result < .89) { t90 = (t90 / 2) + t90; } else if (result > .91) { t90 = (t90 / 2); } result = integrate(0, t90, degrees, t90); result = result * 2; } while ((again == 'y') || (again == 'Y')) { //system("clear"); cout << endl << endl << "Please enter a Raw Estimate of the Lines of Code : "; cin >> rawEstimate; projectedEstimate = b0 + (b1 * rawEstimate); seventyUPI = range(n, estimate, actual, b0, b1, projectedEstimate, xavg, t70); seventyUPI = projectedEstimate + seventyUPI; seventyLPI = range(n, estimate, actual, b0, b1, projectedEstimate, xavg, t70); seventyLPI = projectedEstimate - seventyLPI; ninetyUPI = range(n, estimate, actual, b0, b1, projectedEstimate, xavg, t90); ninetyUPI = projectedEstimate + ninetyUPI; ninetyLPI = range(n, estimate, actual, b0, b1, projectedEstimate, xavg, t90); ninetyLPI = projectedEstimate - ninetyLPI; //system("clear"); cout << endl << endl << "Raw Estimate : " << rawEstimate << endl << "Projected Estimate :" << projectedEstimate << endl << "70% Upper Prediction Interval :" << seventyUPI << endl << "70% Lower Prediction Interval :" << seventyLPI << endl << "90% Upper Prediction Interval :" << ninetyUPI << endl << "90% Lower Prediction Interval :" << ninetyLPI << endl << endl; cout << "Would you like to calculate another prediction interval (Yy/Nn)? "; cin >> again; } return 0; } double beta1(int n, LinkedList & estimate, LinkedList &actual, double xavg, double yavg) { int i; int esttemp; int acttemp; double numerator = 0; double denominator = 0; double b1 = 0; estimate.Reset(); actual.Reset(); for (i = 0; i < n; i++) { esttemp = estimate.ReturnVal(); acttemp = actual.ReturnVal(); if ((esttemp != -1) && (acttemp != -1)) { numerator = numerator + (esttemp * acttemp); } else { break; } } estimate.Reset(); actual.Reset(); denominator = estimate.Square(); numerator = numerator - (n * (xavg * yavg)); denominator = denominator - (n * (xavg * xavg)); b1 = numerator / denominator; return b1; } double beta0(double yavg, double b1, double xavg) { double b0 = 0; b0 = yavg - (b1 * xavg); return b0; } double gamma(double n) { double answer; if (n == 1) { answer = 1; } else if (n == .5) { answer = sqrt(PI); } else { answer = (n - 1) * gamma((n - 1)); } return answer; } double tgamma(double n) { double answer; answer = (gamma(((n + 1) / 2)) / (sqrt((n * PI)) * gamma((n / 2)))); return answer; } double tfunction(double t, double n) { double answer; answer = pow( (1 + ((t * t) / n)), (-1 * ((n + 1) / 2))); return answer; } double simpsons(double lower, double upper, double n, double t) { double answer = 1; double oldAnswer = 0; double check; double e = 0.01; double w; int flag = 0; double i; check = answer - oldAnswer; if (check < 0) { check = check * -1; } while (check > e) { w = (upper - lower) / n; oldAnswer = answer; answer = tfunction(lower, n); for (i = (lower + w); i < (upper - w); i = (i + w)) { if (flag == 0) { answer = answer + (4 * tfunction(i, n)); flag = 1; } else if (flag == 1) { answer = answer + (2 * tfunction(i, n)); flag = 0; } } answer = answer + tfunction(upper, n); answer = answer * (w / 3); check = answer - oldAnswer; if (check < 0) { check = check * -1; } n = n * 2; } return answer; } double integrate(double lower, double upper, double n, double t) { double answer; answer = tgamma(n) * simpsons(lower, upper, n, t); return answer; } double stddev(int n, LinkedList &estimate, LinkedList &actual, double b0, double b1) { int i; int esttemp = -1; int acttemp = -1; double stnddev = 0; double sum = 0; estimate.Reset(); actual.Reset(); for (i = 0; i < n; i++) { esttemp = estimate.ReturnVal(); acttemp = actual.ReturnVal(); if ((esttemp != -1) && (acttemp != -1)) { sum = sum + ((acttemp - b0 - (b1 * esttemp)) * (acttemp - b0 - (b1 * esttemp))); } else { break; } } estimate.Reset(); actual.Reset(); sum = sum / (n - 2); stnddev = sqrt(sum); return stnddev; } double range(int n, LinkedList &estimate, LinkedList &actual, double b0, double b1, double projectedEstimate, double xavg, double t) { int i; int esttemp = -1; double range = 0; double numerator = projectedEstimate - xavg; double denominator = 0; double sum = 0; double stnddev = 0; estimate.Reset(); for (i = 0; i < n; i++) { esttemp = estimate.ReturnVal(); if (esttemp != -1) { denominator = denominator + ((esttemp - xavg) * (esttemp - xavg)); } else { break; } } estimate.Reset(); sum = 1 + (1/n) + ((numerator * numerator) / denominator); sum = sqrt(sum); stnddev = stddev(n, estimate, actual, b0, b1); range = t * stnddev * sum; return range; }