title: C++ matrix - Get number of columns where the lowest value is in the same row as the highest value in the columns after it
body: <p>I'm writing a program in C++ where the inputs are N (the number of villages/rows), M (the number of days/columns) and an H[N][M]
  matrix where I individually input the temperatures (min -50, max 50).</p>
  
  <p>The output should be the total number of days when the village with the lowest
  temperature has the highest forecast temperature, and after that the number (column) of these days in ascending order.</p>
  
  <p>So if I input something like this:</p>
  
  <pre><code>3 5 
  10 15 12 10 10
  11 11 11 11 20
  12 16 16 16 20
  </code></pre>
  
  <p>The output should be:</p>
  
  <pre><code>2 2 3
  </code></pre>
  
  <p>Or input:</p>
  
  <pre><code>3 3
  1 2 3
  1 2 3
  1 2 3
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>2 1 2
  </code></pre>
  
  <p>My approach was to first store the minimum temperatures and maximum forecast temperatures of each day into two separate arrays and
  then writing a for loop where I check each village day by day if they contain both the minimum value on the given day and maximum forecast temperatures from that day on.</p>
  
  <p>I have the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  const int maxarr = 1000;
  int H[maxarr][maxarr];
  
  using namespace std;
  
  void read(int N, int M, int t[maxarr][maxarr]);
  void count(int N, int M, int t[maxarr][maxarr]);
  
  int main()
  {
      int N;
      int M;
      cout&lt;&lt;"Number of villages? ";
      cin&gt;&gt;N;
      cout&lt;&lt;"Number of days? ";
      cin&gt;&gt;M;
      read(N,M,H);
      count(N,M,H);
  
      return 0;
  }
  
  void read(int N, int M, int t[maxarr][maxarr])
  {
      for(int i = 0; i &lt; N ; i++)
      {
          for(int j = 0; j &lt; M ; j++)
          {
              cin&gt;&gt;t[i][j];
          }
      }
  }
  
  void count(int N, int M, int t[maxarr][maxarr])
  {
      int mintemparr[maxarr];
      int maxtemparr[maxarr];
      int mintemp;
      int maxtemp;
      int days[maxarr];
      int cnt = 0;
  
      for(int j = 0; j&lt;M; j++)
      {
          mintemp = 51;
          for(int i = 0; i&lt;N; i++)
          {
              if(t[i][j]&lt;mintemp)
              {
                  mintemp = t[i][j];
              }
              mintemparr[j] = mintemp;
          }
      }
  
      for(int i = 0; i &lt; M-1; i++)
      {
          maxtemp = -51;
          for(int j = 0; j &lt; N; j++)
          {
              for(int k = i+1; k &lt; M; k++)
              {
                  if(t[j][k]&gt;maxtemp)
                  {
                      maxtemp = t[j][k];
                  }
              }
              maxtemparr[i] = maxtemp;
          }
      }
      for(int i = 0; i &lt; M-1; i++)
      {
          for(int j = 0; j &lt; N; j++)
          {
              for(int k = i+1; k &lt; M; k++)
              {
                  if(t[j][i] == mintemparr[i])
                  {
                      if(t[j][k] == maxtemparr[i])
                      {
                          days[cnt] = i+1;
                          cnt++;
                          //tried an i++ here, didn't work as intended
                      }
                  }
                  else
                  {
                      j++;
                  }
              }
          }
      }
      cout&lt;&lt;cnt&lt;&lt;" ";
      for(int i = 0; i &lt; cnt; i++)
      {
          cout&lt;&lt;days[i]&lt;&lt;" ";
      }
  }
  </code></pre>
  
  <p>There are some instances where it works perfectly, for example with the first input it's output is as it should be. But with the
  second input I get </p>
  
  <pre><code>6 1 1 1 2 2 2
  </code></pre>
  
  <p>and a longer (1000x1000) input, which I obviously can't copy here also gives wrong results.
  How could I make this code work as intended?</p>