Free2Code
The majority of forums are now only available as archives, which means posting/editing is disabled.

The Anything and Everything forum is still open.
 
Time: 2013-05-18, 10:24pm
Help with pascal triangle program
Subject: Help with pascal triangle program  ·  Posted: 2005-02-20, 04:04pm
Rank: ? (1)
Member #: 23526
plz guyz im really dumb, but i really want to get this done

im trying to let the user type in the row they want to see, and print it out, thats what i have so far..

thx



import cs1.Keyboard;

public class Triangle
{
public static final int EVEN = 2;

public static void main(String[] args)
{
int numRow;

System.out.println("Which row do you want to see?";
numRow = Keyboard.readInt();

int[][] triangle = new int [numRow][];

for (int i = 0; i<numRow; i++)
{
triangle[i] = new int[i+1];

triangle[i][0] = 1;

for (int j=1; j<i; j++)
{
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
}

triangle[i][i] = 1;
}



System.out.println(triangle[numRow]);
}
}

 
  Reply to this ·  Post link ·  Top
Subject: Re: Help with pascal triangle program  ·  Posted: 2005-03-05, 03:30pm
Rank: ? (171)
Member #: 11947
try this instead:

The n row is a array of binomial coeffecients, which essentially are combinations.

the list is: nC0, nC1, nC2... nCn

but to reduce processing time you can only have to do the floor of half of n, then repeat backwards. This might be an easier algorthim, and would look something like this

Code:
  1. main(String args[]){
  2. System.out.println("Please enter the row number: ");
  3. int n = getRow(); //method to get the row number
  4. int i = 0;
  5. int[] retval = new int[(int)n/2];
  6. do{
  7.      retval[i] = combination( n, i) // n things taken i times
  8.      i++;
  9. }while(i-1 != (int)(n/2) );
  10. for(int i = 0; i < retval.length; i++){
  11.      System.out.print(retval[i]);
  12.      System.out.print(" ");
  13. }
  14. for(int i = retval.length; i >= 0; i--){
  15.      System.out.print(retval[i]);
  16.      System.out.print(" ");
  17. }
  18. }


I'm sure the combination and getRow methods are easy enough to impliment.


 
  Reply to this ·  Post link ·  Top
Subject: Re: Help with pascal triangle program  ·  Posted: 2005-05-26, 10:28pm
Rank: Unregistered
*
***
*****

How to print like the above shape in Java? Can any one help me..?
 
  Reply to this ·  Post link ·  Top
Subject: Re: Help with pascal triangle program  ·  Posted: 2005-06-09, 11:12am
Rank: ? (171)
Member #: 11947
depends on the length you want to print out.

since you know that.. then the first row would print LENGTH-1 spaces, the second LENGTH-2.... 0.

as long as it's a monospaced font you'll be fine.

if you wanted to print until someone hit a stop button, say to a textfield or file, then you'll have to read the file, and print a single space before each line everytime you added a line.

» Post edited 2005-06-09, 11:13am by Umojan.

 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

Penguino AVR

Want to learn about robotics or microcontrollers?
Check out the Penguino AVR from our friends at
Icy Labs