Code:
#include <fstream.h>
#include <stdlib.h>
// Math library
#include <math.h>
const int FileNameLen = 30, ArrayLen = 6;
// The class array
class array
{
public:
array(void);
//Inline functions
inline int getsize() {return(size);}
void read(void);
void write(void);
void draw(void);
private:
//Private - available only to member functions
int x[ArrayLen];
int size;
};
array::array(void)
{
size = 0;
}
// Read the array
void array::read(void)
{
size = 0;
// Read each value
do {
cout << "Enter a value\t?";
cin >> x[size++]; // Read an integer
} while (x[size-1] != 0 && size < ArrayLen);
// Array indices run from 0 to n-1
size = (size == ArrayLen)? size - 2: size - 1;
}
void array::write(void)
{
//CREATE A FILE
ofstream outfile; // file object
char filename[FileNameLen]; // Store the file name
// as a character string
cout << "Enter file name ->\t";
cin >> filename;
// Open the file
outfile.open(filename);
// If you can't open the file, print an error message
// and quit
if (!outfile) {
cerr << "Cannot open " << filename << endl;
exit(1);
}
for (int i = 0; i < size; i++)
outfile << "x[" << i << "] = " << x[i] << '\n';
}
void array::draw(void)
{
ifstream infile; // file object
char filename[FileNameLen]; // Store the file name
char inchar; // as a character string
cout << "Enter file name ->\t";
cin >> filename;
infile.open(filename);
if (!infile) {
cerr << "Cannot open " << filename << endl;
exit(1);
}
//Read the file a charachter
while (!infile.eof())
{
inchar = infile.get(); //This reads in
//even whitespace charachters
cout << inchar; //Display it on screen
}
infile.close(); //close the file
}
int main(void)
{
double sum = 0.0;
double sumOfSquares = 0.0;
double average = 0.0;
double standardDeviation = 0.0;
//display file of screen
const int FileNameLen = 30; // constant integer
ifstream infile; //file object
average = sum / ArrayLen;
standardDeviation = sqrt(sumOfSquares / ArrayLen - average * average);
array myarray;
myarray.draw();
cout << "\nThe average is: " << average << endl;
cout << "The standard deviation is: " << standardDeviation <<endl;
return(0);
}
also consider that the program reads the following txt file that has these values
x[0] = 5
x[1] = 6
x[2] = 7
x[3] = 45
so the program has to find the average and standard deviation. can someone help me out and show me how is that done? i have the formulas and all but i dont know how to compute all that from the array.