Results 1 to 5 of 5
  1. #1
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default pascal's triangle like problem

    hi,

    thank you for reading this thread

    our instructor gave us a problem like this. you need to add up all the user's input and make it look like a triangle

    9
    5 4
    2 3 1

    i already did the branching and adding but it turns out upside down like this

    *****
    ***
    *

    i was wondering if there is an easy fix for this one? thanks! :D

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kulangotski View Post
    i was wondering if there is an easy fix for this one? thanks! :D
    Yep, fix your code.

    You've got the code, and we don't, so presently, that's about all we can recommend! :)

    You will probably want to show us your code.
    Last edited by Fubarable; 12-04-2010 at 03:32 PM.

  3. #3
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    sorry, newbie here

    it is kinda like a spaghetti

    Java Code:
    package praktisan;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    
    public class Pascals {
    
        InputStreamReader stream = new InputStreamReader(System.in);
        BufferedReader buffer = new BufferedReader(stream);
        int[] nValues; // holds the user inputs
        int nNumberOfValuesLeft; //holds the number of inputs by the user, decreases as the values get added up
    
        public static void main (String[] args) throws IOException {
            Pascals pascals = new Pascals();
            pascals.getNoOfInputs();
            pascals.takeInputs();
            pascals.printIt();
        }
    
        public void getNoOfInputs() throws IOException {
            System.out.println("How many numbers do you want to type in?");
            nValues = new int[Integer.parseInt(buffer.readLine())];
            nNumberOfValuesLeft = nValues.length;
            //System.out.println(numberOfValuesLeft); //nakukuha ung entry kung ilan ung kukunin
        }
       
        public void takeInputs() throws IOException {
            System.out.println("Please type in the numbers");
    
            for (int start=0; start<nValues.length; start++) {
                nValues[start] = Integer.parseInt(buffer.readLine());
                //System.out.println(values[start]); //nakukuha ung mga values
            }
    
    
        }
    
        public void addThemUp() {
    
            nNumberOfValuesLeft -= 1;//decrease the number of values to be added
            
            for (int start=0;start<nNumberOfValuesLeft;start++) {
                nValues[start] = nValues[start] + nValues[start+1];
                
            }
            
        }
    
        public void printIt() {
    
            for (int x = -1; x < nValues.length - nNumberOfValuesLeft; x++) {
                for (int a = 0; a<=nValues.length - nNumberOfValuesLeft; a++) {
                    System.out.print(" ");
                    
                }
                
                for (int w = 0; w < nNumberOfValuesLeft; w++) {
                    System.out.print(nValues[w] + " ");
                    
                }
                System.out.println();
    
                if (nNumberOfValuesLeft == 0) { break;}
                addThemUp();            
            }
        }
    
    
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One possible way to solve this is to use a two-dimensional ragged array to hold your calculations. This array would have the same number of rows as user inputs (nValues in your program). The first (0th) row would have nValues items or columns. The next row would hold nValues - 1 items and would hold the sums from the user inputs. The next row would hold nValues - 2 items and would hold the sum of sum,... on and on until you have a row with 1 item. Then you could print out the results in any order you desire.

  5. #5
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

Similar Threads

  1. triangle
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 11-07-2010, 07:12 PM
  2. Pascal's Triangle formatting.
    By dalangley in forum New To Java
    Replies: 13
    Last Post: 02-19-2010, 09:10 AM
  3. While loop triangle problem
    By cmb3587 in forum New To Java
    Replies: 4
    Last Post: 03-02-2009, 01:53 PM
  4. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 03:08 PM
  5. Java Zelda Triangle Problem
    By Hevonen in forum New To Java
    Replies: 3
    Last Post: 10-12-2008, 09:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •