2D arrays/greatest product

Hi,
So I need to write one of those greatest product codes where the user enters the number of rows and columns, and they are to be filled with random numbers. I'm not to concerned with the greatest product part quite yet as I'm looking for a bit of guidance on the creating the 2D part to get me started. So I'm totally getting how I should go about it. I know I need a couple for loops to initialize the arrays and I think I might be able to figure that part out. I want to use pointers so that I can work off of the heap since my teacher recommended always running off of the heap if we can.
So I need something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cstdlib>

using namespace std;

void col_row_input( int *rows, int *columns )
{
     cout << "Enter number of rows: ";
     cin >> rows;
     out << "Enter number of columns: ";
     in >> columns;
}

int array( int *rows, int *columns )
{
     for ( int i = 0; i < rows; i++ )
     {
          for ( int j = 0; j < columns; j++ )
          {
               something?
          }
     }
}

int main( int argc, char *argv[] )
{
     int *rows, *columns;

     col_row_input( rows, columns );

     cout << rows << "    " << columns << endl;
}


Thats all I got so far. I have yet to accomplish working with pointers and I know I'm doing it wrong in the code above but if someone could give me some pointers (hehe, get it?) I would be ever so grateful.
If you are gonna use pointers, use * when you want the value of the pointer. So you want to use cin >> *rows instead of cin >> rows , (same thing with cout and the for loop, however leave line 29 as it is).

As a good tip, don't use pointers to numerical values because it is useless. Pointers are mainly used for classes. So probably the only time you would need a pointer to a numerical value is for arrays. Otherwise just use stack memory.

And for the random number generator you can take a look at how to do it here: https://www.youtube.com/watch?v=j5z5ZF8oilk&index=14&list=PL2DD6A625AD033D36
Hmmm, ok, so actually I think I may have been misled into thinking I needed pointers. I am working with arrays but I'm seeing a lot of people using things like for example array[x][y]. But that's still just a 1D array right? Something I saw did a 2D array like this:
1
2
3
4
5
6
7
8
int matrix()
{
     int **array = new int *[rows]
     for ( int i = 0; i < rows; ++i )
     {
           array[i] = new int [columns];
     }
}


This looked more like what I've seen my teacher do so I changed my "array()" function above to this. But then I'm still confused because the [rows] and [columns] part don't quite look right to me. Doesn't it need to be named like array[rows] and array[columns]? I guess I'm more lost than I originally thought and I'm still not sure the best way of how to go about passing values/references/pointers from my col_row_input() function into the matrix() function. And then I still need to tackle the random number business but I figured I should figure this matrix() function out first. Thanks again for the guidance. In the meantime I'll be watching that video and studying more posts on arrays. Hopefully something will start to click. :)

*EDIT*
Ok sorry I changed it again to something that seemed much more understandable to me. Now my matrix() function goes like this:
1
2
3
4
5
6
7
8
9
10
11
12
int matrix( int rows, int columns )
{
     int array[rows][columns];

     for ( int i = 0; i < rows; ++i )
     {
          for ( int j = 0; j < columns; ++j )
          {
               /*????????????*/
          }
     }
}


Then in my /*??????*/ section is that where my random number business should go? I liked this because it didn't seem to need pointers. I don't like them, they're weird. But still I know I'll have to use them or some kind of passing to get my row and column values. But I'll hit that bridge when I get to it. Also I just noticed that this is basically what I had the first time around. I guess it was meant to be. :)
Last edited on
can you use vectors?

http://cpp.sh/7kvm
Last edited on
I could if I could explain how they work but we haven't covered them yet so I don't know anything about them. Your example looked pretty simple and seemed like it fit nicely into the code I have though. Lately all we've been covering is pointers and dynamic arrays but they confuse me so much.
But based on your example could I do something like:
1
2
3
4
5
for  ( int j = 0; j < columns; ++j )
{
     array[i][j] = 7;
     cout << array[i][j] << " ";
}

Whether it be arrays or vectors would that be the proper code to put in my for loop?
This link pretty much tells you what a vector is.

http://www.cplusplus.com/reference/vector/vector/

Seems to have worked like a charm. Even got my random number generator going just fine. Here's a question though: when I print the matrix, is there an easy way to line up my columns. It came out like this:

Enter number of rows: 10
Enter number of columns: 10
43 37 28 28 41 9 36 33 10 36 
34 19 47 17 48 30 9 34 7 42 
17 43 44 12 6 41 20 48 46 21 
10 40 10 39 20 50 47 7 34 8 
42 17 29 40 36 26 19 44 12 27 
35 30 22 30 41 29 20 11 28 18 
33 37 9 42 27 28 43 25 35 26 
33 28 45 11 20 30 38 38 25 49 
17 10 30 38 39 21 18 11 31 45 
30 15 33 38 9 9 18 1 35 4 

It just looks so messy. I feel like we were told a way to do it at one point but I can't remember how or what it's called or which reference to look under.

