Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java pascal triangle array program wont compile

Posted on 2009-07-12
6
Medium Priority
?
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

715 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