-1

So I've been trying to get a 2d array into a function, I've tried a lot of things but they don't seem to work, so if anyone could help or explain how to do it, I'd appreciate that.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void product(int arr[][]);

int main () {
    srand(time(0));
    int const rows = 4;
    int const cols = 4;
    int product = 1;
    
    
    int arr[rows][cols];
    
    for (int i=0; i<rows; i++)
        for (int j=0; j<cols; j++)
            arr[i][j] = 1 + (rand() % 20); 
    
    cout << "Random 2d generated array:" << endl;
    for (int i=0; i<rows; i++){
        for (int j=0; j<cols; j++)
            cout << arr[i][j] << " ";
        cout << endl;
    }
    
    
}

void product(int arr[][]) {
    
    
    for(int i=0;i<rows; i++) {
        product = product * arr[i][rows-i-1];
            
    }
    
    cout << "Product of right diagonal: " << product;
    
}

kegggx
  • 1

0 Answers0