L7_T1

 avatar
unknown
c_cpp
2 years ago
1.3 kB
6
Indexable
#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;

const int N = 100;

void InitMatrix(int (&)[N][N], int, int);
void PrintMatrix(int (&)[N][N], int, int);

int GetProductInRange(int (&)[N][N], int, int);


int main()
{
    int n, m;
    int matrix[N][N];
    
    cout << "Enter matrix dimensions" << endl;
    cin >> n >> m;
    
    InitMatrix(matrix, n, m);
    PrintMatrix(matrix, n, m);
    
    cout << "Product of elements in range [10, 50]: " << GetProductInRange(matrix, n, m);
    
    return 0;
}

int GetProductInRange(int (&A)[N][N], int n, int m)
{
    int product = 1;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            int currAbs = abs(A[i][j]);
            if(currAbs >= 10 && currAbs <= 50)
                product *= currAbs;
        }
    }
    return product;
}

void InitMatrix(int (&A)[N][N], int n, int m)
{
    srand(time(0));
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            A[i][j] = 50 - rand() % 100;
        }
    }
}
void PrintMatrix(int (&A)[N][N], int n, int m)
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << ' ' << A[i][j];
        }
        cout << endl;
    }
}
Editor is loading...