What's new

Need some help with Java

Rusty

ELITE MEMBER
Joined
Jun 19, 2011
Messages
8,563
Reaction score
3
Country
Canada
Location
Pakistan
I have been stuck on this for a few days so I might as well take the long shot and ask here.

I need to program a pascal's triangle
Here is the assignment
"Design and implement a recursive program to determine and print up to the Nth line of Pascal’s Triangle, as shown below. Each interior value is the sum of the two values above it.
"

I am not even sure how to start this.
Any help is appreciated.
 
.
you just need pseudo code or full function?

you need to write two functions

fn pascalVal(row,column) (

if col == 0
return 1
if row == 0
return 0
else
return pascalVal((row-1)+(col-1)) + ((row-1)+(col))
)

then write one more function:

fn pascalRows(nRows) (
if nRow <0
print - need positive number

else:
loop through each i in nRow (
loop through k in nRow +1
pascalVal(i,k)
print (i,k)
)
)
)
 
.
Ugh, it's more maths than programming. Always hated it in school.

If it was ordinary Java, i could have helped
 
.
you just need pseudo code or full function?

you need to write two functions

fn pascalVal(row,column) (

if col == 0
return 1
if row == 0
return 0
else
return pascalVal((row-1)+(col-1)) + ((row-1)+(col))
)

then write one more function:

fn pascalRows(nRows) (
if nRow <0
print - need positive number

else:
loop through each i in nRow (
loop through k in nRow +1
pascalVal(i,k)
print (i,k)
)
)
)

thank you for the reply.
I should add that it needs to use an array and recursion.

Also full code.

I had this so far:

public static void main(String[] args)
{

Scanner scan = new Scanner(System.in);
int input;

System.out.println("Enter number");
input = scan.nextInt();

int[] pascal;


public static int pascalRow(int input)
{
pascal = new int[input +1];
if (input == 0)
{
pascal[0] = 1;
return pascal;
}
else
{
//I am stuck here. need to implement recursion here.
}
 
.
you only need one input == number of rows

the other input (number of columns == number of rows +1)

next, you use the number of rows to create a (x,y) array if (row,column)

you do this by:

get number of rows, loop from 1 to number of rows
then loop again from 1 to number of (rows+ 1) // this is number of columns
call pascalVal fn

this should give yoou:

for input 3:

pascalVal(1,1)
pascalVal(2,1),(2,2)
pascalVal(3,1),(3,2),(3,3)

use the pascalVal function I wrote in my earlier post.. that has the recursion.
 
.
you only need one input == number of rows

the other input (number of columns == number of rows +1)

next, you use the number of rows to create a (x,y) array if (row,column)

you do this by:

get number of rows, loop from 1 to number of rows
then loop again from 1 to number of (rows+ 1) // this is number of columns
call pascalVal fn

this should give yoou:

for input 3:

pascalVal(1,1)
pascalVal(2,1),(2,2)
pascalVal(3,1),(3,2),(3,3)

use the pascalVal function I wrote in my earlier post.. that has the recursion.
Thank you, will try now now.
 
. .
Back
Top Bottom