### Need Help with 2D dynamic arrays and finding largest product of any four adjacent numbers in the array

Pages: 123
Hey guys, I need help with writing a program that creates a 2D dynamic array from a command line argument and then finds the largest product of any four adjacent numbers in the array.

You can read the problem here:
https://www.dropbox.com/s/uj2kd8a764v7txy/Problem%20Set.pdf

I have tried and and I still cant figure out where to get started. Please help me out here.

do you need to keep the 2D dynamic array organized, or you can mess it up?
hello,

thank you for responding, do you mean the order? Looking at the example in the problem set. I think it is okay if it is messy.
what I ment is, can you sort the 2d array so all the big numbers will be at one side of the array?
well I don't think so. It has to be random. Can you please help me out with the code?

Thank you
so you want to make the dimensions of the 2d array both the same and multiples of 2, like 8x8!

so make an 8x8 array of ints!

now make another array, a 2x2 int array representing the box that the numbers to be multiplied with be in.

then you need to make a loop that starts the box out in a bottom corner of the 8x8 and moves to the side, multiplying the numbers that are int the 2x2. then when the 2x2 gets to the edge, have it start 1 point up and at the side that it started at.

hope this helped!!!
hey, thank you for responding. I actually need some help with the code.

Thanks
sure, ill give u the code for a 2x2 square then ill show u what u need to change for a 4x4, and so on.
k?
thanks. looking forward to the code
actually ill do a 4x4 grid, and products of 2x2.

it may take a few days though.
It seems like you need code that reads in two integers. One integer represents the number of columns and one represents the number of rows in the 2D array. That sounds like what you are asking for.
Of course once these numbers are read in by the program you would want the 2D array to have the proper number of columns and rows. Is that what you were looking for?
Hey. Thanks for the reply. Yes that is correct. Since this should read command line arguments -rows -cols . We need to use arg v and arg c

https://www.dropbox.com/s/uj2kd8a764v7txy/Problem%20Set.pdf

once the array is created , then it has to calculate the largest product .

program should also display messages under "Error Handling section"

I opened the pdf. The assignment you have been given is your assignment and is meant as a learning exercise for you. That being said, you will not get many replies on this site when asking for complete solutions to what appears to be a homework problem. We are here to help with programming, not writing the complete program. So I have done a little bit of googling to get the bits and pieces that should help you get a good start with the code. When you get further along in the assignment and have some code already typed, I will be glad to help you with any other questions that may arise.

How to get integers from command line:
http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200401/notes/command-args.html

How to create a multidimensional dynamic array:
http://www.cplusplus.com/forum/beginner/63/

Using the variable argc inside of if statements will provide "error handling".

Your code is going to begin like the template you see here (also this link has more examples on how to use command line arguments):
http://wwwx.cs.unc.edu/~sparkst/howto/cpp_main.php

Try something as simple as printing the array to standard output with spaces in between every column and newlines at the end of every row. If you get all that going, then you can begin to tackle the largest product part of the assignment.
Last edited on
Hey thank you so much for the post. And glad you could help. As of now I have come up with this part.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include #include #include using namespace std; void create_array( void ); int main(){ cout << "WELCOME!" << endl; create_array(); return 0; } void create_array( void ) // Function that creates the Array { int col , row; cout << "Columns\n> " << flush; cin >> col; cout << "Rows\n> "<< flush; cin >> row; int array[row][col]; srand(time(NULL)); // Generate Random Numbers for( int i = 0; i

But the problem is that the program does not output the matrix according to the input. Sometimes it outputs more rows, or less columns. And also can you help me with the command line input and argv/arg c stuff. I went through the posts you sent me. Unfortunately I do not quite understand how to implement that here.

Thanks for helping
 ``123456`` `````` int col , row; cout << "Columns\n> " << flush; cin >> col; cout << "Rows\n> "<< flush; cin >> row; int array[row][col];``````

This is not legal C++. The dimensions of an array must be specified with compile time constants. You should probably turn off this compiler extension. Were it legal C++, one should remember that the valid indices of an array are 0 to it's size-1 so that `array[row][col]` would be an element that doesn't exist.
Last edited on
Within your for loop you would want to use i and j as the indices `array[i][j]`. row and col are constant on every iteration of the for loop statements. They are constant because no new values are assigned to them. However, i and j are increased by one every time. `++i` is basically equivalent to `i = 1 + i`. Of course you will want to change the indices on line 41 and line 43.

Also at some point you are going to need command line arguments so you should have `int main(int argc, char * argv[])` instead of `int main()`.

 ```WELCOME! Columns > 5 Rows > 8 58 22 58 10 96 25 98 62 28 5 93 21 67 72 5 16 45 80 15 1 96 76 24 24 51 90 88 27 65 30 68 78 90 95 25 74 18 83 26 30```

Your next step is going to be passing this array back to main so main can use it in another function. This other function is what finds the largest product, but it is going to need the array as input. I think if you can understand passing some normal variables by reference, it will help you in passing an array by reference.
http://www.cplusplus.com/doc/tutorial/functions2/

So now your goal should be making 2 functions and main. One function creates the array, then main takes the created array and passes it the other function, and for now just make the other function print out the array to see if it worked correctly. Then you can worry about making the other function find the largest product.

Edit:
I changed the code by swapping row and col, and leaving i and j in order.
 ``123456789101112131415`` ``````void create_array() { int col, row; cout << "Columns\n> " << flush; cin >> col; cout << "Rows\n> "<< flush; cin >> row; int array[row][col]; srand(time(NULL)); for( int i = 0; i

The setw modifier requires including this at the top: `#include <iomanip> `
Last edited on
Hey thanks for the post. I made the changes. But still the outputs is the same.

Here's the code.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include #include #include using namespace std; void create_array( void ); int main(int argc, char * argv[]){ cout << "WELCOME!" << endl; create_array(); return 0; } void create_array( void ) // Function that creates the Array { int col , row; cout << "Columns\n> " << flush; cin >> col; cout << "Rows\n> "<< flush; cin >> row; int array[row][col]; srand(time(NULL)); // Generate Random Numbers for( int i = 0; i

Output:
 ``` WELCOME! Columns > 3 Rows > 3 14 41 88 65 54 16 70 57 9 ```

If you had a space instead of an endl for in between every column on line 44. Also I noticed that rows and columns are swapped for some reason I was doing j before i.
Try out writing the functions that I mentioned in my previous post. If you need help writing functions, you can find a lot of help here on the tutorial section.
The tutorial section has two chapters dedicated to functions.
http://www.cplusplus.com/doc/tutorial/
so how can I fix the first problem? You referred to in line 44?
Change endl to a space character ---> `endl` to `" "`
Also I edited my post from earlier to show what I ran on my machine.
Last edited on
Pages: 123