ChameL1oN

Лаба5_Задача2(Вар.5)

Dec 17th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <locale>
  4. #include <time.h>
  5. #include <iomanip>
  6. using namespace std;
  7.  
  8. int* form_mas(int count){
  9. int* massive = new int[count];
  10. int a = 0;
  11.  
  12. while (a < count){
  13. massive[a] = rand() % 500 - 150;
  14. a++;
  15. }
  16. return massive;
  17. }
  18.  
  19. int** form_matr(int n, int m)
  20. {
  21. int **matr = new int*[n];//выделение памяти под массив указателей
  22. for (int i = 0; i < n; i++){
  23. //выделение памяти 100*sizeof(int) байт для массива значений
  24. matr[i] = form_mas(m);
  25. }
  26. return matr;//возвращаем указатель на массив указателей
  27. }
  28. void init_matr(int** matr, int n, int m){
  29. //Отсортировать по убыванию все строки матрицы.
  30. int r, a = 0, b = 0, j = 0,i=n-1;
  31. while (j < m){
  32. for (int i = 1; i<n; i++)
  33. for (int a = n - 1; a >= i; a--)
  34. if (matr[a][j] > matr[a - 1][j])
  35. {
  36. int r = matr[a][j];
  37. matr[a][j] = matr[a - 1][j];
  38. matr[a - 1][j] = r;
  39. }
  40. j++;
  41. }
  42. cout << "После обработки :" << endl;
  43. r = 0;
  44. a = 0;
  45. while (r < m){
  46. while (a < n){
  47. cout << setw(6) << matr[a][r] << " ";
  48. a++;
  49. }
  50. a = 0;
  51. cout << endl;
  52. r++;
  53. }
  54. }
  55.  
  56. void main()
  57. {
  58. int n, m, i = 0, j = 0;
  59. setlocale(LC_ALL, "rus");
  60. cout << "Введите кол-во столбцов в двумерном массиве" << endl;
  61. cin >> n;
  62. cout << "Введите кол-во строк в двумерном массиве" << endl;
  63. cin >> m;
  64. time_t t;
  65. srand((unsigned)time(&t));
  66. int** matr = form_matr(n, m);
  67. while (j < m){
  68. while (i < n){
  69. cout << setw(6) << matr[i][j] << " ";
  70. i++;
  71. }
  72. i = 0;
  73. cout << endl;
  74. j++;
  75. }
  76. init_matr(matr, n, m);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment