Question #37627

Write a function in C++ to print the product of each column of a two dimensional integer array passed as the argument of the function.
1

Expert's answer

2013-12-10T10:59:46-0500

Answer on Question#37627, Programming, C++


#include<iostream>
using namespace std;
void product(int *arr[2], int number); // prototype of function product
void product(int *arr[2], int number) { // function product
    int product = 1;
    for (int i = 0; i < 2; i++) {
        product = 1;
        for (int j = 0; j < number; j++) {
            product *= arr[j][i];
        }
        cout << "Product of " << i + 1 << " column : " << product;
        cout << endl;
    }
}
int main() {
    int number;
    cout << "Please enter amount of numbers in array : ";
    cin >> number;
    cout << endl;
    int **arr = new int *[number]; // allocate memory for array
    for (int i = 0; i < number; i++) {
        arr[i] = new int[2];
        for (int j = 0; j < 2; j++) {
            cout << "Please enter the numbers of the " << i + 1 << " column : ";
            cin >> arr[i][j]; // input array
            cout << endl;
        }
    }
    product(arr, number); // pass the array and number of numbers to the function
    delete []arr; // clear memory
    system("pause");
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS