title: What's wrong with my pascal's triangle?
body: <p>I've been looking for some simple coding challenges recently, and discovered about Pascal's triangle (<a href="http://programmingpraxis.com/2011/12/06/pascals-triangle/" rel="nofollow">here</a>), and I've tried to generate one myself in C/Objective-C. For those that don't know what it is, that link explains it pretty well.</p>
  
  <p>I'm starting to get oddness after the fourth row, and I just can't figure out why.</p>
  
  <p>My output for 5 iterations currently looks like this:</p>
  
  <pre><code>   1      
    1 1     
   1 2 1    
  1 3 3 1   
   4 6 3 1
  </code></pre>
  
  <p>It should look like this:</p>
  
  <pre><code>    1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1
  </code></pre>
  
  <p>Here is my code so far. The first loop is just a reset loop (setting all the values to 0). The actual logic happens mostly in the second loop. The third loop is where the values are concatenated and formatted in a string.</p>
  
  <p>I've commented this code much more than I would for myself just to aid readability.</p>
  
  <pre><code>int iterations, i, b, mid, chars, temp;
  NSLog(@"Please enter the number of itereations");
  scanf("%i",&amp;iterations); // take users input and store it in iterations
  
  // calculate where the first 1 should go.
  if (iterations % 2 == 0) mid = (iterations)/2;
  else mid = (iterations+1)/2;
  
  chars = iterations*2;
  
  int solutions[iterations][chars];
  
  // reset loop
  for (i = 0; i&lt;iterations; i++) {
      for (b = 0; b&lt;chars; b++) {
          solutions[i][b] = 0;
      }
  }
  
  solutions[0][mid] = 1; // place the initial 1 in first row
  
  for (int row = 1; row&lt;iterations; row++) {
      for (int chi = 0; chi&lt;chars; chi++) {
          temp = 0;
          if (chi &gt; 0) {
              temp += solutions[row-1][chi-1]; // add the one diagonally left
          }
          if (chi &lt; iterations) {
              temp += solutions[row-1][chi+1]; // add the one diagonally right
          }
          solutions[row][chi] = temp; // set the value
      }
  }
  
  // printing below...
  
  NSMutableString *result = [[NSMutableString alloc] initWithString:@"\n"];
  NSMutableString *rowtmp;
  
  for (i = 0; i&lt;iterations; i++) {
      rowtmp = [NSMutableString stringWithString:@""];
      for (b = 0; b&lt;chars; b++) {
          if (solutions[i][b] != 0) [rowtmp appendFormat:@"%i",solutions[i][b]];
          else [rowtmp appendString:@" "]; // replace any 0s with spaces.
      }
      [result appendFormat:@"%@\n",rowtmp];
  }
  
  NSLog(@"%@",result);
  [result release];
  </code></pre>
  
  <p>I have a feeling the problem may be to do with the offset, but I have no idea how to fix it.  If anyone can spot where my code is going wrong, that would be great.</p>