0

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
1
  • 4
    You are modifying a copy of shape boxShape in the boxProduct function, so don't see the change Commented Nov 20, 2013 at 13:13

1 Answer 1

1

C and C++ functions are call by value by default, not call by reference. That is, the compiler makes copies of the arguments to give to functions, and if the function modifies its arguments, it modifies a copy.

Consider this example:

void foo( int x )
{
    x++;  // increments foo's own local copy of 'x'
}

int main()
{
    i = 42;
    cout << i << endl;   // prints 42
    foo(i);
    cout << i << endl;   // ALSO prints 42!

    return 0;
}

This will print 42 twice, because foo modifies a copy.

If you modify the code slightly, you tell the C++ compiler to pass the argument by reference. (Note: This is a C++ only feature; it does not work in C.) Now, any modification to the argument inside the function will modify the value the caller sees also:

void foo( int& x )  // The & means "pass this parameter by reference"
{
    x++;
}

int main()
{
    i = 42;
    cout << i << endl;  // prints 42
    foo(i);
    cout << i << endl;  // prints 43

    return 0;
}

An alternate way to modify a value held by the caller is to pass a pointer to that value, rather than the value itself. This is still call by value, but in this case the value you pass the function is a pointer. Example:

void foo( int* x )  // x is now a pointer to integer
{
    (*x)++;  // The (*x) dereferences the pointer.  What happens if you leave off the parens?
}

int main()
{
    i = 42;
    cout << i << endl;  // prints 42
    foo(&i);            // the & there takes the address of 'i' and passes that to foo()
    cout << i << endl;  // prints 43

    return 0;
}

Because C does not support call-by-reference arguments, it requires this last scheme. Some C++ code also uses pointers in this way. Modern C++ style tends to avoid bare pointers wherever possible, but you'll still see pointers from time to time.

Punchline: You'll want to apply this knowledge to your shape boxShape structure above. You either want to pass shape boxShape by reference, or pass a pointer to shape boxShape. Both are valid approaches, although C++ tends to prefer passing a reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.