Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. //Timothy Tran Program #5
  2. //This program will build an array of numbers from an
  3. //input file and find its average and differences.
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. void readdata (int, double[]);
  9. void printarray(int, double[]);
  10. double findaverage(int, double[]);
  11. void howfaraway(int, double, double[], double[]);
  12.  
  13. ifstream infile("num.txt");
  14.  
  15. int main() {
  16.  
  17. int size;
  18. double first[size];
  19. double average;
  20. double second[size];
  21.  
  22. infile >> size;
  23. readdata(size, first);
  24. cout << "Here is the original array." << endl;
  25. printarray(size, first);
  26. average = findaverage(size, first);
  27. cout << "The average is equal to" << average << endl;
  28. howfaraway(size, average, first, second);
  29. cout << "This is the new array for this program" << endl;
  30. printarray(size, second);
  31. cout << "The new average is equal to" << findaverage(size, second) << endl;
  32.  
  33. infile.close();
  34.  
  35. return 0;
  36. }
  37. // readdata will read in multiple data's into an array. The
  38. // data will be used throughout the program.
  39. void readdata(int n, double numbers[])
  40. {
  41. for(int i = 0; i <= n; i++){
  42. infile >> numbers[i];
  43. }
  44.  
  45. return;
  46. }
  47. // printarray will print an array of the numbers in rows of five.
  48. // After the five it goes tot he next row.
  49. void printarray(int q, double numbs[])
  50. {
  51. cout << "Here is the original array." << endl;
  52.  
  53. cout.setf(ios::fixed,ios::floatfield);
  54. cout.precision(2);
  55.  
  56. for(int i = 0; i <= q; i++){
  57. cout << numbs[i] << " ";
  58. if((i+1) % 5 == 0){
  59. cout << endl;
  60. }
  61. }
  62.  
  63. cout << endl;
  64.  
  65. return;
  66. }
  67. // findaverage will find the average of the total of the array numbers
  68. // and divide it by tht many numbers.
  69. double findaverage(int k, double p[])
  70. {
  71. int i;
  72. double sum;
  73. double avg;
  74.  
  75. for(i = 0; i <= k; i++){
  76. sum = sum + p[i];
  77. }
  78.  
  79. avg = sum / i;
  80.  
  81. return avg;
  82. }
  83. // howfaraway will find the difference between the the original arrays average and the
  84. // number itself.
  85. void howfaraway (int m, double avg, double r[], double s[])
  86. {
  87. for(int i = 0; i <= m; i++){
  88. s[i] = r[i] - avg;
  89. }
  90.  
  91. return;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement