second function skipped by main

Jun 6, 2013 at 3:18am
Hi, I have been working on an assignment and having some seriously frustrating issues, and I hope someone can point me in the right direction. The assignment is to dynamically create an array of random integers via command line arguments, and search the array for the greatest product of four adjacent numbers going up, down, left, right, zigzag, in a box, etc. I have the array and the random integers working and I have functions for searching for greatest product. Two of them are in the code and work separately just fine (up and down). My problem is, that when I call one function and then the other in main(), it completely skips the second function (I mean the second searching function, not counting the create_array() one). I've searched and searched the forums with no luck and I'm already late with the assignment, so any help at all would be hugely appreciated. I'm pulling my hair out trying to figure out what I'm missing. Thanks, here's the 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
  #include <iostream>

#include <stdlib.h>

#include <time.h>

#include <string>



using namespace std;



void create_array(int rows, int columns, int **(&array));

void down(int rows, int columns, int **(&array), int &greatest, string &shape, int &p1, int &p2);

void up(int rows, int columns, int **(&array), int &greatest, string &shape, int &p1, int &p2);



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


   int rows = atoi(argv[1]), columns = atoi(argv[2]), greatest = 0, **array, p1, p2;

   string shape;




   create_array(rows, columns, *(&array));

   down(rows, columns, *(&array), greatest, shape, p1, p2);

   up(rows, columns, *(&array), greatest, shape, p1, p2);

cout << greatest << "\n";

   up(rows, columns, *(&array), greatest, shape, p1, p2);

   cout << "Greatest product: " << greatest << " Shape: " << shape << " Position: " << p1 << ", " << p2 << endl;


}



void create_array(int rows, int columns, int **(&array)){
   srand(time(NULL));
   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() % 100;

	 cout << array[i][j] << " ";

      }
      cout << endl;

   }

}



void down(int rows, int columns, int **(&array), int &greatest, string &shape, int &p1, int &p2){

   for(int i = 0; i < (rows - 3); i++){

      for(int j = 0; j < columns; j++){

         int a = array[i][j];

	 int b = array[i+1][j];

	 int c = array[i+2][j];

	 int d = array[i+3][j];

	 int x = a * b * c * d;

	 if(x > greatest){

	    greatest = x;

            shape = "Down";

	    p1 = i;

	    p2 = j;

	    cout << "down " << x << endl;

	 }

      }

   }
}


void up(int rows, int columns, int **(&array), int &greatest, string &shape, int &p1, int &p2){

   for(int i = 3; i < rows; i++){

      for(int j = 0; j < columns; j++){

         int a = array[i][j];

	 int b = array[i-1][j];

	 int c = array[i-2][j];

	 int d = array[i-3][j];

	 int x = a * b * c * d;

	 if(x > greatest){

	    greatest = x;

            shape = "Up";

	    p1 = i;

	    p2 = j;

	    cout << "up " << x << endl;

	 }

      }

   }

}
Last edited on Jun 6, 2013 at 3:26am
Jun 6, 2013 at 3:22am
p.s. The cout(s) on lines 39, 102, and 138 were for troubleshooting and were added to help me confirm it was actually skipping the second function.
Jun 6, 2013 at 8:16am
p.s. The cout(s) on lines 39, 102, and 138 were for troubleshooting and were added to help me confirm it was actually skipping the second function.

That's a good technique to use for diagnosing bugs in your code, but in this instance, it's not's not doing exactly what you think.

The output at line 138 doesn't tell you whether or not it's skipping the function. It only tells you whether, once it enters that function, it enters the conditional block at line 129.

My guess would be that isn't "skipping" the up function at all. It's entering that function just fine, but the condition at line 128 is never true.
Last edited on Jun 6, 2013 at 8:17am
Jun 6, 2013 at 8:34am
so much whitespace.

> p.s. The cout(s) on lines 39, 102, and 138 were for troubleshooting
>> That's a good technique to use for diagnosing bugs in your code
It is not.
Jun 6, 2013 at 10:31am
Using diagnostic messages to track down a bug is sometimes the only sensible tool available - but it depends upon the context.

For example, if a strange behaviour occurs when a program executes with ten million data records, but doesn't show up on a test with 100 records. Or many other situations.

Still, the first tool I'd reach for is the debugger in this case.
Jun 6, 2013 at 10:35am
Agreed that using a debugger is an even better way of seeing what's going on, and I've advised people on here to use debuggers on many occasions myself.

But putting in some debugging output isn't a bad way to start out, particularly for a beginner who's got a ton of other stuff to learn as it is (and, remember, we're in the Beginners forum here). It certainly shows more thought than many of the people who post here asking for help :)
Jun 6, 2013 at 11:09am
At least use a different stream, like std::cerr (that also has the advantage to be unbuffered)

> particularly for a beginner who's got a ton of other stuff to learn
like program flow...
Jun 6, 2013 at 2:05pm
Sorry about all the white space. I am using a Linux based text editor to write the program which doesn't allow copy/paste outside of the program itself (at least as far as I know), so I had to open up the file in notepad and edit it to look readable. I didn't realize it was basically double spacing.

@MikeBoy: Yes, I am a beginner and so I'm trying to use the tools we have been given to debug. Thanks for recognizing that I'm trying to figure things out as much myself as I can without asking for too much help. I've seen people basically ask others to write their code for them in various forums, and this seems basically like cheating to me, so I'm just looking for pointers or for someone to point out if I'm doing something wrong. Also, your first reply got me thinking and when I am checking the array up and down, I'm checking the same thing basically, so it makes sense that whichever function up() or down() gets called first would cause the conditional block in the second function to never be met. Thanks! I can't believe I didn't think about that. I put in a function that checks products from left to right into my code and it is working. Thanks!

We haven't been given access to a debugging program and I'm pretty sure we are not supposed to use one.

@ne555: I'll look into std::cerr

I have the pseudo-code for the rest of the functions I need already written, but I have to go to work, so I won't be adding them to the code until later today most likely. I'll mark this as solved later as long as I don't have issues adding these other functions to my code later today. Thanks for the input, and I'm interested in any more insight if people want to add more.
Topic archived. No new replies allowed.