pascal's triangle like problem

• 12-04-2010, 03:27 PM
kulangotski
pascal's triangle like problem
hi,

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
• 12-04-2010, 03:28 PM
Fubarable
Quote:

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

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.
• 12-05-2010, 01:38 AM
kulangotski
sorry, newbie here

it is kinda like a spaghetti

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();                    }     } }```
• 12-05-2010, 03:48 AM
Fubarable
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.
• 12-05-2010, 09:15 AM
kulangotski
thanks! i will try this one :D