Solved

java pascal triangle array program wont compile

Posted on 2009-07-12
6
480 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 125 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 125 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
This video teaches viewers about errors in exception handling.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now