And on a completely different note, how do I go about getting started on finding the greatest product of four adjacent numbers in the matrix going in straight lines going up/down, left/right, diagonal, or in a box. I have absolutely no clue on how to design such a thing much less start writing code for it. Any hints there will be greatly appreciated. I've been looking through some related posts on this but I have yet to realize what's going on with them.

********EDIT*********
I just noticed that whatever number is entered for rows sets both number of rows and columns. So if I enter 6 and 3 for rows and columns respectively, I get a 6x6 matrix. If 3 and 6, I get a 3x3 matrix.

Enter number of rows: 3
Enter number of columns: 6
6 27 31 
5 47 30 
37 31 28 

Enter number of rows: 6
Enter number of columns: 3
10 19 27 47 21 27 
27 19 49 3 37 11 
25 18 26 46 34 24 
32 39 10 38 11 36 
46 38 3 7 46 45 
30 8 13 6 6 35 


And just for reference my updated code is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>

using namespace std;

void col_row_input( int &rows, int &columns )
{

	cout << "Enter number of rows: ";
	cin >> rows;
	cout << "Enter number of columns: ";
	cin >> columns;

}

int matrix( vector< vector<int> > &array )
{

	srand( time(0) );

	for ( int i = 0; i < array.size(); ++i )
	{
	
		for ( int j = 0; j < array.size(); ++j )
		{
		
			array[i][j] = rand() % 50 + 1;
			cout << array[i][j] << " ";

		}

		cout << endl;
	
	}

}

int main()
{

	int rows = 0; 
	int columns = 0;

	col_row_input( rows, columns );
	vector< vector<int> > array(rows, vector<int>(columns));

	matrix( array );

	return 0;

}

Thank you:)
Last edited on
to line up you your matrix you need to set the width of the items printed. you can do it with setw() command.

you need to include iomanip

http://www.cplusplus.com/reference/iomanip/setw/
oh sweet, I knew it was something easy like that. So, now how do I fix the column length problem? I'm only getting a square matrix based on the number given to rows.

************EDIT**************
Wait never mind on the column problem. I fixed it. I had a typo in there.

But how do I go about finding the greatest product? I know I have to look in the matrix going one space over ( in whatever direction is specified ) and multiply the numbers in those spaces. But how do I pick where to start? Maybe that's not the right question but my instructions were unclear on this part. I'm guessing I need function to have the user enter where in the matrix to start. And then I need to send that location into a function for determining the greatest product for a certain direction. If that's right I'm glad I understand that much but I'm not sure how to get that started.
Last edited on
Alrighty then. So I've figured a bunch of stuff out. I made a function that allows the user to pick what number to start at by choosing the row and column of the number. I then made a function that "theoretically" should find the greatest product of four numbers going from left to right. Maybe I've just been at it for too long but I'm stuck on how to connect my location function to my horizontal function so that I can see if it actually works or not. Hints and tips are greatly appreciated. Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <iomanip>

using namespace std;

void col_row_input( int &rows, int &columns )
{

	cout << endl << "Create an NxM matrix." << endl << endl;

	cout << "Enter number of rows: ";
	cin >> rows;
	cout << "Enter number of columns: ";
	cin >> columns;

	cout << endl;

}

void matrix( int rows, int columns, int **&array )
{

	srand( time(0) );
	array = new int *[rows];

	for ( int i = 0; i < rows; ++i )
	{
	
		array[i] = new int [columns];
	
	}

	for ( int i = 0; i < rows; ++i )
	{
	
		for ( int j = 0; j < columns; ++j )
		{
		
			array[i][j] = rand() % 50 + 1;
			cout << setw(5) << array[i][j];
		
		}

		cout << endl;
	
	}

}

void location( int rows, int columns, int **&array )
{

	cout << endl << "Choose the row and column of a number." << endl << endl;

	cout << "Row: ";
	cin >> rows;
	cout << "Column: ";
	cin >> columns;

	cout << "The number at this location is: " << array[rows-1][columns-1] << endl << endl;

}

void horizontal( int rows, int columns, int **&array, int &greatest_product )
{

	for ( int i = 0; i < rows; ++i )
	{
	
		for ( int j = 0; j < columns; ++j )
		{
		
			int num1 = array[i][j];
			int num2 = array[i][j+1];
			int num3 = array[i][j+2];
			int num4 = array[i][j+3];

			greatest_product = num1*num2*num3*num4;

			cout << "The greatest product is "<< greatest_product << endl;
		
		}
	
	}

}

int main()
{

	int rows = 0, r = 0; 
	int columns = 0, c = 0;
	int **array;

	col_row_input( rows, columns );
	matrix( rows, columns, *&array );

	location( rows, columns, *&array );

	return 0;

}
Anybody out there?

I've been thinking about it. Is it ok to keep using the rows and columns variables over and over again? The values are only changing in main right? If that's correct, then I just need either a function or a switch statement in main that will let the user choose which direction to go from the chosen starting point to find the greatest product right?
Well thats all been sooo helpful thank you!
I finally figured all that out.

Dare I ask how one might go about getting multiple functions compared so that the one with the greatest product of all gets its value printed?
Topic archived. No new replies allowed.