Skip to content

Instantly share code, notes, and snippets.

@Compro-Prasad
Created August 29, 2016 15:04
Show Gist options
  • Save Compro-Prasad/9460ff815a5521fad0e6cab7281aff15 to your computer and use it in GitHub Desktop.
Save Compro-Prasad/9460ff815a5521fad0e6cab7281aff15 to your computer and use it in GitHub Desktop.
C code to generate the largest pascal's triangle on the screen
#include <stdio.h>
#include <math.h>
#ifndef LINUX
#define LINUX
#endif
/* uncomment the following line if you get compilation errors */
//#undef LINUX
#ifdef LINUX
#include <sys/ioctl.h>
#include <fcntl.h>
#endif
int main()
{
unsigned long t, Nth_row, total_rows, r, midx, max_num_len;
long i;
char spacing[20];
#ifdef LINUX
struct winsize terminal_screen;
#endif
printf("\n===============================================================================");
printf("\n Pascal's Triangle ");
printf("\n===============================================================================");
printf("\n-------------------------------------- --------------------------------------");
printf("\n Number of Rows of the triangle|:|");
scanf("%lu", &total_rows);
printf("-------------------------------------- --------------------------------------\n");
printf("Length of the Maximum number generated|:|");
scanf("%lu", &max_num_len);
printf("-------------------------------------- --------------------------------------\n");
#ifdef LINUX
/* get the maximum size of the screen */
if (ioctl(open("/dev/tty", O_NONBLOCK), TIOCGWINSZ, &terminal_screen) < 0)
{
/* If an error occurred while taking the terminal screen size
then read it manually from the user */
#endif
printf(" Enter mid point of line|:|");
scanf("%lu", &midx);
printf("-------------------------------------- --------------------------------------\n");
#ifdef LINUX
}
else
/* if successfull then set midx */
midx = terminal_screen.ws_col / 2;
#endif
/* setting the spacing.
it will actually work like "%Nlu" while displaying a number
where N is the part of print format of printf */
sprintf(spacing, "%%%lulu", max_num_len + max_num_len);
/* The main algorithm:
An arbitrary row can be represented as:-
C(n,0) C(n, 1) C(n,2) ... C(n, n-1) C(n, n)
where,
n = Row number
C(n, r) = factorial(n) / (factorial(n-r) * factorial(r))
factorial(n) = 1 x 2 x 3 x 4 ... x (n-1) x n
Now, to generate T(r) term of the row we have to know the previous term.
Since, we already know the first term will always be 1 by calclating
C(n, 0) so we need to work out on a formula to calculate the next term
given we already know the row number.
T(r) = C(n, r) , r > 0
T(r-1) = C(n, r-1)
T(r) C(n, r)
-------- = -----------
T(r-1) C(n, r-1)
Doing the necessary calculations and simplifying the equation we get,
T(r) = T(r-1) * (n - r + 1) / r
We already know 'n' which the row number and 'r' is itself dependant upon
'n'. T(r-1) will firstly be 1 and will change as the loop progresses.
Analogy of the theory and the program:
T(r) = t = T(r-1)
n = Nth_row
r = r
*/
for (Nth_row = 0; Nth_row < total_rows; Nth_row++)
{
/* Print preceding spaces of a row.
Number of spaces decreases as the Nth_row increases */
for (i = midx - max_num_len * (Nth_row + 1); i > 0; --i)
printf(" ");
/* Every row always begins with a one */
printf(spacing, t = 1);
/* Print other numbers(if any) */
for (r = 1; r <= Nth_row; r++)
/* The formula to generate the next number */
printf(spacing, t = t * (Nth_row - r + 1) / r);
/* Always move to next line when a whole row is printed */
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment