1. Senior Member
Join Date
Feb 2009
Posts
117
Rep Power
0

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

2. 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.
Last edited by Fubarable; 12-04-2010 at 02:32 PM.

3. Senior 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.IOException;

public class Pascals {

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?");
nNumberOfValuesLeft = nValues.length;
//System.out.println(numberOfValuesLeft); //nakukuha ung entry kung ilan ung kukunin
}

public void takeInputs() throws IOException {

for (int start=0; start<nValues.length; start++) {
//System.out.println(values[start]); //nakukuha ung mga values
}

}

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;}
}
}

}```

4. 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. Senior Member
Join Date
Feb 2009
Posts
117
Rep Power
0
thanks! i will try this one :D

Posting Permissions

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