DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Need Help With Printing Pascal's Triangle In C (http://www.daniweb.com/forums/thread23641.html)

indianscorpion2 May 16th, 2005 3:38 am
Need Help With Printing Pascal's Triangle In C
 
hi my name is srikanth.i have been trying to develop a source code to print the pascal's triangle for the past three days.i have'nt had any success.i would be greatly obliged if anyone could give me a simple source code to print the pascal's triangle in C.

winbatch May 16th, 2005 7:56 am
Re: Need Help With Printing Pascal's Triangle In C
 
If you've been working on it for 3 days, surely you have developed a starting point. Why don't you post your code, indicate specifically what's not working, and someone will help you.

indianscorpion2 May 16th, 2005 8:31 am
need a source code in C to print pascal's triangle
 
hi i am a student of computer science.i am a beginner.i am looking for a simple source code in C to print the pascal's triangle.the user should have the option to enter the number of rows he requires to view.i would be obliged if anyone could help out here.

winbatch May 16th, 2005 8:33 am
Re: need a source code in C to print pascal's triangle
 
You didn't like the answer the first time so you post it again?

indianscorpion2 May 18th, 2005 4:07 am
i need an algorithm to print pascal's triangle
 
i am a student of computer science.i require an algorithm to print the pascal's triangle.

Dave Sinkula May 18th, 2005 11:00 am
Re: Need Help With Printing Pascal's Triangle In C
 
Merged threads: indianscorpion2, please don't start new threads with the same topic in a relatively short period of time (a couple days). If you haven't had any success, how about starting with a failed attempt?

Dogtree May 18th, 2005 1:22 pm
Re: Need Help With Printing Pascal's Triangle In C
 
Here's the mathematical way to do it. But if this is homework, your teacher is probably looking for the brute force way of doing it. I won't show you that one because half the fun is figuring things out for yourself. The other half is writing the code to do it. :)
#include <iostream>

double factorial(double n)
{
  return (n > 1) ? n * factorial(n - 1) : 1;
}

double ncr(int n, int r)
{
  return factorial(n) / (factorial(r) * factorial(n - r));
}

int main()
{
  std::cout.setf(std::ios::fixed, std::ios::floatfield);
  std::cout.precision(0);

  for (int i = 0; i < 15; i++) {
    for (int j = 0; j <= i; j++)
      std::cout << std::right << ncr(i, j) << ' ';
    std::cout << std::endl;
  }
}

uzone24 Jul 28th, 2005 8:03 am
Re: need a source code in C to print pascal's triangle
 
Quote:

Originally Posted by indianscorpion2
hi i am a student of computer science.i am a beginner.i am looking for a simple source code in C to print the pascal's triangle.the user should have the option to enter the number of rows he requires to view.i would be obliged if anyone could help out here.

#include<stdio.h>
void main()
{
int ary[i][j],n;

printf("\nEnter the number of rows:");
scanf("%d",&n);

 for(i=0;i<n;i++)
 {
  for(j=0;j<=i;j++)
  {
  if(j==0!!j==1)
  ary[i][j]=1;
  else
  ary[i][j]=ary[i-1][j-1]+ary[i-1][j];
  }
 }

 for(i=0;i<n;i++)
 {
  printf("\n");
  for(j=0;j<=i;j++)
  {
  printf("%d",ary[i][j]);
  }
 }
}
<< moderator edit: added [code][/code] tags >>

Narue Jul 28th, 2005 7:25 pm
Re: Need Help With Printing Pascal's Triangle In C
 
uzone24, one reason I haven't moderated your post is because the original question was posed several months ago. But we don't give answers to homework without effort on the student's part. The other reason is because your code both sucks and is horribly broken, so if someone bothered to turn it in, they would surely fail.

uzone24 Jul 29th, 2005 4:11 am
Re: Need Help With Printing Pascal's Triangle In C
 
Quote:

Originally Posted by Narue
uzone24, one reason I haven't moderated your post is because the original question was posed several months ago. But we don't give answers to homework without effort on the student's part. The other reason is because your code both sucks and is horribly broken, so if someone bothered to turn it in, they would surely fail.

Narue can u elaborate on reasons y my code sucks as well wud fail ??


All times are GMT -4. The time now is 3:14 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC