ChameL1oN

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

Dec 17th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <locale>
  4. #include <time.h>
  5. #include <iomanip>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11.  
  12. void Print_Matr(int** matr, int n, int m){ //Функция для печати матрицы
  13. int i = 0, j = 0;
  14. while (j < m){
  15. while (i < n){
  16. cout << setw(6) << matr[i][j] << " ";
  17. i++;
  18. }
  19. i = 0;
  20. cout << endl;
  21. j++;
  22. }
  23. }
  24.  
  25. int* form_mas(int count){
  26. int* massive = new int[count];
  27. int a = 0;
  28.  
  29. while (a < count){
  30. massive[a] = rand() % 10 - 5;
  31. a++;
  32. }
  33. return massive;
  34. }
  35.  
  36. int** form_matr(int n, int m)
  37. {
  38. int **matr = new int*[n]; //выделение памяти под массив указателей
  39. for (int i = 0; i < n; i++){
  40.  
  41. matr[i] = form_mas(m);
  42. }
  43. return matr;//возвращаем указатель на массив указателей
  44. }
  45.  
  46. void dobav_str(int** matr, int n, int m){
  47. // Добавить заданный одномерный массив после всех строк, начинающихся на отрицательное значение
  48. int m_new = m, i = 0, j = 0, a = 0, k = 0, b = 0;
  49. int* zadan_mas = new int[n];
  50. for (j = 0; j < n; j++){ // Формируем "заданный массив"
  51. cout << "Введите [" << j << "] элемент для заданного массива" << endl;
  52. cin >> zadan_mas[j];
  53. }
  54. cout << endl;
  55. cout << endl;
  56. if (m % 2 == 0){
  57. while (i < m / 2 + b){
  58. for (j = 0; j < n; j++){
  59. a += matr[j][i];
  60. }
  61. if (a == 0){
  62. b++;
  63. k = m;
  64. while (k > i + 1){
  65. for (j = 0; j < n; j++){
  66. matr[j][k] = matr[j][k - 1];
  67. }
  68. k--;
  69. }
  70. for (j = 0; j < n; j++){
  71. matr[j][i + 1] = zadan_mas[j];
  72. }
  73.  
  74. }
  75.  
  76. i++;
  77. }
  78. }
  79. else{
  80. while (i < m / 2 + b){
  81. for (j = 0; j < n; j++){
  82. a += matr[j][i];
  83. }
  84. if (a == 0){
  85. b++;
  86. k = m;
  87. while (k > i + 1){
  88. for (j = 0; j < n; j++){
  89. matr[j][k] = matr[j][k - 1];
  90. }
  91. k--;
  92. }
  93. for (j = 0; j < n; j++){
  94. matr[j][i + 1] = zadan_mas[j];
  95. }
  96.  
  97. }
  98.  
  99. i++;
  100. }
  101.  
  102. }
  103.  
  104.  
  105. j = 0;
  106. i = 0;
  107. cout << endl;
  108. cout << "Матрица после обработки : " << endl;
  109. cout << endl;
  110. Print_Matr(matr, n, m / 2 + b);
  111.  
  112. }
  113.  
  114.  
  115. void main()
  116. {
  117. int n, m, i = 0, j = 0;
  118. setlocale(LC_ALL, "rus");
  119. cout << "Введите кол-во столбцов в двумерном массиве" << endl;
  120. cin >> n;
  121. cout << "Введите кол-во строк в двумерном массиве" << endl;
  122. cin >> m;
  123. time_t t;
  124. srand((unsigned)time(&t));
  125. int** matr = form_matr(n, m * 2);
  126. cout << "Исходная матрица : " << endl;
  127. Print_Matr(matr, n, m);
  128. dobav_str(matr, n, m * 2);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment