Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pascal's Triangle question
#1
Can anyone help me in forming a pascal triangle?

This is what i got so far, i've only been able to show a specific row,
the formula for calculating an element in pascal triangle is
nCr = n!/ r!(n-r)!
n is the number of row
r is the element we're looking for

In my code :
n is n
n! is j
r! is k
(n-r)! is m
and x is to show elements containing in n row

Code:
DIM r AS STRING
CLS

INPUT "Enter the number of row"; n
IF n = 0 THEN
PRINT 1
GOTO 1
END IF

j = 1
DO
i = i + 1
j = j * i
LOOP UNTIL i = n
PRINT
PRINT 1;


k = 1
DO
  l = l + 1
  k = k * l
     m = 1
    FOR g = n - l TO 1 STEP -1
    m = m * g
    NEXT g
    x = j / (k * m)
    PRINT x;
LOOP UNTIL l = n

PRINT 1

1 END


my question is, how do i repeat this process from 1 to n?
so if i input 5, it'd show

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

instead of just "1 5 10 10 5 1" like my program does
i've tried using for next and do while, but it still doesn't work.. help anyone?
Reply
#2
Try this modified program:
Code:
CLS

INPUT "Enter the number of rows"; r
PRINT
PRINT 1
IF r > 1 THEN PRINT " 1  1" ELSE GOTO FIN

FOR n = 2 TO r
  i = 0
  j = 1
  l = 0
  PRINT 1;

  DO
    i = i + 1
    j = j * i
  LOOP UNTIL i = n

  k = 1
  DO
    l = l + 1
    k = k * l
    m = 1
    FOR g = n - l TO 1 STEP -1
      m = m * g
    NEXT g
    x = j / (k * m)
    PRINT x;
  LOOP UNTIL l = n
  PRINT

NEXT n

FIN:
END
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply
#3
It workd! Thanks alot!
Damn.. so that's why i kept getting overflow message, i forgot to state the value of variables at the start of the program... X(
Reply
#4
I'm happy for you that you took the time to analyse your problem! This is a good way to learn, and learning is something I always endorse.
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)