/* Here you go Ben i've tried to add as much information as I can in the 
 * comments, to try and make it easier to understand. My English isnt to good
 * so dont take the piss!
 * It will work for numbers in the triangle that dont go over three digits
 * after this it starts to go tits up, but there is a pattern in the way it
 * works out were to put the numbers, you could comment on this to get more
 * marks! Good luck
 * The next comment makes your program look quite professional (I think anyway)
 */
/* 
   Name: 
   Author: 
   Description: 
   Date: 
   Copyright: 
*/
/* Include the main stdio.h file which is all the basic commands like printf */
#include <stdio.h>
#include <math.h>
/* Define the maximum amount of rows */
#define max_rows 100
/* Call the main function (the first function and the only one in this case
 * that will be run)
 */
int main()
{
      /* This part sets up the maximum amount of rows and a few integers
       * the reason it is plus one is because in the calculations
       * it needs the blank one at the end so it doesnt crash on the most
       * amount of rows
       * The plus two part is for the width of the matrix
       */
      int number[max_rows + 1][max_rows + 2],n,a,b,c,d,rows;
      /* This prints out to standard error so it appears on screen but not 
       * in a redirected file (if tested by computer)
       */
      fprintf(stderr, "Please enter the number of rows\n");
      /* Takes in the number of rows and puts it into 'rows' */
      scanf("%d", &rows);
      /* If there are 'rows' than allowed then exit with and error */
      if (rows > max_rows)
      {
         fprintf(stderr, "Too many rows");
         exit(1);
      }
      /* Set u the first two numbers */
      number[0][0] = 1;
      number[1][0] = 1;
      /* The width of the current row */
      b = 2;
      /* The amount of digits from the biggest number in the triangle */
      d = 1;
      /* Repeat the amount of times there are rows apart from the first */
      for(n = 1; n < rows; n++)
      {
            /* Set the first number on each line to one */
            number[0][n] = 1;                                          
            /* Repeat until reaches the end of the line 
             * The last one adds to a zero value at the end equalling one
             */
            for(a = 1; a <= b; a++)
            {
                  /* This number equals the number above added to the one
                   * before it
                   */
                  number[a][n] = number[a][n-1] + number[a-1][n-1];
                  /* If any of the numbers go over 1 digit then d must equal
                   * two. The and d does not equal 3 part is so if there is
                   * three digits it doesnt change d back to two.
                   */
                  if(number[a][n] > 9 && d != 3)
                       d = 2;
                  /* If any of the numbers go over 2 digits the d must equal
                   * three
                   */
                  if(number[a][n] > 99)
                       d = 3;
            }
            /* Add one to the row width */
            b++;
      }
      /* Back to the start for printing, so the row width is back to two */
      b = 2;
      /* c equals the amount of rows, this is for the spaces before the numbers
       * come
       */
      c = rows;
      /* If there is more than 1 digit then the triangle needs to be spread out
       * more.
       */
      if(d >= 2)
           c = c*2;
      /* Repeat the amount of rows there are for printing*/
      for(n = 0; n < rows; n++)
      {
            /* Puts in the spaces at the start of each line */
            for(a = 0; a < c; a++)
                  printf(" ");
            /* Repeats the amount of numbers there are in the line */
            for(a = 0; a < b; a++)
            {
                  /* Print the number */
                  printf("%i", number[a][n]);
                  /* If there is only one digit then you only need to put
                   * one space between each number
                   */
                  if(d == 1)
                       printf(" ");
                  /* If there are two digits then you need three spaces between
                   * the smaller numbers and take one off for the bigger numbers
                   * of two digits
                   */
                  if(d == 2 && number[a+1][n] < 10)
                       printf("   ");
                  if(d == 2 && number[a+1][n] > 9)
                       printf("  ");               
                  /* If there are three digits then you need to take off an
                   * extra space for the ones with three digits and only one
                   * off for the ones with two digits
                   */
                  if(d == 3 && number[a+1][n] < 10)
                       printf("   ");
                  if(d == 3 && number[a+1][n] > 9 && number[a+1][n] < 100)
                       printf(" ");               
                  if(d == 3 && number[a+1][n] < 100 && number[a+1][n] > 9)
                       printf(" ");
                  if(d == 3 && number[a+1][n] > 99)
                       printf(" ");               
            }
            /* Add one to the column width */
            b++;
            /* If the triangle has more than two digits in it then take off two
             * for each space at the start of the line rather than one
             */
            if (d >= 2)
               c--;
            c--;
            /* Print a carrage return to go onto the next line */
            printf("\n");
      }
      /* You might not need this, it will just pause at the end till you press
       * a key. Text is also printed but wont go into a redirected file ie
       * the text is printed to standard error
       */
      system("PAUSE");
      /* Because the main function is an integer return a zero, this could
       * be used to pass on numbers to a different program. It isnt needed
       * but you get a warning if you dont have it
       */
      return 0;
}
