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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <iostream>
#include <fstream>
#include <cmath>
#include "stdDevFns.h"
using namespace std;
// Definition of the function calcStdDev ()
bool calcStdDev (double inputArray [], int n, double & dev, double & avg)
{
bool success = true;
double total = 0.0, stdTotal=0.0;
if (n >= MinStudents && n <= MaxStudents)
{
success = true;
// calculate average grade
for (int i=0; i < n; i++)
total += inputArray [i];
avg = total / n;
// calculate standard deviation
for (int i=0; i < n; i++)
stdTotal += pow ((inputArray [i] - avg), 2);
dev = sqrt (stdTotal / (n-1));
}
else
success = false;
return success;
}
bool inputData (double gradeArray [], int &num)
{
bool worked = false, quit = false;
num = -1;
do
{
cout << "How many students do you have? " << endl;
cout << "Number must be between 2 and 25 (inclusive). Enter -1 to quit" << endl;
cin >> num;
if (num == -1)
quit = true;
else
quit = false;
} while ((num < MinStudents || num > MaxStudents) && !quit);
if (!quit)
{
// Use a for loop to enter data into the array
for (int i=0; i < num; i++)
{
cout << "Enter the grade for student " << i << ": ";
cin >> gradeArray [i];
}
worked = true;
}
return worked;
}
void outputData (double gradeArray [], int num)
{
cout << endl << "The grades for the class are : " << endl;
// Use a for loop to display the grades for the students
for (int i=0; i < num; i++)
{
cout << "Student " << i+1 << " has a grade of: " << gradeArray [i] << endl;
}
cout << endl;
}
bool inputFmFile (double gradeArray [], int &num)
{
bool worked = true, quit = false;
num = -1;
string inFileName;
ifstream inStr;
do
{
cout << "Enter name of file to read from: ";
cin >> inFileName;
inStr.open (inFileName.c_str());
if (inStr.fail())
{
cerr << "Error opening file. Try again." << endl;
inStr.clear();
worked = false;
}
else
worked = true;
} while (!worked);
inStr >> num;
if (num < MinStudents || num > MaxStudents)
{
quit = true;
worked = false;
}
else
quit = false;
if (!quit)
{
// Use a for loop to enter data into the array
for (int i=0; i < num; i++)
inStr >> gradeArray [i];
}
return worked;
}
void outputToFile (double gradeArray [], int num, double dev, double avg)
{
bool worked = false;
string outFileName;
ofstream outStr;
do
{
cout << "Enter name of file to write to: ";
cin >> outFileName;
outStr.open (outFileName.c_str());
if (outStr.fail())
{
cerr << "Error opening file. Try again." << endl;
outStr.clear();
worked = false;
}
else
worked = true;
} while (!worked);
outStr << num << endl;
// Use a for loop to display the grades for the students
for (int i=0; i < num; i++)
outStr << gradeArray [i] << endl;
outStr << dev << endl;
outStr << avg << endl;
}
|