Need help troubleshooting 2D array

Pages: 12
Yep I somehow figured that one out by myself. What I am struggling with is outputting the maximum peak value. I tried doing
1
2
3
4
5
double max;
                            if(Peak > max)
                            {
                                max = Peak;
                            }

but it didn't work
closed account (E0p9LyTq)
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
   // loop to find the peaks
   std::cout << "Peaks:\n";

   double maxPeak = 0.0;

   for (int x = 1; x < nrows - 1; x++)
   {
      for (int y = 1; y < ncols - 1; y++)
      {
         if (peak[x][y] > peak[x - 1][y])
         {
            if (peak[x][y] > peak[x + 1][y])
            {
               if (peak[x][y] > peak[x][y - 1])
               {
                  if (peak[x][y] > peak[x][y + 1])
                  {
                     std::cout << peak[x][y] << " at " << (x + 1) << ", " << (y + 1) << '\n';

                     if (peak[x][y] > maxPeak) { maxPeak = peak[x][y]; }
                  }
               }
            }
         }
      }
   }
   if (maxPeak > 0)
   {
      std::cout << "\nMax Peak: " << maxPeak << '\n';
   }
Peaks:
547.1 at 3, 3
548.9 at 3, 5
545.7 at 6, 3

Max Peak: 548.9
You're the best <3
¿is zero a sensible value to start `maxPeak'?
¿you don't care about the borders?
instead of nested ifs you may use the `and' operator
1
2
3
4
if (peak[x][y] > peak[x-1][y]
   and peak[x][y] > peak[x+1][y]
   and peak[x][y] > peak[x][y-1]
   and peak[x][y] > peak[x][y+1])
closed account (E0p9LyTq)
¿is zero a sensible value to start `maxPeak'?

Zero is less than the smallest value in the data.

It was an arbitrary choice. It could have been the value contained in peak[0][0].
Last edited on
closed account (E0p9LyTq)
instead of nested ifs you may use the `and' operator

That would be more concise logic for checking the array elements, if the OP has already learned the logical operators.
By the way, what does it mean when you say maxPeak = 0.0?
Where are the local peaks in this array:
3 3 1 3 3
3 3 1 3 3
1 1 2 1 1
3 3 1 3 3
3 3 1 3 3
closed account (E0p9LyTq)
By the way, what does it mean when you say maxPeak = 0.0?

Are you asking about line 34? double maxPeak = 0.0;?

Creating a double variable named maxPeak, initialized to zero.

Why 0.0? is an integer, which can be converted to a double. 0.0 is a double value used to initialize a double variable. No conversion.

The peak array was declared double, best if any variables dealing with the array also be declared the same.

Similar idea using unsigned (actually it's unsigned int) when working with an array's elements. An int is signed and can hold positive and negative values.

An array's elements can never properly be indexed using a negative number, you would go out of scope.

The fundamental variable types in C++ are explained here (scroll down a bit):
http://www.cplusplus.com/doc/tutorial/variables/
Topic archived. No new replies allowed.
Pages: 12