Solved

junk output with pascal triangle generation

Posted on 2008-10-28
4
758 Views
Last Modified: 2012-05-05
when i used larger numbers of rows for pascal triangle, junk appears.
i think the compiler cannot hold the big values in my constructPascal function.
/* 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;
 
}

Open in new window

0
Comment
Question by:Zhongcai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Author Comment

by:Zhongcai
ID: 22823595
i'm now trying to change my binomialCoefficient function so that the top term gets divided by the bottom term as the terms gets bigger. but i can't seem to put it in language.
//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;          
}

Open in new window

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 22823960
>>     return (factorial(n))/((factorial(k))*(factorial(n-k)));

That is due to the way you calculate the values. 13! alreadt overflows a 32bit integer.
But your second post indicates that you already realized that :)


>> so that the top term gets divided by the bottom term as the terms gets bigger. but i can't seem to put it in language.

You can write a loop. n! / k! is the same as n * (n - 1) * (n - 2) * ... * (k + 1). So, you multiply the terms n through (k + 1) in a loop. That will already get you further down the Pascal triangle.


There are of course alternative ways of calculating the values in the triangle. Each value is the sum of the two values above it.
0
 

Author Comment

by:Zhongcai
ID: 22832377
i managed to think about a solution already!
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;
    
}

Open in new window

0
 

Author Comment

by:Zhongcai
ID: 22832398
solution using arrays
/**********************************************************************/
/* 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");
             
     }
}

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question