I have an assignment that requires the following:
-Take command line input for rows and columns and dynamically create a 2-D array filled with random numbers
-Create a function called find_greatest_product to find the largest product of four adjacent numbers in the array. The four adjacent numbers can be any configuration of the shapes found in the game Tetris in the array. Your function needs to return the max product, starting position, shape, and direction of the four numbers using a struct.
Yes, it's a totally useless program purely to practice 2-D arrays.
I've got my array set up so I started with the easiest shape: the box. However, when I try to access the array full of random numbers, the product and factors all seem to be 0. Any hints as to why I'm unable to access the ints within the array of random numbers to find the product? The relevant bits of code are below. You can assume all functions not copied here work fine.
struct shape {
int highest;
int factors[4];
int startRow;
int startColumn;
} tShape, sShape, iShape, boxShape;
int main(int argc, char* argv[]) {
if(argc == 5) {
for(int i = 1; i < argc; i++) {
rows = getArg(argc, argv, i, compare1);
}
for(int i = 1; i < argc; i++) {
columns = getArg(argc, argv, i, compare2);
}
}
int ** array = new int*[rows];
int i, j;
for (i = 0; i < rows; i++) {
array[i] = new int[columns];
}
create_array(array, rows, columns);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << array[i][j];
cout << " ";
}
cout << endl;
}
boxProduct(array, rows, columns, boxShape);
cout << boxShape.highest << endl;
for (int i = 0; i < 4; i++) {
cout << boxShape.factors[i];
cout << " ";
}
cout << endl;
return 0;
}
void boxProduct(int *array[], int rows, int columns, shape boxShape) {
int highest = 0;
int product = 0;
for (int i = 0; i < rows - 1; i++) {
for (int j = 0; j < columns - 1; j++) {
product = (array[i][j]*array[i][j+1]*array[i+1][j]*array[i+1][j+1]);
if (product > highest) {
boxShape.highest = product;
boxShape.factors[0] = array[i][j];
boxShape.factors[1] = array[i][j+1];
boxShape.factors[2] = array[i+1][j];
boxShape.factors[3] = array[i+1][j+1];
}
}
}
}
Here is a sample output with a matrix 10 rows x 5 columns:
27 86 4 41 44
17 6 5 40 32
42 58 14 95 53
8 28 95 27 91
63 22 27 49 2
38 37 39 37 76
9 17 14 13 10
10 30 16 67 22
49 10 33 63 5
86 71 86 34 50
0 <- product
0 0 0 0 <- the four factors
shape boxShapein the boxProduct function, so don't see the change