Solved

java pascal triangle array program wont compile

Posted on 2009-07-12
6
484 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 108
JList custom Cell Renderer refresh 15 57
jboss wildfly 10.1 10 94
Java: The Public Class Main 4 16
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…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

777 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