// Dackral Phillips // Comp 6700 - The Software Process // This program calculates the mean and the standard deviation of a group of numbers // stored in a linked list. #include #include #include // 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; } double GetValue(); private: double value; Node *next; }; double Node::GetValue() { return value; } class LinkedList { public: LinkedList() { init(); } ~LinkedList(); void init() { items = 0; mean = 0; stddeviation = 0; first = NULL; } void add(int Value); double Mean(); double StdDeviation(); private: int items; double mean; double stddeviation; Node *first; }; void LinkedList::add(int Value) { Node *temp; items++; temp = new Node(Value); temp->setNext(first); first = temp; } 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::StdDeviation() { Node *temp; Node *trail; double difference; trail = first; while (first != NULL) { temp = first; first = first->Next(); difference = temp->GetValue(); difference = difference - mean; stddeviation = stddeviation + (difference * difference); } stddeviation = stddeviation / (items - 1); stddeviation = sqrt(stddeviation); first = trail; return stddeviation; } LinkedList::~LinkedList() { Node *temp; while (first != NULL) { temp = first; first = first->Next(); temp ->init(); delete temp; temp = NULL; } } int main() { LinkedList testset1; LinkedList testset2; LinkedList testset3; double mean1, mean2, mean3; double stddeviation1, stddeviation2, stddeviation3; testset1.add(160); testset1.add(591); testset1.add(114); testset1.add(229); testset1.add(230); testset1.add(270); testset1.add(128); testset1.add(1675); testset1.add(624); testset1.add(1503); mean1 = testset1.Mean(); stddeviation1 = testset1.StdDeviation(); testset2.add(186); testset2.add(699); testset2.add(132); testset2.add(272); testset2.add(291); testset2.add(331); testset2.add(199); testset2.add(1890); testset2.add(788); testset2.add(1601); mean2 = testset2.Mean(); stddeviation2 = testset2.StdDeviation(); testset3.add(15.0); testset3.add(69.9); testset3.add(6.5); testset3.add(22.4); testset3.add(28.4); testset3.add(65.9); testset3.add(19.4); testset3.add(198.4); testset3.add(38.8); testset3.add(138.2); mean3 = testset3.Mean(); stddeviation3 = testset3.StdDeviation(); cout << "The Mean of Test Set #1 is: " << mean1 << endl << "The Standard Deviation of Test Set #1 is: " << stddeviation1 << endl << endl; cout << "The Mean of Test Set #2 is: " << mean2 << endl << "The Standard Deviation of Test Set #2 is: " << stddeviation2 << endl << endl; cout << "The Mean of Test Set #3 is: " << mean3 << endl << "The Standard Deviation of Test Set #3 is: " << stddeviation3 << endl << endl; return 0; }