Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Timothy Tran Program #5
- //This program will build an array of numbers from an
- //input file and find its average and differences.
- #include <iostream>
- #include <fstream>
- using namespace std;
- void readdata (int, double[]);
- void printarray(int, double[]);
- double findaverage(int, double[]);
- void howfaraway(int, double, double[], double[]);
- ifstream infile("num.txt");
- int main() {
- int size;
- double first[size];
- double average;
- double second[size];
- infile >> size;
- readdata(size, first);
- cout << "Here is the original array." << endl;
- printarray(size, first);
- average = findaverage(size, first);
- cout << "The average is equal to" << average << endl;
- howfaraway(size, average, first, second);
- cout << "This is the new array for this program" << endl;
- printarray(size, second);
- cout << "The new average is equal to" << findaverage(size, second) << endl;
- infile.close();
- return 0;
- }
- // readdata will read in multiple data's into an array. The
- // data will be used throughout the program.
- void readdata(int n, double numbers[])
- {
- for(int i = 0; i <= n; i++){
- infile >> numbers[i];
- }
- return;
- }
- // printarray will print an array of the numbers in rows of five.
- // After the five it goes tot he next row.
- void printarray(int q, double numbs[])
- {
- cout << "Here is the original array." << endl;
- cout.setf(ios::fixed,ios::floatfield);
- cout.precision(2);
- for(int i = 0; i <= q; i++){
- cout << numbs[i] << " ";
- if((i+1) % 5 == 0){
- cout << endl;
- }
- }
- cout << endl;
- return;
- }
- // findaverage will find the average of the total of the array numbers
- // and divide it by tht many numbers.
- double findaverage(int k, double p[])
- {
- int i;
- double sum;
- double avg;
- for(i = 0; i <= k; i++){
- sum = sum + p[i];
- }
- avg = sum / i;
- return avg;
- }
- // howfaraway will find the difference between the the original arrays average and the
- // number itself.
- void howfaraway (int m, double avg, double r[], double s[])
- {
- for(int i = 0; i <= m; i++){
- s[i] = r[i] - avg;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement