1

I'm trying to write a program to display Pascal's triangle up to a user-inputted number of levels. We aren't allowed to use the factorial method. My current code is this:

#include <stdio.h>

void trianglePrint(const int numLevels);

int main() {
  int numLevels;

  printf("Please enter how many levels of Pascal's Triangle you would 
  like to see: ");
  scanf("%d", &numLevels);

  trianglePrint(numLevels);


  return 0;
}

void trianglePrint(const int numLevels) {
  int pascalTriangle[28][28];
  int i, j;

  for (i = 0; i < numLevels; ++i) {
    for (j = 0; j <= i; ++j) {
      if (i == 0 || i == 1 || j == 0 || j == numLevels) {
        pascalTriangle[i][j] = 1;
        printf("%d ", pascalTriangle[i][j]);
      }
      else {
        pascalTriangle[i][j] = pascalTriangle[i - 1][j - 1] + 
        pascalTriangle[i - 1][j];
        printf("%d ", pascalTriangle[i][j]);
      }
    }
    printf("\n");
  }

}

We're only supposed to be able to go up to 28 levels, which is why I am using an array of size 28 in both dimensions.
This works fine for about 6 levels of the triangle, but for larger levels it gives really large integers. I assumed it was due to uninitialized arrays, but I'm not sure. Does anyone know where the error is?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Ismael
  • 197
  • 1
  • 2
  • 12

3 Answers3

0

The problem is that you haven't set pascalTriangle[i - 1][j] before you use it to compute pascalTriangle[i][j] in the else clause.

0

If you change

if (i == 0 || i == 1 || j == 0 || j == numLevels)

to

if (i == 0 || i == 1 || j == 0 || j == i)

(thanks to Melpomene), then all accesses to your array end up on already intiailised members.

That solves the strange numbers.

Output:

Please enter how many levels of Pascal's Triangle you would like to see: 6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Process returned 0 (0x0)   execution time : 2.264 s
Press any key to continue.

Note:
Also initialising an array is a wise precaution. You could initialise with values which help finding an error, instead of hiding it, e.g. 42.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • @BLUEPIXY Thanks for spotting the error I fixed. Couldn't delete accepted answer for editing.... – Yunnosch Nov 05 '17 at 17:11
  • Upvoted, but I don't agree with your comment about initialising arrays. If you initialise an array, you prevent tools like valgrind from helping you,and in any case uninitialised values stand out a mile, so are informative. –  Nov 05 '17 at 17:26
0

Try the code

void Pascal(int n)
    {
      int arr[n][n]; 


      for (int i = 0; i < n; i++)
      {

        for (int j = 0; j <= i; j++)
        {
          // First and last values in every row are 1
          if (i == j || j == 0)
               arr[i][j] = 1;
          else // Other values are sum of values just above and left of above
               arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
          printf("%d ", arr[i][j]);
        }
        printf("\n");
      }
    } 
shubhamj
  • 778
  • 1
  • 7
  • 13
  • You would deserve an upvote for a fast and good (not to say better than my first attempt) answer. But code-only answers are unlikely to be upvoted. Try explaining why your code does not have the problem, i.e. how you solved it. – Yunnosch Nov 05 '17 at 17:17