Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. // Challenge 7
  2. // Number Analysis Program
  3. //
  4. // Program by: Sarah.
  5.  
  6. #include <iostream>
  7.  
  8. #include <string>
  9.  
  10. #include <fstream>
  11.  
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16. // Ask the user for a file name. Assume the file contains a series of
  17. // numbers, each written on a separate line. The program should read the
  18. // contents of the file into an array and then display the following data:
  19.  
  20. // – The lowest number in the array.
  21. // – The highest number in the array.
  22. // – The total of the numbers in the array.
  23. // – The average of the numbers in the array.
  24.  
  25. const int ARRAY_SIZE = 12; // Array size.
  26. int numbers[ARRAY_SIZE]; // Array with 12 elements.
  27. int count = 0; // Loop counter variable.
  28.  
  29. string filename; // For the user to enter the file name.
  30. ifstream inputFile; // Input the file stream object.
  31.  
  32. // Get the file name from the user.
  33.  
  34. cout << "Enter the name of the file you wish to open : ";
  35. cin >> filename;
  36.  
  37. inputFile.open(filename); // Open the file.
  38. // If the file successfully opened, process it.
  39.  
  40. if (inputFile)
  41. {
  42. while (count < ARRAY_SIZE && inputFile >> numbers[count])
  43. {
  44. count++;
  45. }
  46.  
  47. // Close the file.
  48. inputFile.close();
  49. }
  50. else
  51. {
  52. // Display an error message.
  53. cout << "Error opening the file.\n\n";
  54. }
  55.  
  56. // Display the numbers read.
  57.  
  58. int value = count;
  59.  
  60. cout << "There are " << value << " numbers in the array.\n\n";
  61.  
  62. cout << "The numbers are : ";
  63.  
  64. for (int index = 0; index < count; index++)
  65. {
  66. cout << numbers[index] << " ";
  67.  
  68. }
  69. cout << "\n";
  70.  
  71. // Display the sum of the numbers.
  72.  
  73. cout << "The sum of these numbers is : ";
  74.  
  75. int sum = 0; // Initialize accumulator.
  76. for (int count_Sum = 0; count_Sum < ARRAY_SIZE; count_Sum++)
  77. {
  78. sum += numbers[count_Sum];
  79. }
  80.  
  81. cout << sum << "\n\n";
  82.  
  83. // Display the average of the numbers.
  84.  
  85. cout << "The average of these numbers is : ";
  86.  
  87. double total = 0; // Initialize accumulator.
  88. double average; // To hold the average.
  89.  
  90. for (int count_Average = 0; count_Average < ARRAY_SIZE; count_Average++)
  91. {
  92. total += numbers[count_Average];
  93. }
  94.  
  95. average = total / ARRAY_SIZE;
  96.  
  97. cout << average << "\n\n";
  98.  
  99. // Display the highest value of the numbers.
  100.  
  101. cout << "The highest value of these numbers is : ";
  102.  
  103. int count_Highest;
  104. int highest;
  105.  
  106. highest = numbers[0];
  107. for (count_Highest = 1; count_Highest < ARRAY_SIZE; count_Highest++)
  108. {
  109. if (numbers[count_Highest] > highest)
  110. {
  111. highest = numbers[count_Highest];
  112. }
  113. }
  114. cout << highest << "\n\n";
  115.  
  116. // Display the lowest value of the numbers.
  117.  
  118. cout << "The lowest of these numbers is : ";
  119.  
  120. int count_Lowest;
  121. int lowest;
  122.  
  123. lowest = numbers[0];
  124. for (count_Lowest = 1; count_Lowest < ARRAY_SIZE; count_Lowest++)
  125. {
  126. if (numbers[count_Lowest] < lowest)
  127. {
  128. lowest = numbers[count_Lowest];
  129. }
  130. }
  131. cout << lowest << "\n\n";
  132.  
  133. return 0;
  134.  
  135. system("pause");
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement