Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java pascal triangle array program wont compile

Posted on 2009-07-12
6
Medium Priority
?
492 Views
Last Modified: 2013-11-23
hi everyone,

this program is supposed to take a number 'rowCount' as input from a user, then use that number to create an equal number of rows in a pascal triangle. the code initializes pascal0[] and pascal1[] in the PascalTri class to remember values in the current (1) and previous (0) rows.
it's also supposed to print these rows to the console, but i must be doing something wrong because it hasnt worked. can anyone point me in the right direction?

1
11
121
1331
14641
...
public class PascalTri{
 
        public int rowCount;
        int[] pascal1 = new int[0];
        int[] pascal0 = {};
 
        public PascalTri(int row){
            row = rowCount;
            for(int i=0; i<row; i++){
                pascal1[0] = 1;
                pascal1[pascal0.length] = 1;
                pascal1 = new int[pascal0.length+1];
                for(int j=1; j<pascal0.length;j++){
                    pascal1[j] = pascal0[j] + pascal0[j-1];
                }
                pascal0 = pascal1;
                }
            }
       
        public void displayRows(){
            System.out.println(pascal1);
        }
    }
 
=============================================================================
 
import java.util.Scanner;
 
    public class PascalDriver{
      public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of rows");
        int rowCount = scan.nextInt();
        PascalTri t = new PascalTri(rowCount);
        t.displayRows();
      }
    }

Open in new window

0
Comment
Question by:bpiche
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24836631
you need to use a loop in displayRows to display the triangle
0
 

Author Comment

by:bpiche
ID: 24836649
how could I do that short of embedding the displayRows function within the PascalTri function?

thanks again
0
 
LVL 92

Expert Comment

by:objects
ID: 24836703
you need to use a 2d array, following shows how to use it with your current code

import java.util.Scanner;

public class PascalTri {

      int[][] pascal1 = null;

      public PascalTri(int row) {
            
            // create empty triangle
            
            pascal1 = new int[row][];
            
            // initialise first row
            
            pascal1[0] = new int[1];
            pascal1[0][0] = 1;
            
            // calculate remaining rows
            
            for (int i = 1; i < row; i++) {
                  pascal1[i] = new int[i+1];
                  pascal1[i][0] = 1;
                  pascal1[i][i] = 1;
                  for (int j = 1; j < pascal1[i].length; j++) {
                        int[] pascal0 = pascal1[i-1];
                        //pascal1[j] = ...;
                  }
            }
      }

      public void displayRows() {
            for (int i=0; i
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bpiche
ID: 24836768
is there any way i can do this without 2d arrays? sorry i forgot to mention that >.<
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24836774
you'd need to print it out as you calculate instead of printing it out from a separate method
0
 
LVL 20

Assisted Solution

by:a_b
a_b earned 500 total points
ID: 24837116
What  I would suggest is that you move the logic from the constructor of the PascalTri class to the displayRows(). Within displayRows(), run loops as shown below -

for(i=0;i<rowCount;i++)
{
     for(j=0;j<i;j++)
         //Print the values 1 upto rowCount
     for(j=i;j>0;--+)
         //Print the values from  rowCount down to 1
}

This is in a nutshell, you might have to fine tune the indexes just a bit.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question