PDA

View Full Version : ngemogram Array, help


anna_ht
27-12-2006, 02:04 PM
hiii, mo aq sedang belajar ttg pemprograman VBScript(yg pke html notepad itu lo),.. n aq bingung buat program dgn soal gini:
develop an algorithm to compute Pascal's triangle of binomial coefficients. Such a triangle has the form

where, in general, a row is obtained by noting that each element in this row is the sum of the two elements immediately above it. Generate the first 10 rows of this triangle.

Buat sedigita pascal, dgn inputan n(n=banyak baris) gmn ya???
help me plzzzz,. thxa lot

swl
28-12-2006, 09:44 AM
ini ada codenya, tp tidak dalam vbscript, hasilnya sudah kesimpan di dlm array.
ex: input row = 3
result[1][1] = 1
result[2][1] = 1, result[2][2] = 1
result[3][1] = 1, result[3][2] = 2, result[3][3] = 1

$row = 5;
$arr_result = array();

for($i = 1; $i <= $row; $i++)
{
for($j = 1; $j <= $i; $j++)
{
if($j == 1 || $j == $i)
{
$arr_result[$i][$j] = 1;
}
else
{
if($i > 2)
{
$arr_result[$i][$j] = $arr_result[$i - 1][$j - 1] + $arr_result[$i - 1][$j];
}
}
}
}


semoga membantu ::oops::

anna_ht
28-12-2006, 10:25 AM
waw, itu dlm C++ yo??
ehm, tar d aq coba ke VBScript dl,. Coz gk ngerti tuh yg namanya kyk2 "j++",..
thxa lot^ :)

swl
28-12-2006, 01:47 PM
J++ itu kalo diterjemahin menjadi J = J + 1 :D

anna_ht
28-12-2006, 03:45 PM
du du du duh,. aq bingung,. Apa lg tuh "=="
Ehm, bner2 gk ngerti,..
TTG SOAL : tanpa inputan, langsung diminta 10 baris pertama dr PASCAL
yg aq buat si kyk gini :

n=10
redim(10,100)

FOR i=3 TO n
x(i,1)=1
document.write x(i,1)
FOR j=2 TO i-1
x(i,j)=x(i-1,j-1)+x(i,1)
document.write x(i,j)
NEXT
x(i,i)=1
document.write x(i,i)
NEXT

aq bingung ttg array nya tuh ,. REDIM (brp, brp) yah??

anna_ht
28-12-2006, 03:46 PM
du du du duh,. aq bingung,. Apa lg tuh "==" ::grrr:: (gk jiwa programming nih, hik hik)
Ehm, bner2 gk ngerti,..
TTG SOAL : tanpa inputan, langsung diminta 10 baris pertama dr PASCAL
yg aq buat si kyk gini :

n=10
redim(10,100)

FOR i=3 TO n
x(i,1)=1
document.write x(i,1)
FOR j=2 TO i-1
x(i,j)=x(i-1,j-1)+x(i,1)
document.write x(i,j)
NEXT
x(i,i)=1
document.write x(i,i)
NEXT

aq bingung ttg array nya tuh ,. REDIM (brp, brp) yah??

swl
29-12-2006, 10:55 AM
Btw kalo dr yg u buat, baris 1 & 2 nya ga nongol donk CMIIW
Tuh dah hampir bener sih, buat redim u make aja (n, n); biar deklarasi array nya sesuai dgn inputan n.

Yah kalo di C "==" itu cuma buat pembanding, kalo di VB sih biasanya cuma "=" doank pada kondisi if nya... kalo ga salah if($j == 1 || $j == $i) di vb ditulis jd if j = 1 or j = i then statement end if

;)