Solved

java pascal triangle array program wont compile

Posted on 2009-07-12
6
489 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

623 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