Created
December 26, 2011 19:00
-
-
Save fhs/1521932 to your computer and use it in GitHub Desktop.
Assignment #5 solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <iomanip> | |
#include <cmath> | |
#include <cstdlib> | |
#include "p0.h" | |
using namespace std; | |
READWRITE::READWRITE(void) | |
{ | |
n = 0; | |
} | |
void | |
READWRITE::READ(void) | |
{ | |
int i, grade; | |
ifstream fin; | |
ofstream fout; | |
fin.open("in.0"); | |
if(fin.fail()){ | |
n = 0; | |
return; | |
} | |
fin >> n; | |
if(n < 0 || n > 999){ | |
cout << "READ: invalid number of grades " << n << endl; | |
n = 0; | |
} | |
i = 0; | |
while(fin >> grade) | |
if(0 <= grade && grade <= 100) | |
A[i++] = grade; | |
if(i != n){ | |
cout << "in.0: number of grades is incorrect" << endl; | |
exit(2); | |
} | |
fin.close(); | |
fout.open("out.0"); | |
if(fout.fail()) | |
return; | |
fout << n << " GRADES ARE READ." << endl; | |
fout.close(); | |
} | |
double | |
READWRITE::AVERAGE(void) | |
{ | |
int i; | |
double sum; | |
sum = 0; | |
for(i = 0; i < n; i++) | |
sum += A[i]; | |
return sum/n; | |
} | |
double | |
READWRITE::STD() | |
{ | |
int i; | |
double sum, mu; | |
mu = AVERAGE(); | |
sum = 0; | |
for(i = 0; i < n; i++) | |
sum += (A[i] - mu)*(A[i] - mu); | |
return sqrt(sum/(n-1)); | |
} | |
void | |
READWRITE::PRINT(void) | |
{ | |
ofstream f; | |
int i; | |
f.open("out.0", fstream::app); | |
if(f.fail()) | |
return; | |
f << n << " GRADES ARE:" << endl; | |
for(i = 0; i < n; i++) | |
f << A[i] << endl; | |
f << endl; | |
f << "average: " << fixed << setprecision(2) << AVERAGE() << endl; | |
f << "std: " << fixed << setprecision(2) << STD() << endl; | |
f.close(); | |
} | |
void | |
READWRITE::REVERSE_PRINT(void) | |
{ | |
ofstream f; | |
int i; | |
f.open("out.0", fstream::app); | |
if(f.fail()) | |
return; | |
f << n << " GRADES ARE:" << endl; | |
for(i = n-1; i >= 0; i--) | |
f << A[i] << endl; | |
f << endl; | |
f << "average: " << fixed << setprecision(2) << AVERAGE() << endl; | |
f << "std: " << fixed << setprecision(2) << STD() << endl; | |
f.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment