# Need Help with 2D arrays and functions

• May 31st, 2013, 06:47 PM
Jinjaninja1
Need Help with 2D arrays and functions
Hey guys, I'm trying to do this problem set. You can read it here

https://www.dropbox.com/s/uj2kd8a764...blem%20Set.pdf

Basically I have already created the function that takes command line arguments and creates the dynamic array.

And the array/matrix works fine. Now Im trying modify the function that finds the largest product of 4 adjacent numbers. Code for that will be posted below.

I need help linking these two functions together so that find_greatest_product function would take user input from create_array function and would find the largest product.

Basically what I'm asking is to help me modify it.

P.S We cannot use vectors for this assignment.

Code:

```#include <iostream> #include <cstdlib> #include <string> #include <ctime> #include <iomanip> using namespace std; int ** create_array(int rows, int cols); void fillArray(int ** a, int rows, int cols); void showArray(int ** a, int rows, int cols); void find_greatest_product (void); int main(int argc, char * argv[]) {     for (int i=0; i<argc; i++)         cout << i << " " << argv[i] << endl;                 int rows = atoi(argv[2]);     int cols = atoi(argv[4]);         cout << "rows = " << rows << endl;     cout << "cols = " << cols << endl;         int ** array = create_array(rows, cols); // Allocate the array     fillArray(array, rows, cols);            // Put in some values     showArray(array, rows, cols);            // Display the contents         return 0; } int ** create_array(int rows, int cols) {     int ** arr = new int * [rows];     for (int i=0; i<rows; i++)         arr[i] = new int [cols];         return arr; } void fillArray(int ** a, int rows, int cols) {     srand(time(NULL)); // Generate Random Numbers         int k = 0;     for (int i=0; i< rows; i++)         for (int j=0; j<cols; j++)             a[i][j] = k++;     } void showArray(int ** a, int rows, int cols) {     srand(time(NULL)); // Generate Random Numbers         for (int i=0; i< rows; i++)     {         for (int j=0; j<cols; j++)                         cout << setw(3) << (a[i][j] =(rand() % 99) + 1) << " ";                 cout << endl;     } } void find_greatest_product (void) // Function Needs To be Modified So that it takes input from create_array function and finds the largest product of four adjacent numbers {         char line[10];     int a[i][j];         for(int i =0 ; i<20; i++) {         for(int j=0; j<20; j++) {             tmpfile.getline(line, 10, ' ');             a[i][j] = atof(line);             cout<<a[i][j]<<" ";         }         cout<<endl;     }         double max=1;     double prod;         // rows     for(int i=0;i<20; i++) {         for(int j=0; j<17; j++) {             prod = a[i][j] * a[i][j+1] * a[i][j+2] * a[i][j+3];             if(max < prod) {                 max = prod;             }         }     }         // column     for(int j =0; j<20;j++) {         for(int i=0;i<17; i++) {             prod = a[i][j] * a[i+1][j] * a[i+2][j] * a[i+3][j];             if(max < prod) {                 max = prod;             }         }     }         // diagonal right top to bottom left     // exclude 3 from 20 as it never can make it to 4 elements     for(int i=0; i< 17;i++) {         for(int j=0; j<17;j++) {             prod = a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3];             if(max < prod)                 max = prod;         }     }         // diagonal left top to right bottom     for(int i=3; i< 20;i++) {         for(int j=0; j<17;j++) {             prod = a[i][j]*a[i-1][j+1]*a[i-2][j+2]*a[i-3][j+3];             if(max < prod)                 max = prod;         }     } }```
• May 31st, 2013, 06:52 PM
Paul McKenzie
Re: Need Help with 2D arrays and functions
Quote:

Originally Posted by Jinjaninja1
Hey guys, I'm trying to do this problem set. You can read it here

https://www.dropbox.com/s/uj2kd8a764...blem%20Set.pdf

Basically I have already created the function that takes command line arguments and creates the dynamic array.

You need to destroy the array at the end. Without that, you have a memory leak, and you also won't show your teacher you know how to dispose of the allocated memory.
Quote:

I need help linking these two functions together so that find_greatest_product function would take user input from create_array function and would find the largest product.
Aren't you just supposed to pass the int** to the function and work with it inside that function?

Regards,

Paul McKenzie
• May 31st, 2013, 07:01 PM
Jinjaninja1
Re: Need Help with 2D arrays and functions
Quote:

Originally Posted by Paul McKenzie
You need to destroy the array at the end. Without that, you have a memory leak, and you also won't show your teacher you know how to dispose of the allocated memory.
Aren't you just supposed to pass the int** to the function and work with it inside that function?

Regards,

Paul McKenzie

Hey Paul, thank you for the reply. Yes that is exactly what I'm supposed to do. I'm new to the whole programming area and this is the first language I'm learning. So I'm sorry if my last post didn't make any sense. I'm not so familiar with the terms.

Anyways, can you please show me how to do it. I've been researching all day, and I can't figure out how to do that on my own. And also there might be some unnecessary code in the find_greatest_product function. I'd be glad if you can also remove that, and post the modified code below,

• May 31st, 2013, 10:14 PM
Paul McKenzie
Re: Need Help with 2D arrays and functions
Quote:

Originally Posted by Jinjaninja1
Anyways, can you please show me how to do it. I've been researching all day, and I can't figure out how to do that on my own.

You pass the int** just like you did in the fillArray and showArray functions. You also need to pass the number of rows and columns.
Code:

`void find_greatest_product (int** a, int rows, int cols)`
You need to get rid of that local a[][] array inside of that function, since the array is being passed to that function.

Regards,

Paul McKenzie
• May 31st, 2013, 10:32 PM
Jinjaninja1
Re: Need Help with 2D arrays and functions
Quote:

Originally Posted by Paul McKenzie
You pass the int** just like you did in the fillArray and showArray functions. You also need to pass the number of rows and columns.
Code:

`void find_greatest_product (int** a, int rows, int cols)`
You need to get rid of that local a[][] array inside of that function, since the array is being passed to that function.

Regards,

Paul McKenzie

Hey thank you for the reply. I just got rid of the a[][]

Code:

`char line[10];`
and

Code:

`tmpfile.getline(line, 10, ' ');`
Can you also show me how to pass int** and where to insert cout statements so that the function will output the max product based on the user input?

Thanks once again
• June 1st, 2013, 07:01 AM
2kaud
Re: Need Help with 2D arrays and functions
Code:

```int rows = atoi(argv[2]); int cols = atoi(argv[4]);```
What happens if argc < 5? You haven't tested for the error conditions mentioned in the assignment.

Your fillArray sets the initial values. But your showArray also sets values in the array! Shouldn't showArray just show the values and not also change values?

Code:

`tmpfile.getline(line, 10, ' ');`
This will read a line from the file stream associated with tmpfile. But you don't open a file anywhere! From where did you get this code for find_greatest_product? Its based upon a 20x20 array read from a file - whereas for your program the size of the array is entered on the command line and fillArray sets the values of the array rather than reading from a file! You need to get rid of this code that reads from a file and change the loop conditions to refer to the rows and cols variables passed to the function as indicated in Paul's post.

Quote:

where to insert cout statements so that the function will output the max product based on the user input?
When the function has finally calculated the maximum value!