Format of Pascal's triangle [duplicate] - java

This question already has answers here:
Pascal's Triangle Format [duplicate]
(8 answers)
Closed 3 years ago.
I have to print Pascal's Triangle, so it should have number 1 on each side, and format of triangle should be even on each side (now it's longer on the right side). My code:
public static void main(String[] args) {
printPascalTriangle(10);
}
public static void printPascalTriangle(int size) {
for (int i = 0; i <= size; i++) {
System.out.println();
for (int j = 0; j <= (size - i); j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(" " + (i + j));
}
}
}
And the output is:
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
6 7 8 9 10 11 12
7 8 9 10 11 12 13 14
8 9 10 11 12 13 14 15 16
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19 20
Why it doesn't sum up? And why loop doesn't format spaces properly?

You should use printf method instead of print with proper arguments to format your output:
public static void main(String[] args) {
printPascalTriangle(10);
}
public static void printPascalTriangle(int rows) {
for (int i = 0; i < rows; i++) {
int number = 1;
System.out.printf("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
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

Related

Calculating total weight of MST using Prim's and a heap

Attempting to implement a method that builds a MST using prim's and a min heap (using priority queue) and returns the total weight of the MST.
My code seems to produce different results to what is expected, however, looking at my code it seems to be fundamentally correct and follows closely to other examples online.
for (int v = 0; v < size; v++) {
key[v] = Integer.MAX_VALUE;
pred[v] = null;
visited[v] = false;
}
int s = 0;
key[s] = 0;
for (int v = 0; v < size; v++) {
minHeap.add(new T(v, key[v], pred[v]));
}
while (!minHeap.isEmpty()) {
int u = minHeap.poll().vertex;
for (int v = 0; v < size; v++) {
if (g.getWeight(u, v) > 0 && !visited[v]) {
if (g.getWeight(u, v) < key[v]) {
key[v] = g.getWeight(u, v);
pred[v] = u;
minHeap.add(new T(v, key[v], pred[u]));
}
}
}
visited[u] = true;
}
int distance = 0;
for (int i = 0; i < size; i++) {
distance += key[i];
if (!visited[i])
return -1;
}
return distance;
current input is an adjacency array:
5 16 5 8 19 9 8 1 5 8
16 10 19 7 7 5 15 4 7 18
5 19 1 7 1 20 12 15 9 2
8 7 7 14 11 18 13 5 11 14
19 7 1 11 0 15 10 17 0 8
9 5 20 18 15 0 11 20 10 7
8 15 12 13 10 11 8 14 20 20
1 4 15 5 17 20 14 12 0 8
5 7 9 11 0 10 20 0 5 7
8 18 2 14 8 7 20 8 7 15
expected output is: 36
current output is: 32
Perhaps your minHeap is sorting items to the largest key[v] rather than smallest. Maybe try
minHeap.add(new T(v, Integer.MAXVALUE - key[v], pred[u]));
The problem was in where the visited[u] = true statement was.
moving that statement to after polling for vertex with highest priority in minHeap and before the loop checking for adjacent vertices fixed it.

Bubble sort with output

So I have edited it some and am getting almost exactly what I want. The only problem I am having now is that I am getting a line of output that I don't want. I feel like the fix here is simple but my brain is fried right now.
static void bubbleSort(int[] myArray) {
int n = myArray.length;
int temp = 0;
int counter = 0;
for (int i = 0; i < n; i++) {
int k;
for (k = 0; k < n; k++) {
System.out.print(myArray[k] + "|");
}
System.out.println(" Num swaps: " + counter);
for (int j = 1; j < (n - i); j++) {
if (myArray[j - 1] > myArray[j]) {
//swap elements
temp = myArray[j - 1];
myArray[j - 1] = myArray[j];
myArray[j] = temp;
counter++;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] myArray = new int[10];
for (int i = 0; i < 10; i++) {
System.out.print("Enter slot " + i + ": ");
myArray[i] = sc.nextInt();
}
bubbleSort(myArray);
}
Here is an example of what I get:
Enter slot 0: 10
Enter slot 1: 9
Enter slot 2: 8
Enter slot 3: 7
Enter slot 4: 6
Enter slot 5: 5
Enter slot 6: 4
Enter slot 7: 3
Enter slot 8: 2
Enter slot 9: 1
10|9|8|7|6|5|4|3|2|1| Num swaps: 0
9|8|7|6|5|4|3|2|1|10| Num swaps: 9
8|7|6|5|4|3|2|1|9|10| Num swaps: 17
7|6|5|4|3|2|1|8|9|10| Num swaps: 24
6|5|4|3|2|1|7|8|9|10| Num swaps: 30
5|4|3|2|1|6|7|8|9|10| Num swaps: 35
4|3|2|1|5|6|7|8|9|10| Num swaps: 39
3|2|1|4|5|6|7|8|9|10| Num swaps: 42
2|1|3|4|5|6|7|8|9|10| Num swaps: 44
1|2|3|4|5|6|7|8|9|10| Num swaps: 45
That first line of output where it just repeats what the user input and says 0 swaps. I don't want that.
Just changed the position of the for loops. Hope this is the output you actually want :).
static void bubbleSort(int[] myArray) {
int n = myArray.length;
int temp = 0;
int counter = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (myArray[j - 1] > myArray[j]) {
// swap elements
temp = myArray[j - 1];
myArray[j - 1] = myArray[j];
myArray[j] = temp;
counter++;
}
}
int k;
for (k = 0; k < n; k++) {
System.out.print(myArray[k] + "|");
}
System.out.println(" Num swaps: " + counter);
}
}
Algorithm with two nested streams: Bubble sort with step-by-step output Java 8
Bubble sort with step-by-step output
The outer do-while-loop repeats until the array is sorted, and the inner for-loop passes through the array, swapping the unordered adjacent elements. The output is the swapped elements in the inner loop, grouped by passes in the outer loop.
public static void main(String[] args) {
int[] arr = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
bubbleSort(arr);
}
public static void bubbleSort(int[] arr) {
// counters
int passes = 0, swaps = 0;
// marker
boolean swapped;
// repeat the passes through the array until
// all the elements are in the correct order
do {
// output the beginning of the pass and increase the counter of passes
System.out.print((passes == 0 ? "<pre>" : "<br>") + "Pass: " + passes++);
swapped = false;
// pass through the array and
// compare adjacent elements
for (int i = 0; i < arr.length - 1; i++) {
// if this element is greater than
// the next one, then swap them
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
// output the array and increase the counter of swaps
System.out.print(outputSwapped(arr, i, i + 1, swaps++));
}
}
// if there are no swapped elements at the
// current pass, then this is the last pass
} while (swapped);
// output total
System.out.print("<br>Total: Passes=" + passes);
System.out.println(", swaps=" + swaps + "</pre>");
}
static String outputSwapped(int[] arr, int e1, int e2, int counter) {
StringBuilder sb = new StringBuilder("<br>");
for (int i = 0; i < arr.length; i++) {
if (i == e1 || i == e2) {
// swapped elements are in bold
sb.append("<b>").append(arr[i]).append("</b>");
} else {
// other elements
sb.append(arr[i]);
}
sb.append(" ");
}
return sb.append("| ").append(counter).toString();
}
Output:
Pass: 09 10 8 7 6 5 4 3 2 1 | 09 8 10 7 6 5 4 3 2 1 | 19 8 7 10 6 5 4 3 2 1 | 29 8 7 6 10 5 4 3 2 1 | 39 8 7 6 5 10 4 3 2 1 | 49 8 7 6 5 4 10 3 2 1 | 59 8 7 6 5 4 3 10 2 1 | 69 8 7 6 5 4 3 2 10 1 | 79 8 7 6 5 4 3 2 1 10 | 8Pass: 18 9 7 6 5 4 3 2 1 10 | 98 7 9 6 5 4 3 2 1 10 | 108 7 6 9 5 4 3 2 1 10 | 118 7 6 5 9 4 3 2 1 10 | 128 7 6 5 4 9 3 2 1 10 | 138 7 6 5 4 3 9 2 1 10 | 148 7 6 5 4 3 2 9 1 10 | 158 7 6 5 4 3 2 1 9 10 | 16Pass: 27 8 6 5 4 3 2 1 9 10 | 177 6 8 5 4 3 2 1 9 10 | 187 6 5 8 4 3 2 1 9 10 | 197 6 5 4 8 3 2 1 9 10 | 207 6 5 4 3 8 2 1 9 10 | 217 6 5 4 3 2 8 1 9 10 | 227 6 5 4 3 2 1 8 9 10 | 23Pass: 36 7 5 4 3 2 1 8 9 10 | 246 5 7 4 3 2 1 8 9 10 | 256 5 4 7 3 2 1 8 9 10 | 266 5 4 3 7 2 1 8 9 10 | 276 5 4 3 2 7 1 8 9 10 | 286 5 4 3 2 1 7 8 9 10 | 29Pass: 45 6 4 3 2 1 7 8 9 10 | 305 4 6 3 2 1 7 8 9 10 | 315 4 3 6 2 1 7 8 9 10 | 325 4 3 2 6 1 7 8 9 10 | 335 4 3 2 1 6 7 8 9 10 | 34Pass: 54 5 3 2 1 6 7 8 9 10 | 354 3 5 2 1 6 7 8 9 10 | 364 3 2 5 1 6 7 8 9 10 | 374 3 2 1 5 6 7 8 9 10 | 38Pass: 63 4 2 1 5 6 7 8 9 10 | 393 2 4 1 5 6 7 8 9 10 | 403 2 1 4 5 6 7 8 9 10 | 41Pass: 72 3 1 4 5 6 7 8 9 10 | 422 1 3 4 5 6 7 8 9 10 | 43Pass: 81 2 3 4 5 6 7 8 9 10 | 44Pass: 9Total: Passes=10, swaps=45
See also: Bubble sort output is incorrect

Creating Triangular Multiplication Table using Do-while Loop

I want to ask a question or a probable favor on how am I going to make my program coding "do-while loop" in creating a Triangular Multiplication. Is there a probable way on to create such thing without using any other statement?
public class Main {
static void ssbr(int n) {
int i = 1;
do{
System.out.printf("%4d", n * i);
i = i + 1;
} while(i <= 7);
System.out.println("");
}
public static void main(String[] args) {
int i = 1;
do{
ssbr(i);
i = i + 1;
} while (i <= 7);
}
}
Output it gave:
1 2 3 4 5 6 7
2 4 6 8 10 11 12
3 6 9 12 15 18 21
4 8 12 16 20 24 30
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49
Output I wanted:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
You can do it with the following algorithm:
You have to do it 7 times and therefore you can use a loop that should run 7 times.
Each row starts with the row number, and run for row number * row number times with a step-value equal to the row number.
Given below is the implementation of this algorithm using for loop and I leave it to you to implement it using the do-while loop (as it seems to be your homework 😀)
public class Main {
public static void main(String[] args) {
int n = 7;
for (int row = 1; row <= n; row++) {
for (int col = row; col <= row * row; col += row) {
System.out.printf("%-4d", col);
}
System.out.println();
}
}
}
Output:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49

Pascaline triangle logic not working in java

I am trying to make a java program to print the Pascaline triangle. But it is not working properly. The code is provided below :
int rows=10;
int[] array=new int[10], temp=new int[10];
array[0]=1;
temp[0]=1;
System.out.println(1);
for(int i=1;i<rows;i++)
{
for(int j=1;j<=i;j++)
{
temp[j]=array[j-1]+array[j];
}
for(int term:temp)
{
System.out.print(term+"\t");
}
System.out.println();
array=temp;
}
It is giving the following output :
1
1 1
1 2 3
1 3 5 5
.....
Please tell what's wrong with the code.
Pascaline triangle is not factorial serial
A proposal is (warning I am not a Java programmer, please don't be rude with me if something is stupid / can be improved easily) :
public class Pascaline {
public static void main(String args[]) {
int n = 10, i, j;
int [] f = new int[n];
f[0] = 1;
for (i = 1; i != n; i++)
f[i] = f[i - 1] * i;
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++)
System.out.print((f[i] / (f[i - j] * f[j])) + " ");
System.out.println();
}
}
}
Compilation and execution :
pi#raspberrypi:/tmp $ javac Pascaline.java
pi#raspberrypi:/tmp $ java Pascaline
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
and to be a little prettier :
public class Pascaline {
public static void main(String args[]) {
int n = 10, i, j;
int [] f = new int[n];
f[0] = 1;
for (i = 1; i != n; i++)
f[i] = f[i - 1] * i;
for(i = 0; i < n; i++) {
for(j = 0; j < n-i; j++)
System.out.print(" ");
for(j = 0; j <= i; j++)
System.out.print((f[i] / (f[i - j] * f[j])) + " ");
System.out.println();
}
}
}
Compilation and execution :
pi#raspberrypi:/tmp $ javac Pascaline.java
pi#raspberrypi:/tmp $ java Pascaline
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

