This is what I have so far, I had a function for the standard deviation but I srapped it as I was overwhelmed with my errors. The formula for standard deviation is: (n 1 – a)2 , (n 2 – a)2 , (n 3 – a)2 , and so forth. This is also my first time using stackoverflow, so any pointers or direction is much appreciated.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
void fileNumber_generator(ofstream& fout, int maximum);
// Generates numbers 0 - 9 in the file chosen.
double average(ifstream& fin, int maximum);
// Creates the average of all the numbers in the file.
int main()
{
ifstream fin;
ofstream fout;
int maximum = 10;
srand(time(0));
fileNumber_generator(fout, maximum);
cout << average(fin, maximum) << endl;
}
void fileNumber_generator(ofstream &fout, int maximum)
{
int i;
double number = 0;
fout.open("numbers.txt");
if (fout.fail())
{
cout << "File could not open.\n";
exit(1);
}
for (i=0; i < maximum; i++)
{
fout << number << endl;
number++;
}
fout.close();
}
double average(ifstream& fin, int maximum)
{
int i;
double number = 0, total = 0, average = 0;
fin.open("numbers.txt");
if (fin.fail( ))
{
cout << "File could not open.\n";
exit(1);
}
if (fin)
fin >> number;
while (fin)
{
total = total + number;
i++;
fin >> number;
}
if (i > maximum)
{
average = total / maximum;
return(average);
}
}
Aucun commentaire:
Enregistrer un commentaire