Guest User

Untitled

a guest
Jul 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream> //for inputting a file
  4. using namespace std;
  5.  
  6. int sumOfDataItems, numberOfDataItems, count, printRows, lastRow, rowCount, colCount;
  7. vector<int> v1;
  8. int numbersArray[1300];
  9.  
  10. int readInput(int& totalNum)
  11. {
  12. ifstream inputFile; //File stream object
  13.  
  14. int sum, data; //define variables
  15.  
  16. inputFile.open("C:\\data\\TopicFin.txt"); //open the file
  17. if (!inputFile)//test for errors
  18. cout << "Cannot open TopicFin.txt. Program Ending\n";
  19. else
  20. {
  21.  
  22. //initialize statistics
  23. totalNum = 0;
  24. sum = 0;
  25.  
  26. //loop until the end of the file
  27. while (inputFile >> data)
  28. {
  29. v1.push_back(data);
  30. //compute statistics
  31. sum += data; //update sum
  32.  
  33. totalNum++;
  34. }
  35.  
  36. inputFile.close(); //close the input file
  37.  
  38. return (sum);
  39. }
  40. }
  41.  
  42. void sortArray(int array[], int size)
  43. {
  44. bool swap;
  45. int temp;
  46.  
  47. do
  48. {
  49. swap = false;
  50. for (int count = 0; count < (size - 1); count++)
  51. {
  52. if (array[count] > array[count +1])
  53. {
  54. temp = array[count];
  55. array[count] = array[count +1];
  56. array[count + 1] = temp;
  57. swap = true;
  58. }
  59. }
  60. } while (swap);
  61. }
  62.  
  63. int main()
  64. {
  65. sumOfDataItems = readInput(numberOfDataItems);
  66. if (sumOfDataItems == 0) //check for no data
  67. {
  68. cout << "Error - no input file data" << endl;
  69. }
  70. else
  71. {
  72. printRows = numberOfDataItems / 15;
  73. lastRow = numberOfDataItems % 15;
  74. cout << "The values read are:" << endl;
  75. for (rowCount = 0; rowCount < printRows; rowCount++)
  76. {
  77. for (colCount=0; colCount<15; colCount++)
  78. {
  79. cout << v1[count] << " ";
  80. count++;
  81. }
  82. cout << endl;
  83. }
  84. for (colCount=0; colCount<lastRow; colCount++)
  85. {
  86. cout << v1[count] << " ";
  87. count++;
  88. }
  89. cout << endl;
  90. for (count=0; count<numberOfDataItems; count++)
  91. numbersArray[count] = v1[count];
  92. sortArray( numbersArray, numberOfDataItems );
  93. cout << "\n\nThe average of values is " << (sumOfDataItems / numberOfDataItems) << endl;
  94. cout << numberOfDataItems << " Values were read" << endl << endl;
  95. cout << "The sorted result is\n";
  96. for (rowCount = 0; rowCount < printRows; rowCount++)
  97. {
  98. for (colCount=0; colCount<15; colCount++)
  99. {
  100. cout << v1[count] << " ";
  101. count++;
  102. }
  103. cout << endl;
  104. }
  105. for (colCount=0; colCount<lastRow; colCount++)
  106. {
  107. cout << numbersArray << " ";
  108. count++;
  109. }
  110. cout << endl;
  111. }
  112.  
  113. return 0;
  114. }
Add Comment
Please, Sign In to add comment