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
|
#include <iostream>
#include <fstream>
using namespace std;
void introduction();
void readdata(int &, double[]);
void printarray(int &, double[]);
double findaverage(int &, double[]);
void howfaraway(int &, double[], double []);
int main()
{
int size=0, q=0;
ifstream infile;
infile.open ("infile.txt");
ofstream outfile;
outfile.open("output.txt");
introduction();
cout << "Enter the size of your array > ";
cin >> size;
double first[size];
double second[size];
readdata(size, first);
outfile << "Here is the original array \n";
printarray(size, first);
outfile << "\n";
outfile << "Here is the average of the first array \n"
<< findaverage(size, first) << "\n";
howfaraway(size, first, second);
outfile << "Here is the new array \n";
printarray(size, second);
outfile << "Here is the new average \n"
<< findaverage(size, second) << "\n";
return 0;
}
void readdata(int &n, double numbers[]) // will n numbers from file)
{
ifstream infile;
infile.open("infile.txt");
for(int i=0;i <= n; i++){
infile >> numbers[i];
}
infile.close();
return;
}
void printarray (int &q, double numb[]) // will print q numb from file
//in order of 5 before printing to a new line
{
ofstream outfile;
outfile.open("output.txt");
for (int n = 0 ; n < q ; n++)
{
if (n==5)
{
outfile << endl;
}
outfile << numb[n] << " ";
}
outfile.close();
return;
}
double findaverage(int &k, double p[]) // will find the average
// of the first array
{
double sum =0;
for(int i=0; i <= k; i++)
sum+=p[i];
return (double) sum/k;
}
void howfaraway(int &m, double r[], double s[]) // will construct a new
// array from the difference of
//the average of the first array
//and the first number of the first array
//and continue so forth
{
for(int i=0; i<= m; i++){
s[i]=r[i]-findaverage(m,r);
}
return;
}
void introduction() // will introduce the program and what it will do
{
ofstream outfile;
outfile.open("output.txt");
outfile << "\t\t Jonathan Velez \n"
<< "\t\t Program 4 \n"
<< "Program 4 will receive input from a file \n"
<< "it will then read the next n numbers \n"
<< "and find their average and print it in an output \n"
<< "after which it will make a new array of numbers \n"
<< "and find the average of the new numbers \n";
outfile.close();
return;
}
|