klasscho

Untitled

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