Results 1 to 5 of 5
Thread: pascal's triangle like problem
 12042010, 02:27 PM #1Senior Member
 Join Date
 Feb 2009
 Posts
 117
 Rep Power
 0
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

Last edited by Fubarable; 12042010 at 02:32 PM.
 12052010, 12:38 AM #3Senior Member
 Join Date
 Feb 2009
 Posts
 117
 Rep Power
 0
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(); } } }

One possible way to solve this is to use a twodimensional 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.
 12052010, 08:15 AM #5Senior Member
 Join Date
 Feb 2009
 Posts
 117
 Rep Power
 0
Similar Threads

triangle
By Shyamz1 in forum New To JavaReplies: 4Last Post: 11072010, 06:12 PM 
Pascal's Triangle formatting.
By dalangley in forum New To JavaReplies: 13Last Post: 02192010, 08:10 AM 
While loop triangle problem
By cmb3587 in forum New To JavaReplies: 4Last Post: 03022009, 12:53 PM 
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01102009, 02:08 PM 
Java Zelda Triangle Problem
By Hevonen in forum New To JavaReplies: 3Last Post: 10122008, 08:21 PM
Bookmarks