Hello Stackoverflow! Today I have a question about getting started with an assignment I have. The objective of the assignment is to create a function, find_greatest_product(), that takes an array argument and returns the max product of four adjacent numbers that can be up, down, left, right, diagonally, or in a box formation in the N × M grid/matrix.
What I already have posted is my exact code that creates an array and populates it with a random number from 0 to 50 for the sake of testing. What I don't know is how to create an algorithm that can scan the entire array for the objective. I have a method that can find the greatest product in a 2x2 box, but the straight and diagonal lines are proving difficult. Any help, input, or suggestions are very welcome. Here is my pre-exsisting code:
int find_greatest_product(int **newArray, int &r, int &c){
//find the greatest product of 4 adjacent numbers, in any direction.
}
int test_array(){
//error handling
}
int print_array(int **newArray, int &r, int &c){
for (int i = 0; i < c; ++i)
{
for (int j = 0; j < r; ++j)
{
cout << newArray[i][j] << ' ';
}
cout << endl;
}
}
int create_array(int r, int c){
srand((unsigned)time(0));
int** newArray = new int*[r];
for (int i = 0; i < r; ++i)
{
newArray[i] = new int[c];
for (int j = 0; j < c; ++j)
newArray[i][j] = rand() % 50 + 1;
}
print_array(newArray, r, c);
}
int main(int argc, char* argv[]) {
int rows, col;
cout << "enter rows : " << endl;
cin >> rows;
cout << "enter cols : " << endl;
cin >> col;
create_array(rows, col);
}
Also, for those interested, here is the original problem statement: