Welcome to Dream.In.Code
Become a Java Expert!

Join 149,848 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,337 people online right now. Registration is fast and FREE... Join Now!




Need Help Formatting Pascal's Triangle

 
Reply to this topicStart new topic

Need Help Formatting Pascal's Triangle

prelic
20 Feb, 2007 - 10:38 AM
Post #1

New D.I.C Head
*

Joined: 13 Feb, 2007
Posts: 15


My Contributions
Hey, this is my code for calculating pascal's triangle, but I'm having trouble actually making it look like a triangle.


CODE

public static void printPascal(int[] array)
{
  
     System.out.println(1);

    
    for(int i = 1; i < array.length; i++)
    {
    
        for(int j = i; j > 0; j--)
        {
            array[j] += array[j - 1];
        }
        
      
        for(int j = 0; j <= i; j++)
        {
            
            System.out.print("" + array[j]);
            
        }
        System.out.println("\n\n");
    
    }    
  
}


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Need Help Formatting Pascal's Triangle
20 Feb, 2007 - 03:00 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,869



Thanked: 53 times
Dream Kudos: 550
My Contributions
Well here is what I suggest: This will work only if the triangle you are printing is small. Well it will WORK with anyway, but only look like triangle if it is small.

what we want is something that looks like:
CODE

           1
         1   1
       1   2   1
     1   3   3   1


To do this neatly we need to set element we will print to a set number of char. For the above I chose 3.

so you need to make 1 look like .1. (where the "." is a space)

each element gets seperated by a space. So the next line is ".1...1." then ".1...2...2...1." etc.

then choose an offset say 10, then add that many spaces to the first line, and then subtract that by 1 for each additional line.

I admit that this is not pretty but it would work.

Hints. The Integer class has a .toString() function which will convert your ints to strings.
User is offlineProfile CardPM
+Quote Post

005240g
RE: Need Help Formatting Pascal's Triangle
20 Feb, 2007 - 05:02 PM
Post #3

New D.I.C Head
*

Joined: 20 Feb, 2007
Posts: 6


My Contributions
Hi, This is the code I used for a project on this topic. You should find your answer here.


public class PascalTriangle
{
/**
Constructs a triangle of a given height.
@param height the height (0-based)
*/
public PascalTriangle(int height)
{
triangleString = "";
int spacesToSkip = 2 * height; // spaces to skip at the beginning of each row
// start a loop over the number of rows

for (int n = 0; n <= height; n++)
{
skip(spacesToSkip); // space to make a triangle
spacesToSkip = spacesToSkip - 2;

for (int k = 0; k <= n; k++)
{
int comb = combination(n, k);
String out = " " + comb; // pad to a length of 4
triangleString = triangleString + out.substring(out.length() - 4);
}

triangleString = triangleString + "\n";
}
}

/**
Skip n spaces on a line.
@param n - the number of spaces to skip
*/
public void skip(int n)
{
for (int i = 0; i <= n; i++)
triangleString = triangleString + " ";
}

/**
Calculate n factorial.
@param n calculate the factorial of n
@return n!
*/
public static int factorial(int n)
{
int product = 1; // accumulator for the running product

for (int i = 1; i <= n; i++)
product = product * i ;
return product;
}

/**
Calculate the number of combinations of n things taken
k at a time (n choose k).
@param n the number of items to choose from
@param k the number of items chosen
@return n choose k
*/
public static int combination(int n, int k)
{
int comb = factorial(n) / (factorial(k) * factorial(n - k));
return comb;
}

public String toString()
{
return triangleString;
}

private String triangleString;
}
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:21AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month