Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.
/* Title: Using Binomial Coefficients */
/* Purpose: Program ask user to enter the number of rows for the */
/* Pascal Triangle. Program then generate the Pascal Triangle required*/
/* using binomial coeffients method. */
/**********************************************************************/
#include <stdio.h>
//declaring function prototype//
void constructPascal(int numRows);
int binomialCoefficent(int n, int k);
int factorial(int n);
//main function//
int main(void)
{
//declare variable//
int numRows;
//user input//
printf("Enter the number of rows of the Pascal Triangle: ");
scanf("%i", &numRows);
//call for constructPascal function//
constructPascal(numRows);
system("PAUSE");
//exit program//
return 0;
}
//defines constructPascal function//
void constructPascal(int numRows)
{
//declare variables///
int k,n;
//generate each row//
for(n=0;(n+1)<=numRows;n++)
{
//generate each column//
for(k=0;k<=n;k++)
//print out the colummn//
printf("%i ", binomialCoefficient(n,k));
//prints a newline every row//
printf("\n");
}
}
//defines binomialCoefficient function//
int binomialCoefficient(int n, int k)
{
//return the value of each element using n and k.//
//factorial function is used here//
return (factorial(n))/((factorial(k))*(factorial(n-k)));
}
//defines factorial function//
//n!//
int factorial(int n)
{
//declare variable//
int z = 1;
while (n>1)
{
z = z*n;
n--;
}
//return the value//
return z;
}
//defines binomialCoefficient function//
int binomialCoefficient(int n, int k)
{
int top=1, bottom1=1;
while(n>1)
{
top = top*(n);
bottom1 = bottom1*k;
if(!(top%bottom1))
top = top/bottom1;
n--;
k--;
}
return top;
}
int binomialCoefficient(int n, int k)
{
int x=1,z=1,count;
count = k;
while(count>0)
{
x = x*n/z;
n--;
z++;
count--;
}
return x;
}
/**********************************************************************/
/* Matric Number: U083773L */
/* Userid: u0803773 */
/* Lab: COM1/B11 */
/* Lab Group Number: 19 */
/* Lab TA's Name: Lin Cai Quan */
/* Lab Session Date: 29 Oct 2008 */
/* Lab Session Time: 1100 - 1200 */
/* Title: Using Array */
/* Purpose: Program ask user to enter the number of rows for the */
/* Pascal Triangle. Program then generate the Pascal Triangle required*/
/* using arrays to store values. */
/**********************************************************************/
#include <stdio.h>
//declaring function prototype//
void constructPascal(int numRows);
//main function//
int main(void)
{
//declare variable//
int numRows;
//user input//
printf("Enter the number of rows of the Pascal Triangle: ");
scanf("%i", &numRows);
//call for constructPascal function//
constructPascal(numRows);
system("PAUSE");
//exit program//
return 0;
}
//defines constructPascal function//
void constructPascal(int numRows)
{
//declare variables///
int k,n[23]={1},hold=0,hold2,count;
//generate each row//
for(count=0;(count+1)<=numRows;count++)
{
hold =0;
for(k=0;k<=count;k++)
{
hold2 = hold + n[k];
printf("%i ", hold + n[k]);
hold = n[k];
n[k]= hold2;
}
//prints a newline every row//
printf("\n");
}
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Python Encoding Problem \u2013 | 4 | 114 | |
countX | 22 | 108 | |
mapBully challenge | 6 | 127 | |
Acrinis True image | 2 | 77 |
Join the community of 500,000 technology professionals and ask your questions.