Advertisement
tranerius

15. Двумерный динам массив с уник значениями + проверка

Dec 11th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. void FillArr(int** arr, const int rows, const int cols) {
  4.     for (int i = 0; i < rows; i++) {
  5.         for (int j = 0; j < cols; j++) {
  6.             arr[i][j] = 1+rand()%(cols*rows);
  7.         }
  8.     }
  9.     for (int i = 0; i < rows; i++) {
  10.         for (int j = 0; j < cols; j++) {
  11.             for (int m = 0; m < rows; m++) {
  12.                 for (int l = 0; l < cols; l++) {
  13.                     if (j == l && i==m) {
  14.                         continue;
  15.                     }
  16.                     if (arr[i][j] == arr[m][l]) {
  17.                         arr[i][j] = 1+rand() % (cols*rows);
  18.                         m = -1;
  19.                         break;
  20.                     }
  21.                 }
  22.             }
  23.         }
  24.     }
  25. }
  26. void shawArr(int** arr, const int rows, const int cols) {
  27.     for (int i = 0; i < rows; i++) {
  28.         for (int j = 0; j < cols; j++) {
  29.             std::cout << arr[i][j] << "\t";
  30.         }
  31.         std::cout << std::endl;
  32.     }
  33. }
  34. int sumArrVals(int** arr, const int rows, const int cols) {
  35.     int val=0;
  36.     for (int i = 0; i < rows; i++) {
  37.         for (int j = 0; j < cols; j++) {
  38.             val = val + arr[i][j];
  39.         }
  40.     }
  41.     return val;
  42. }
  43. int main() {
  44.     srand(time(NULL));
  45.     setlocale(LC_ALL, "ru");
  46.     int rows, cols;
  47.     std::cout << "Введите количество строк массива ";
  48.     std::cin >> rows;
  49.     std::cout << "Введите количество столбов массива ";
  50.     std::cin >> cols;
  51.     int **arr = new int* [rows];
  52.     for (int i = 0; i < rows; i++) {
  53.         arr[i] = new int[cols];
  54.     }
  55.     FillArr(arr, rows, cols);
  56.     shawArr(arr, rows, cols);
  57.     std::cout << "Сумма элементов массива " << rows << " x " << cols << " равна " << sumArrVals(arr, rows, cols) << std::endl;
  58.     for (int i = 0; i < rows; i++) {
  59.         delete[] arr[i];
  60.     }
  61.     delete[] arr;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement