Bob103

матрица

Jun 23rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. ifstream in("input.txt");
  8. ofstream out("output.txt");
  9.  
  10. void sort(int *a, int n) //Пузырек
  11. {
  12. for (int i = 0; i < n - 1; i++)
  13. for (int j = n - 1; j > i; j--)
  14. if (a[j] < a[j - 1])
  15. swap(a[j], a[j - 1]);
  16. }
  17.  
  18. int main()
  19. {
  20. int n;
  21. in >> n;
  22.  
  23. int **mas = new int *[n]; //создали 2-ный массив
  24. for (int i = 0; i < n; i++)
  25. mas[i] = new int[n];
  26. for (int i = 0; i < n; i++) //записали числа из файла в массив
  27. for (int j = 0; j < n; j++)
  28. in >> mas[i][j];
  29.  
  30. for (int i = 0; i < n; i++)
  31. {
  32. int *b = new int[n]; //создали вспомогательный массив для сортировки
  33. for (int j = 0; j < n; j++)
  34. b[j] = mas[j][i];
  35. sort(b, n); //проводим сортировку Пузырьком
  36. for (int j = 0; j < n; j++) //из вспомогательного обратно в основной
  37. mas[j][i] = b[j];
  38. delete b;
  39. }
  40.  
  41. out << n << '\n';
  42. for (int i = 0; i < n; i++) //выводим массив в файл
  43. {
  44. for (int j = 0; j < n; j++)
  45. out << setw(5) << mas[i][j];
  46. out << '\n';
  47. }
  48.  
  49. in.close();
  50. out.close();
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment