Need help with a couple errors.

Hi. I'm stuck on a greatest product code. I basically got it all working. When I print the location of my starting point I get the wrong location and my value for the greatest product is also wrong. I can't tell what I've done wrong. Could someone please help me locate my errors and maybe a hint or clue on how to fix them? I'll be eternally grateful. :)

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <iomanip>
#include <string>

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, int &x, int &y, string &direction )
{

	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];

			int gp = num1*num2*num3*num4;

			if ( gp > greatest_product )
			{
			
				greatest_product = gp;
				x = i, y = j;
				direction = "Horizontal";
			
			}
		
		}
	
	}

	cout << "The greatest product is " << greatest_product << endl;
        // This is a check to make sure the greatest product doesn't change in passing.
}

int main()
{

	int rows = 0, columns = 0, x = 0, y = 0;
	int greatest_product = 0;
	int **array;
	string direction;

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

	location( rows, columns, *&array );

	horizontal( rows, columns, *&array, greatest_product, x, y, direction );

	cout << "Starting Position: (" << x << ", " << y << ")" << endl;
	cout << "Direction: " << direction << endl;
	cout << "Greatest Product: " << greatest_product << endl;

	return 0;

}



Create an NxM matrix.

Enter number of rows: 7
Enter number of columns: 7

    2    6   22   23   35    3   20
   22   21   31    7   48   45   42
   28   11   50   35   25   28    1
   24   32   10   25   21   39   43
    6    9   44    9   16   17   33
   50   21    3   24   41   33   30
   40   29   23   17   42   22    4

Choose the row and column of a number.

Row: 4
Column: 4
The number at this location is: 25

The greatest product is 1225000  //This line just makes sure it works before passing.
Starting Position: (2, 2)        //This should be (4, 4).
Direction: Horizontal
Greatest Product: 1225000        //Obviously my horizontal function was wrong but at least it passes correctly. :)


Thats where I'm at, thank you in advance for any assistance that can be given.

***********EDIT*************
OK, I discovered that my starting position is somehow randomized by my matrix() function. Still don't know why or how to fix it.
Last edited on
Ok, I got everything working wonderfully and the directions have been clarified by a TA. I only need help with two things if there's anyone out there that can help me.
I'm getting a segmentation fault after the outputs of my horizontal() function. I know it's because I'm requesting the same variables after each of the three functions for direction.
How would I go about having the function with the greatest product in it being printed? Can you put a whole function call in the parameters of an if statement? In case either of those question were confusing, how do I compare the functions so that the one that holds the greatest product over all other functions is the one whose outputs are printed. Hopefully that wasn't even more confusing. Anyways, here's my code:

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <iomanip>
#include <string>


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 horizontal( int rows, int columns, int **&array, int &greatest_product, int &x, int &y, string &direction )
{

	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];

			int gp = num1*num2*num3*num4;

			if ( gp > greatest_product )
			{
			
				greatest_product = gp;
				x = i+1; 
				y = j+1;
				direction = "Horizontal";
			
			}
		
		}
	
	}

}

void diagonal( int rows, int columns, int **&array, int &greatest_product, int &x, int &y, string &direction )
{

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

			int gp = num1*num2*num3*num4;

			if ( gp > greatest_product )
			{
			
				greatest_product = gp;
				x = i+1;
				y = j+1;
				direction = "Diagonal";
			
			}
		
		}
	
	}

}

void vertical( int rows, int columns, int **&array, int &greatest_product, int &x, int &y, string &direction )
{

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

			int gp = num1*num2*num3*num4;

			if ( gp > greatest_product )
			{
			
				greatest_product = gp;
				x = i+1;
				y = j+1;
				direction = "Vertical";
			
			}
		
		}
	
	}

}