Nested for loop not incrementing by 1

In the below code block:
public static void main(String[] args) {
int i;
int x = 0;
int count;
int sum = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter The Table Operator (+, -, *, /, %, or R)");
String operator = scan.nextLine();
System.out.println("Enter The Smallest Operand For the Table:");
int small = scan.nextInt();
System.out.println("Enter The Largest Operand For the Table");
int large = scan.nextInt();
for(i = 0; i < 12; i++) {
for(x = 0; x <= large; x ++)
System.out.printf("%4d", x + small);
System.out.printf("\n");
x++;
large++;
}
}
The code outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13
but I want it to look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
2 3 4 5 6 7 8 9 10 11 12 13 14
3 4 5 6 7 8 9 10 11 12 13 14 15
4 5 6 7 8 9 10 11 12 13 14 15 16
5 6 7 8 9 10 11 12 13 14 15 16 17
6 7 8 9 10 11 12 13 14 15 16 17 18
7 8 9 10 11 12 13 14 15 16 17 18 19
8 9 10 11 12 13 14 15 16 17 18 19 20
9 10 11 12 13 14 15 16 17 18 19 20 21
10 11 12 13 14 15 16 17 18 19 20 21 22
11 12 13 14 15 16 17 18 19 20 21 22 23
12 13 14 15 16 17 18 19 20 21 22 23 24
It's basically an addition table. I'm trying to get the x value and the value of "large" to implement by one on each loop. I'm pretty sure I'm using the for loop incorrectly but I haven't been able to find out how.
for (int row = 1; row <= 12; row++) {
for (int col = row; col < row + 13; col++)
System.out.print(col != row ? " " + col : col);
System.out.println();
}
This pretty simple trianlge. Just inline all temporary variables like row and 'col' into the loops. Be simple.
int large = 10;
int small = 0;
int x,i;
for( i = 0; i < large; i++) {
for( x = 1; x <= large; x ++)
System.out.printf("%d ", x + small);
System.out.printf("\n");
x++;
small++;
}
Change "%4d" to "%d ". %4d makes every number 4 spaces long no matter what but you wanted the number and one space.
The last line in your outer for-loop should be small++ instead of large++ to offset the start value each time.
Your loop looks right. You are incrementing the wrong variables.
for(i = 0; i < large; i++) {
for(x = 0; x <= large; x ++){
System.out.printf("%4d", x + small);
}
System.out.printf("\n");
small++;
}
output:
1 2 3 4 5 6 7 8 9 10 11 12 13
2 3 4 5 6 7 8 9 10 11 12 13 14
3 4 5 6 7 8 9 10 11 12 13 14 15
4 5 6 7 8 9 10 11 12 13 14 15 16
5 6 7 8 9 10 11 12 13 14 15 16 17
6 7 8 9 10 11 12 13 14 15 16 17 18
7 8 9 10 11 12 13 14 15 16 17 18 19
8 9 10 11 12 13 14 15 16 17 18 19 20
9 10 11 12 13 14 15 16 17 18 19 20 21
10 11 12 13 14 15 16 17 18 19 20 21 22
11 12 13 14 15 16 17 18 19 20 21 22 23
12 13 14 15 16 17 18 19 20 21 22 23 24
int small = 1;
int large = 12;
for(int i = 0; i < 12; i++) {
for(int x = i; x <= i + large; x ++)
{
System.out.printf("%4d", x + small);
}
System.out.printf("\n");
}
checkout this simple change by changing the x ranges from i -> i + large which will avoid increment large ( because it is not need as in every iteration i also increase)
what you are trying to is in every iteration of i and x you are increment it at the end of the print operation that wont effect exactly as you want because for every iteration the value you print should iterate and print so change the x value to i, which will get incremented in every iteration and use it till i + large (where large is the number you obtain as maximum limit)
Note this can be achieved since small, and large are constant through out the loop

Categories