So the goal of this is to generate a pascal triangle using three methods: one that generates the next row, the main method, and (the one I'm having trouble on) one that shifts the array over per row to create a triangle. I'm struggling to find an algorithm that does just that. I've provided the full code for context but am really asking for help with just the printRow function.
So here it is:
package pascaltrianglegenerator;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class PascalTriangleGenerator {
public static void main(String[] args) throws IOException {
Scanner cin = new Scanner(System.in);
System.out.print("Enter the name of the output file -> ");
PrintWriter fileOut = new PrintWriter(new File(cin.next()));
System.out.print("Enter the number of rows for the triangle -> ");
int numberRows = cin.nextInt();
if(numberRows < 0) {
System.out.println("ERROR: The number of rows must be a positive integer.");
} else {
int[] currentRow = {};
for(int i = 1; i <= numberRows; i++) {
currentRow = nextRow(currentRow);
printRow(currentRow, numberRows, (numberRows - i), fileOut);
}
fileOut.close();
}
}
/**
* Computes an array containing the coefficients of the binomial (x+y)^(k+1)
* given an array containing the coefficients of the binomial (x + y)^k.
* @param currentRow an array that contains the coefficients of an expansion
* of the binomial (x + y)^k for some k greater than or equal to 0.
* @return an array containing the binomial coefficients of an expansion of
* the binomial (x + y)^(k+1) or the array [1] when currentRow is null.
*/
public static int[] nextRow(int[] currentRow) {
int[] nextRow;
if(currentRow.length == 0) {
nextRow = new int[1];
nextRow[0] = 1;
} else {
nextRow = new int[currentRow.length + 1];
for(int i = 1; i < nextRow.length/2; i++) {
nextRow[i] = currentRow[i] + currentRow[i-1];
}
int counter;
if(nextRow.length % 2 == 0) {
counter = 1;
for(int i = nextRow.length/2; i <nextRow.length - 1; i++) {
nextRow[i] = nextRow[i - counter];
counter += 2;
}
} else {
nextRow[nextRow.length/2] = currentRow[nextRow.length/2] * 2;
counter = 2;
for(int i = nextRow.length/2 + 1; i < nextRow.length - 1; i++) {
nextRow[i] = nextRow[i - counter];
counter += 2;
}
}
nextRow[0] = 1;
nextRow[nextRow.length - 1] = 1;
}
return nextRow;
}
I've tried numerous equations but can't seem to get it right.
/**
* Displays a row of the Pascal’s triangle
* @param row an array containing binomial coefficients
* @param numRows the number of rows that the triangle will have
* @param shift the number of columns by which the first coefficient, 1, of
* the next row is shifted leftward from the first coefficient on the previous row.
* @param writer a reference to a file output stream
*/
public static void printRow(int row[], int numRows, int shift, PrintWriter writer) {
String formatString = "";
for (int i = 1; i <= numRows; i++) {
formatString += row[i] + " ";
}
System.out.printf("%" + (shift) + "s%n", formatString);
writer.printf(formatString);
}
}
This is what the code returns when 12 rows are inputted:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
Exception in thread "main" java.util.FormatFlagsConversionMismatchException:
Conversion = s, Flags = 0
I have searched quite a bit and can't seem to find what I'm looking for.
Thanks in advance, Jordan
You could just iterate over the shift and print out spaces, this fixes your crash:
public static void printRow(int row[], int numRows, int shift, PrintWriter writer) {
String formatString = "";
for (int i = 0; i < row.length; i++) { // firstly, should be i = 0 and row.length is used here ...
formatString += row[i] + " ";
}
// start a new line:
System.out.println();
// print out all the space chars for the shift:
for (int j = 0; j < shift; j++) {
System.out.print(" ");
}
System.out.print(formatString);
writer.printf(formatString);
}
now you just need to take care of the formatting a little, this is the new output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1
So the double and triple digits do affect the format, but I hope this helps somewhat :)