Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Pascal's Triangle Program

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Pascal's Triangle Program

    I wrote a pascal's triangle program with an imbedded 'for' loop and a Factorial class that computes the factorial of a number. When it gets to the third line of the triangle the problems arise. If anyone could help I would greatly appreciate it. Here is my code:
    // --------------------------------------------------------------------
    // This program is intended to make Pascal's triangle.
    // --------------------------------------------------------------------
     
    package PascalTriangle;
     
    public class PascTri {
    	public static void main (String[] args){
    		Factorial factorial = new Factorial();
    		int n, k, subtract;
    		// The number of rows is set to five for now
    		int rows = 5;
    		long term1;
    		long term2;
    		long term3;
    		long term4;
    		long endTerm;
    		// 'n' represents the line in Pascal's triangle
    		// 'k' represents the term in a line in Pascal's triangle
     
    		for(n = 0; n <= rows-1; n++) {
    			for(k = 0; k <= n; k++ ) {
    				subtract = n-k;
    				term1 = factorial.Factorial(n);
    				term2 = factorial.Factorial(k);
    				term3 = factorial.Factorial(subtract);
    				term4 = term2 * term3;
    				endTerm = term1/term4;
    				System.out.print(endTerm + "   ");
    				// I typed this line simply to help troubleshoot
    				System.out.print("Term1: " + term1 + " Term2: " + term2 + " Term3: " + term3 + "   |   ");
    			}
    			System.out.println();
    		}
    	}
    }
     
    // **********************************************
    // Factorial Class
    // **********************************************
    class Factorial {
    	private long hideFact;
    	long i;
    	long finFact = 1;
     
    	long Factorial(long factNum) {
    		hideFact = factNum;
    		if (hideFact == 1)
    			finFact = 1;
    		else if (hideFact == 0)
    			finFact = 1;
    		else
    			for(i = hideFact; i >= 1; i--) {
    				finFact *= i;
    			}
    		return finFact;
    	}
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Pascal's Triangle Program

    When it gets to the third line of the triangle the problems arise.
    What problems? (Describe both the actual and intended behaviour of your program).

    -----

    It's a good idea to keep to Java coding conventions: start package names, methods and variables with a lowercase letter and don't give a method the same name as a constructor as you do with Factorial().

    And test everything!

    public class PascTri {
        public static void main (String[] args){
            Factorial factorial = new Factorial();
            for(int test = 0; test < 10; test++) {
                System.out.printf("%d --> %d%n", test, factorial.Factorial(test));
            }
            int n, k, subtract;
            // etc

    Is the factorial method doing what you expect?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pascal's Triangle Program

    Thank you for the small tips.
    This program is intended to create a Pascal's triangle, for example:
    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    ... etc

    That is not the output that is produced. Instead, if this code is run (minus that small println line I typed for testing) this is produced:
    1
    1 1
    1 4 0
    1 18 0 0
    1 96 0 0 0

    When my Factorial class is run separately, it works perfectly. There are no errors whatsoever.

    I've tried testing every little part and it seems like the error comes in the imbedded for loop. I had it print all of the values of 'n' throughout the imbedded loop, and they are correct. But when factorial.Factorial(n) is computed for the same value of n, the results are different. I can't figure out why. Can something in my main class affect the Factorial class?

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Pascal's Triangle Program

    When my Factorial class is run separately, it works perfectly.
    Did you try the three lines I added at the start of main()?

    The output I get is:

    0 --> 1
    1 --> 1
    2 --> 2
    3 --> 12
    4 --> 288
    5 --> 34560
    6 --> 24883200
    7 --> 125411328000
    8 --> 5056584744960000
    9 --> 8705808953839190016

    Those numbers are not the factorials, so what exactly is the factorial method supposed to be returning?

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    thedman3 (February 4th, 2012)

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pascal's Triangle Program

    I figured it out. When the method Factorial is run just once, the correct value is attained. Because Factorial returns the value of finFact, when Factorial is run again it keeps the previous value of finFact and multiplies that by the correct value. I simply had to change the Factorial class to make finFact = 1 every time. My new Factorial class looks like this:
    class Factorial {
    	private long hideFact;
    	private long i;
    	private long finFact = 1;
     
    	long doFactorial(long factNum) {
    		hideFact = factNum;
    		if (hideFact == 0)
    			finFact = 1;
    		else {
    			finFact = 1;
    			for(i = hideFact; i >= 1; i--) {
    				finFact = finFact * i;
    			}
    		}	
    		return finFact;
    	}
    }

    I changed the name of the method Factorial to doFactorial as you recommended. Thank you so much for your help.

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Pascal's Triangle Program

    You're welcome - I'm glad you've got it figured out.

    In some ways having instance variables can be a menace: as you've found they retain a "memory" from call to call. It's probably best to minimise them.

    In the case of doFactorial() there is nothing that has to be "remembered" from call to call. So I'd be looking for a version that used only local variables (ie variables that are declared and used within the doFactorial() method). Of course if you have working code you may not feel inclined to change anything! But I'm just saying that it was having instance variables rather than local variables - specifically finFact - that bit you here.

Similar Threads

  1. Triangle Printing - Java Program
    By mparthiban in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 10:00 AM
  2. help me draw a triangle....
    By beandip408 in forum Object Oriented Programming
    Replies: 10
    Last Post: October 28th, 2010, 05:49 PM
  3. [HELP] TRIANGLE!
    By kramista in forum Loops & Control Statements
    Replies: 10
    Last Post: July 29th, 2010, 12:58 PM
  4. Triangle Printing - Java Program
    By mparthiban in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 18th, 2010, 05:35 AM
  5. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM

Tags for this Thread