Coding in C (Part 1) - Pascal's Triangle

code ලියන හැටි ඉගෙන ගන්න මෙහාට එන්න.

Coding in C (Part 1) - Pascal's Triangle

Postby Dirae » Thu Sep 19, 2013 11:47 pm

Introduction

Hello and welcome to Coding in C. In this series, we will examine various simple code snippets and understand the basic concepts behind them. We will be covering a wide area, from recreational mathematics, network programming to database management. These tutorials are aimed towards novice programmers who are already familiar with the C language.

I basically code in a Windows environment (Win64) but I have made all the programs discussed ANSI C (C11) standard compatible, so you will not have any issues in building the programs on any complier. In case you do encounter an error when compiling in either platforms, pleas contact me. I have included the complete .C source file for your convenience, but however I suggest that you learn the concept and try out by yourself.

As a debut, I will discuss a program to display Pascal's Triangle. Basically, a Pascal's Triangle is a triangular construct where each number in the triangle is the sum of the two directly above it, like shown below.

Image


Code

The following program will display Pascal's Triangle up to 13 rows. It consists of two functions, main() and fac().

Code: Select all
#include <stdio.h>
long fac(int);
int main(void) {

   int num, spaces, n, k;
   long sum;

   printf ("NO. OF ROWS:");
   scanf ("%d", &num);

   for (n = 0; n <= num - 1; n++){
      for (spaces = num - n; spaces > 0; spaces--){
         printf (" ");
      }
      for (k = 0; k <= n; k++){
         sum = fac(n)/(fac(k) * fac(n-k));
         printf ("%ld ",sum);
      }
      printf ("\n");
   }
   return 0;
}

long fac(int a){
   long prod;

   if (a >1){
      prod = a * fac(a - 1);
      return prod;
   }
   else
      return 1;
}


Discussion

The nth row will contain n number of elements and we will call each element k such that k = 0, 1, 2,...(n-1) and any given element in any row is (n k). For example the 5th row will have 5 elements, each element designated as (5 0), (5 1), (5 2), (5 3) and (5 4).

The first element at the tip is designated (0 0).

To calculate any given (n k) element we use the formula:
(n k) = n! / (k!(n-k)!)


We implement this calculation by:
sum = fac(n)/(fac(k) * fac(n-k));


The function fac(); will recursively calculate the factorial.

This program will not display rows greater than 13 correctly. This is because the factorial is stored in a long variable. A signed long variable can hold upto 2,147,483,647 and an unsigned long variable can hold upto 4,294,967,295. Since 13! = 6,227,020,800, 13 rows would be the maximum we can display correctly.

In this program I have followed a rudimentary approach in spacing and formatting hence the triangle will go askew with higher rows.

End

Your expected output would be something similar:

Image


For those of you who are interested in using computer programming for recreational mathematics, Project Euler is highly recommended.

This concludes Part 1 of Coding with C series and in the subsequent parts we will examine more programs. Your questions, suggestions are feedback are extremely welcome. For further information, contact me on Facebook. මෙම අඩවියේ ලියාපදිංචි වූ භාවිතාකරුවෙක් මෙම ලිපිය සිංහල භාෂාවට පරිවර්තනය කර නැවත මෙහි පළ කරන්නේ නම් ස්තුතිවන්ත වෙමි.
  • 0

"A beautiful thing is never perfect."
Off Topic
Participate in Einstein@Home Project to search for weak astrophysical signals from spinning neutron stars (also called pulsars) using data from the LIGO gravitational-wave detectors.

Image
User avatar
Dirae
 
Posts: 2
Joined: Thu Sep 19, 2013 6:46 pm
Location: Kandy, Sri Lanka
Has thanked: 0 time
Been thanked: 0 time
Reputation: 0

Re: Coding in C (Part 1) - Pascal's Triangle

Postby root » Sat Sep 21, 2013 9:06 pm

nice tutorial thanks
  • 0

User avatar
root
Site Admin
 
Posts: 82
Joined: Mon Jul 15, 2013 3:46 pm
Has thanked: 0 time
Been thanked: 0 time
Reputation: 0


Return to Programming Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron