title: C++ - Could any help me to translate this for loop to recursion function?
body: <p>I have an array, table, with some char, a and b.
  I want to find and replace the 'b' with 'a' then count how many are replaced. How to write a recursive function equivalent to the nested loop?</p>
  
  <pre><code>    const int length = 4;
      char table[length][length] = {
          {'a','b','a','a'},
          {'a','a','a','b'},
          {'a','a','b','a'},
          {'b','b','a','a'}
      };
  
      int count = 0;
      for (int i = 0; i &lt; length; i++) {
          for (int j = 0; j &lt; length; j++) {
              if (table[i][j] == 'b') {
                  count++;
                  table[i][j] = 'a';
              }
          }
      }
      cout &lt;&lt; "Count: " &lt;&lt; count &lt;&lt; endl;
  </code></pre>
  
  <p>This is what I tried: </p>
  
  <pre><code>int replace_char(char array[][length], int row, int col) {
      // base cases and recursive
      if (row+1 != length - 1)
          replace_char(array, row+1, col);
      if (col+1 != length - 1)
          replace_char(array, row, col+1);
  
      // do this
      if (array[row][col] == 'b') {
          array[row][col] = 'a';
          return 1 + replace_char(array, row, col);
      }
      return 0;
  }
  </code></pre>
  
  <p>My idea is, if it's not the end of the col or the row, then go check the next col or row. While checking, if the char is b, then return 1, and start checking from where it stops.
  But it is not working.</p>