Advertisement
klasscho

Untitled

Dec 9th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main();
  7. float Average(int** matrix, int n);
  8. void AverageOutput(float* Arr);
  9. void outputMatrix(int** matrix, int n);
  10. void saveResult(string result);
  11.  
  12. int main()
  13. {
  14. setlocale(LC_ALL, "Russian");
  15. int n;
  16. int** a;
  17. bool FirstIt = true;
  18. bool SecondIt = true;
  19. bool TrirdIt = true;
  20. ifstream in("findmax.txt");
  21.  
  22. if (in.is_open())
  23. {
  24. try
  25. {
  26. if (!in.eof())
  27. {
  28. in >> n;
  29. }
  30. else
  31. {
  32. cout << "Ошибка при чтении!Проверьте правильность текстового файла." << endl;
  33. FirstIt = false;
  34. }
  35.  
  36. a = new int* [n];
  37. for (int i = 0; i < n; i++)
  38. {
  39. a[i] = new int[n];
  40. }
  41.  
  42. for (int i = 0; i < n; i++)
  43. {
  44. for (int j = 0; j < n; j++)
  45. {
  46. if (!in.eof())
  47. {
  48. in >> a[i][j];
  49. }
  50. else
  51. {
  52. cout << "Ошибка при чтении матрицы!Проверьте правильность ее написания" << endl;
  53. SecondIt = false;
  54. }
  55. }
  56. }
  57. }
  58. catch (ios_base::failure& f1)
  59. {
  60. cout << "Ошибка чтения из файла" << endl;
  61. exit(1);
  62. }
  63. in.close();
  64. }
  65. else
  66. {
  67. cout << "Ошибка. Проблема при открытии файла!" << endl;
  68. TrirdIt = false;
  69. }
  70.  
  71. return 0;
  72. }
  73.  
  74. float* Average(int** Matrixx, int n)
  75. {
  76. float Sum, Amount;
  77. float* Arr = new float[n];
  78. for (int i = 0; i < n; i++) {
  79. Sum = 0;
  80. Amount = 0;
  81. for (int j = 0; j < n; j++) {
  82. if (Matrixx[j][i] > 0) {
  83. Sum += Matrixx[j][i];
  84. Amount += 1;
  85. }
  86. Arr[i] = Sum / Amount;
  87. }
  88. }
  89. return Arr;
  90. }
  91.  
  92. void AverageOutput(float* Arr) {
  93. for (int i = 0; i < sizeof(Arr); i++) {
  94. printf("%.4f ", Arr[i]);
  95. }
  96. }
  97.  
  98.  
  99. void saveResult(string result)
  100. {
  101. bool AttemptOne = true;
  102. bool AttemptTwo = true;
  103. try
  104. {
  105. ofstream output;
  106. output.open("max.txt");
  107. if (output.is_open())
  108. {
  109. output << result;
  110. output.close();
  111. }
  112. else
  113. {
  114. cout << "Ошибка. Проблема при записи результата!" << endl;
  115. AttemptOne = false;
  116. }
  117. }
  118. catch (ios_base::failure& f1)
  119. {
  120. cout << "Ошибка. Проблема при записи результата!" << endl;
  121. AttemptTwo = false;
  122. }
  123. }
  124.  
  125. void outputMatrix(int** matrix, int n)
  126. {
  127. for (int i = 0; i < n; i++)
  128. {
  129. for (int j = 0; j < n; j++)
  130. {
  131. cout << matrix[i][j] << "\t";
  132. }
  133. cout << "\n";
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement