Problem with division in C!!!

This is a discussion on Problem with division in C!!! within the C Programming forums, part of the General Programming Boards category; Hi guys! I'm trying to print Pascal's triangle using the mathematical type p(n,k) = (n! / (k! * (n-k)!) In ...

  1. #1
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    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?!?!?!

    Thanks, in advance

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Can you post your code?

    Try printing r1, r2, and r3 along with result. See if they're what you expect them to be.
    If you understand what you're doing, you're not learning anything.

  3. #3
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    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?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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

  5. #5
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    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!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Put this in your code:
    Code:
      printf("r1 = %d, r2 = %d, r3 = %d\n", r1, r2, r3);
      result = r1 / (r2 * r3);
      printf("%d\n",result);
    and post the result.

  7. #7
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    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)


    Thanks in advance

  8. #8
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    As you can see teh result takes only rha values 1,0
    Also, i added a printf for p(n,k)

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > 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.
    Last edited by robatino; 04-19-2007 at 01:59 PM.

  10. #10
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    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

  11. #11
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    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

    Thanks in advance

  12. #12
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    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 ?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  14. #14
    g_p
    g_p is offline
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Thanks,I made a function counting the number of digits.
    What shall i do then?

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. Math Division Problem
    By cookie in forum C Programming
    Replies: 7
    Last Post: 06-14-2007, 11:34 AM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21