Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- void FillArr(int** arr, const int rows, const int cols) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- arr[i][j] = 1+rand()%(cols*rows);
- }
- }
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- for (int m = 0; m < rows; m++) {
- for (int l = 0; l < cols; l++) {
- if (j == l && i==m) {
- continue;
- }
- if (arr[i][j] == arr[m][l]) {
- arr[i][j] = 1+rand() % (cols*rows);
- m = -1;
- break;
- }
- }
- }
- }
- }
- }
- void shawArr(int** arr, const int rows, const int cols) {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- std::cout << arr[i][j] << "\t";
- }
- std::cout << std::endl;
- }
- }
- int sumArrVals(int** arr, const int rows, const int cols) {
- int val=0;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- val = val + arr[i][j];
- }
- }
- return val;
- }
- int main() {
- srand(time(NULL));
- setlocale(LC_ALL, "ru");
- int rows, cols;
- std::cout << "Введите количество строк массива ";
- std::cin >> rows;
- std::cout << "Введите количество столбов массива ";
- std::cin >> cols;
- int **arr = new int* [rows];
- for (int i = 0; i < rows; i++) {
- arr[i] = new int[cols];
- }
- FillArr(arr, rows, cols);
- shawArr(arr, rows, cols);
- std::cout << "Сумма элементов массива " << rows << " x " << cols << " равна " << sumArrVals(arr, rows, cols) << std::endl;
- for (int i = 0; i < rows; i++) {
- delete[] arr[i];
- }
- delete[] arr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement