# Problem with division in C!!!

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-19-2007
g_p
Problem with division in C!!!
Hi guys!

I'm trying to print Pascal's triangle using the mathematical type
p(n,k) = (n! / (k! * (n-k)!)

In my C program i count n! using a for loop and i store the final value in an integer value named r1.
I do the same for k! (int r2) and (n-k)! (int r3).
The results from the for loops are correct cause i have tested them :)

The problem is when i try to count the p(n,k) where i have only to do a simple division,
I do:

int result = r1 / (r2 * r3);
printf("%d\n",result);

The only results i take from the division are 1 and 0.
e.g. p(2,1) = (2! / (1! * (2-1)!) = 2 / (1 * 1) = i shall take 2

I take 1!!!

Why?!?!?!

• 04-19-2007
itsme86

Try printing r1, r2, and r3 along with result. See if they're what you expect them to be.
• 04-19-2007
g_p
Well i have many printfs for r1,r2,r3.
Before every result i type their value.
Right now i haven't finished my code so i cannot paste it yet!
But do you have any idea why the division result is wrong?
:)
• 04-19-2007
robatino
As an aside, if you're trying to print the entire triangle, it's easier to just use addition to do it.

http://en.wikipedia.org/wiki/Pascal's_triangle
• 04-19-2007
g_p
Well i have already studied for Pascal's triangle in many sites and in wikipedia as well :)
My problem is when i try to utilise that in C.
The only problem i have is that division! :)
• 04-19-2007
robatino
Code:

```  printf("r1 = %d, r2 = %d, r3 = %d\n", r1, r2, r3);   result = r1 / (r2 * r3);   printf("%d\n",result);```
and post the result.
• 04-19-2007
g_p
Sorry for the delay :)
Well i give first the number of rows for Pascals Triangle , e.g. i give 4 so according to Pascal
i take ten p(n,k)

The output i get is :

r1 = 1, r2 = 1, r3 = 1
1
p(0,0)

r1 = 1, r2 = 1, r3 = 1
1
p(1,0)
r1 = 1, r2 = 1, r3 = 1
1
p(1,1)

r1 = 2, r2 = 1, r3 = 2
1
p(2,0)
r1 = 1, r2 = 1, r3 = 1
1
p(2,1)
r1 = 1, r2 = 2, r3 = 1
0
p(2,2)

r1 = 6, r2 = 1, r3 = 6
1
p(3,0)
r1 = 1, r2 = 1, r3 = 2
0
p(3,1)
r1 = 1, r2 = 2, r3 = 1
0
p(3,2)
r1 = 1, r2 = 6, r3 = 1
0
p(3,3)

• 04-19-2007
g_p
As you can see teh result takes only rha values 1,0
Also, i added a printf for p(n,k) :)
• 04-19-2007
robatino
> r1 = 1, r2 = 1, r3 = 1
> 1
> p(2,1)

p(2,1) == 2!/(1! * 1!) == 2/(1*1). So your value of r1 is wrong. It has nothing to do with the division. Similarly,

> r1 = 1, r2 = 2, r3 = 1
> 0
> p(2,2)

Here you should have r1 = 2, r2 = 2, and r3 = 1.
• 04-19-2007
g_p
Thanks :)
I'm going to check very carefully my source code and correct my mistakes and if i see that sthg is not
functioning well I'm going to post again :)

Thank you and have a nice evening :)
• 04-19-2007
g_p
Hi again :)

Well right now i corrected the values i take now the right output but the spaces inside the triangle
are wrong!
What shall i do?
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1

• 04-19-2007
g_p
Well the paste wasn't exactly as it otput in my program.
It's like a "christmas tree" but the problem is with the inside spaces

How do i count the inside spaces ?
• 04-19-2007
robatino
As long as it's correct mathematically, I wouldn't worry about it. To get the formatting exactly right, you would have to first compute the whole triangle, then count the total number of characters in each line (including the number of digits in each entry), then offset each line accordingly and put the right number of spaces between the numbers on each line. It's not worth the trouble.
• 04-19-2007
g_p
Thanks,I made a function counting the number of digits.
What shall i do then?
• 04-19-2007
whiteflags
Space the numbers for each row out equally using some of printf's formatting powers. But robatino is pretty much right. Besides, it's a right triangle. Right triangles are still triangles, right? Pascal's right triangle, as it were.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last