L7_T2

 avatar
unknown
c_cpp
3 years ago
2.0 kB
7
Indexable
#include <iostream>
#include <cstdlib>
#include <math.h>

using namespace std;

void InitMatrix(int**, int, int);
void DeleteMatrix(int**, int); 
void PrintMatrix(int**, int, int);

int ProdOfRows(int**, int, int);
int SumOfColumns(int**, int, int);

int main()
{
    int n, m;
    
    cout << "Enter array dimensions: " << endl;
    cin >> n >> m;
    
    int **arr = new int* [n];
    for(int i = 0; i < n; i++)
        arr[i] = new int[m];
    
    InitMatrix(arr, n, m);
    PrintMatrix(arr, n, m);
    
    cout << "Product of row indices of elements that are greater than 8: " << ProdOfRows(arr, n, m) << endl;
    cout << "Sum of column indices of elements that are greater than 8: " << SumOfColumns(arr, n, m) << endl; 
    
    DeleteMatrix(arr, n);
    return 0;
}

int ProdOfRows(int **arr, int n, int m)
{
    int product = 1;
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(arr[i][j] > 8)
            {
                product *= i;
                //cout << endl << i << endl;
            }
        }
    }
    return product;
}
int SumOfColumns(int **arr, int n, int m)
{
    int sum = 0;
    
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(arr[i][j] > 8)
            {
                sum += j;
                //cout << endl << j << endl;
            }
        }
    }
    return sum;
}
void InitMatrix(int **arr, int n, int m)
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << "A[" << i << "][" << j << ']';
            cin >> arr[i][j];
            cout << endl;
        }
    }
}
void DeleteMatrix(int **arr, int n)
{
    for(int i = 0; i < n; i++)
        delete []arr[i];
    delete []arr;    
}
void PrintMatrix(int **arr, int n, int m)
{
    cout << endl;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << ' ' << arr[i][j];
        }
        cout << endl;
    }
    cout << endl;
}
Editor is loading...