ketronix

 avatar
ketronix
c_cpp
2 years ago
2.5 kB
4
Indexable
#include <iostream>
#include <ctime>
#include <math.h>

using namespace std;


int **create(int row, int col) {
    srand((unsigned int) time(NULL));
    int **mas = new int *[row];
    for (int i = 0; i < row; i++) {
        mas[i] = new int[col];
    }
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++) {
            mas[i][j] = -100 + rand() % 200;
        }
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            cout << mas[i][j] << '\t';
        }
        cout << endl;
    }
    return mas;
}

void c_create(int mas[10][10], const int row = 10, const int col = 10) {
    srand((unsigned int) time(NULL));
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col; j++) {
            mas[i][j] = -100 + rand() % 200;
        }
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            cout << mas[i][j] << '\t';
        }
        cout << endl;
    }
}


int num(int **mas, int row, int col) {
    int n = 0;
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col;) {
            if (mas[i][j] > 0) {
                n++;
                break;
            } else {
                j++;
            }
        }
    return n;
}

int num_stat(int mas[10][10], int row, int col) {
    int n = 0;
    for (int i = 0; i < row; i++)
        for (int j = 0; j < col;) {
            if (mas[i][j] > 0) {
                n++;
                break;
            } else {
                j++;
            }
        }
    return n;
}


int main() {

    int a;
    cout << "Використати розмір масиву за замовченням? (так - 1/ні - 0): ";
    cin >> a;
    switch (a) {
        case 0: {
            int row, col;
            cout << "Введіть кількість строк : ";
            cin >> row;
            cout << "Введіть кількість стовбців : ";
            cin >> col;

            int **m = create(row, col);
            cout << "Кількість строк де є хоч один позитивний елемент : ";
            cout << num(m, row, col);
            break;
        }
        case 1: {
            const int row = 10, col = 10;
            int mas[10][10];
            c_create(mas);

            cout << "Кількість строк де є хоч один позитивний елемент : ";
            cout << num_stat(mas, row, col) << endl;
        }
    }
}
Editor is loading...