int main()
{

	int rows = 0, columns = 0, x = 0, y = 0;
	int greatest_product = 0;
	int **array;
	string direction;

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

	horizontal( rows, columns, *&array, greatest_product, x, y, direction );

	cout << endl << "Greatest Product: " << greatest_product << endl;
	cout << "Starting Position: (" << x << "," << y << ")" << endl;
	cout << "Direction: " << direction << endl << endl;

	diagonal( rows, columns, *&array, greatest_product, x, y, direction );

	cout << endl << "Greatest Product: " << greatest_product << endl;
	cout << "Starting Position: (" << x << "," << y << ")" << endl;
	cout << "Direction: " << direction << endl << endl;

	vertical( rows, columns, *&array, greatest_product, x, y, direction );

	cout << endl << "Greatest Product: " << greatest_product << endl;
	cout << "Starting Position: (" << x << "," << y << ")" << endl;
	cout << "Direction: " << direction << endl << endl;

	return 0;

}


Create an NxM matrix.

Enter number of rows: 10
Enter number of columns: 10

   15   25   38   21   37   11   41   24    8    9
   29   15   30    6   45   47    1   32   50   15
   18   26   18   20    6   32   11   16   19   27
    4   33    4   42    3   40    4   46   15   12
    4   44   28   35   49   22   31    1    3   32
   15   22    9   34   42   14   15    4   31   33
   31   34   17   34   27   22   25   31   19   39
   42   22   34   21    8   34   43   38   36   45
   21   50   17   29   33   10   44   50   15   26
   34   45    9    1   30   36   24    6   16   42

Greatest Product: 3016440
Starting Position: (8,8)
Direction: Horizontal

Segmentation fault (core dumped)


The one other thing that I needed help with is command line arguments. I need to have the option of putting the dimensions of the matrix as command line arguments and have the program run based on whether or not command line arguments were given. If they aren't given there's a prompt for the dimensions, if they are, then no prompt for dimensions. I have no idea how to do such a thing so any hints, suggestions, or advice will be greatly appreciated.
But over all, if there's one thing I can get help with please help me with the first problem above.
Thanks again. :)

*********EDIT***********
Crap, so actually all my functions are seg faulting except for the horizontal one when I ran through each of them individually. Any assistance with that would be wonderful as well. Thanks. :)
Last edited on
closed account (D80DSL3A)
Three segfault causes are easy to identify. Array index bounds are exceeded in the horizontal, diagonal and vertical functions. You let the i and j values go all the way to rows-1 and columns-1 but you have indexes where you add to i and j. These go outside of the array.
The solution is to restrict the loop values so that i+3, j+3 do not exceed rows-1 and so on.
Example for the horizontal function:
1
2
3
4
5
6
7
for ( int j = 0; j < columns-3; ++j )// see the -3 there?
		{
		
			int num1 = array[i][j];
			int num2 = array[i][j+1];
			int num3 = array[i][j+2];
			int num4 = array[i][j+3];// now this can't exceed columns-1 

Make a similar adjustment in vertical, and adjust both in diagonal.

EDIT: re. your 2nd question:
How would I go about having the function with the greatest product in it being printed?
I believe you will find that your code already works this way. The value of "direction" gets changed only when a new greatest_product is found. The last function to find a new greatest product should be the one that gets named in your output.
Call all 3 functions then output the 3 lines once.
Last edited on
Fantastic! Thank you so much. I totally forgot about adjusting for the bounds.
Got any advice on getting the function with the greatest product printed? I feel like it's something easy that I'm overlooking, such as the seg fault problem. Is it possible to do something like:

1
2
3
4
5
if ( horizontal(stuff) > diagonal(stuff) )
     {
           horizontal(stuff)
           cout << greatest_product << x << y << direction;
     }


Doesn't seem like it would work to me, and if it did, not sure how I'd cover all the functions without having a ridiculously long block of if statements. Sadly I have yet to come up with any better ideas.
Thanks again. :)
closed account (D80DSL3A)
Reread my last post. You may not have seen my edits.
Oh sweet, thanks! :)
Topic archived. No new replies allowed.