I have been trying to find a way to print an inverted pyramid that has a certain number sequence. The sequence needed is as follows as well as what I currently have.
The prompt asked to write a method that takes two numbers and create an inverted pyramid with the first row having a length of the first integer and starting with the number entered second. Then only have the sequence start at 1 after 9 is reached.
Needed: Currently Have:
1 2 4 7 2 7 4 1 2 3 4 5 6 7
3 5 8 3 8 5 8 9 1 2 3 4
6 9 4 9 6 5 6 7 8 9
1 5 1 7 1 2 3 4
6 2 8 5 6 7
3 9 8 9
1 1
static int plotTriangle(int a, int b){
int num = b;
for (int row = a; row >= 0; row--){
for (int i = a; i - row >= 0; i--){
System.out.print(" ");
num += (num+a-row);
num -= 2;
}
for (int i = 0; i <= row; i++){
num++;
while (num >= 10){
num -= 9;
}
System.out.print(num + " ");
}
System.out.println();
}
return 0;
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Enter length: ");
int l = in.nextInt();
System.out.print("Enter Start: ");
int s = in.nextInt();
int triangle = plotTriangle(l, s);
}
Try this:
public static void main(String[] args) {
int length = 7;
int[][] numbers = new int[length][length];
int count = 1;
for(int i = 0; i < numbers.length; ++i) {
for(int j = 0; j < (i+1); ++j) {
numbers[i][j] = count++;
if(count > 9)
count = 1;
}
}
for(int i = 0; i < numbers.length; ++i) {
for(int j = 0; j < numbers.length; ++j) {
if(numbers[j][i] == 0)
System.out.print(" ");
else
System.out.print(numbers[j][i]);
System.out.print(" ");
}
System.out.println();
}
}
This will give you your result. Note that I didn't include the dynamic part with the scanner. I used the length and the start-number as constants.
Explanation:
In the first loop I am basically just storing the numbers in an array. And in the second loop this array is printed in a different order.
Related
I want to program a Magic Square wherein the utilization of arrays is at place but when i want to run it, it shows Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MagicSquare.main(MagicSquare.java:6)
What should I do? It would show
4 9 2 7 1 6
3 5 7 not 3 5 7
8 1 6 4 9 2
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int n = Integer.parseInt(args[3]);
if (n % 2 == 0) throw new RuntimeException("n must be odd");
int[][] magic = new int[n][n];
int row = n-1;
int col = n/2;
magic[row][col] = 1;
for (int i = 2; i <= n*n; i++) {
if (magic[(row + 1) % n][(col + 1) % n] == 0) {
row = (row + 1) % n;
col = (col + 1) % n;
}
else {
row = (row - 1 + n) % n;
}
magic[row][col] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (magic[i][j] < 10) System.out.print(" ");
if (magic[i][j] < 100) System.out.print(" ");
System.out.print(magic[i][j] + " ");
}
System.out.println();
}
}
What else should I add or take away from this program?
According to your program, need to provide 4 arguments when running. ex: If class name is Test:
java Test 1 1 1 3
Result:
4 9 2
3 5 7
8 1 6
Number Pattern
I am asked to enter a number rc, and based on rc construct this pattern. I am able to initialize the table but without the highlighted numbers:
int [][] num2 = new int [rc][rc];
counter = 1;
for(int i = 0; i < rc; i++){
if(i!=0)
counter--;
for(int j =0; j < rc; j++){
num2 [i] [j] = counter;
counter ++;
}
}
Any hints or ideas?
You got it partially right. The numbers printed on each row are the same but the start point is incremented by 1 each time. Thus, you can use variable i again to shift it.
int [][] num2 = new int [rc][rc];
int counter = 1;
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
num2[i][(j + i) % rc] = counter++;
}
}
The following code is working fine for your problem.
int rc=5;
int [][] num2 = new int [rc][rc];
int counter = 1;
for(int i = 0; i < rc; i++){
for(int j =i; j < rc; j++){
num2 [i] [j] = counter;
counter ++;
}
for(int k =0; k < rc; k++){
if(num2[i][k]==0){
num2 [i] [k] = counter;
counter++;
}
System.out.print(num2[i][k]+"\t");
}
System.out.println();
}
The logic behind my solution is:
First fill an array from 1 - N, where N is the user input (or
rc in this case):
Then, we check if it's not the first line, if it is, we simply print
the numbers in order.
Now, we have to know which numbers go first:
In the line 1 (remember it starts from 0), it must print the number at [1][4] in [1][0], so our loop substracts rc - i + j, this gives: 5 - 1 + 0, which in fact is index [4].
We know that after we've printed the last numbers first, we must continue the sequence, so we print index: [1][0] at [1][1] (Why 1, 2? Because otherwise we would get something like the example below, that's why we need to substract 1 to it
1 2 3 4 5
10 7 8 9 10
And that's it:
public class StrangePattern {
public static void main(String[] args) {
int rc = 5;
int number = 1;
int spaces = 0;
int[][] numbers = new int[rc][rc];
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
numbers[i][j] = number;
number++;
}
}
for (int i = 0; i < rc; i++) {
for (int j = 0; j < rc; j++) {
if (i != 0) {
if (j < i) {
System.out.print(numbers[i][rc - i + j] + "\t");
} else {
System.out.print(numbers[i][j - spaces] + "\t");
}
} else {
System.out.print(numbers[i][j] + "\t");
}
}
spaces++;
System.out.println();
}
}
}
Which provides this output:
1 2 3 4 5
10 6 7 8 9
14 15 11 12 13
18 19 20 16 17
22 23 24 25 21
And this one for rc = 3:
1 2 3
6 4 5
8 9 7
So, I'm trying to create a program that captures 9 numbers in a 2D array, being of size 3 x 3. After capturing all the numbers, they are re-arranged, reversed, and displayed.
So if a user entered:
1 2 3
4 5 6
7 8 9
It should be reversed as this:
9 8 7
6 5 4
3 2 1
So far I have this:
public class Reversenumber {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int[][] array1 = new int[3][3];
int val1 = 0;
int val2 = 0;
int[][] array2 = new int[3][3];
int val3 = 0;
int val4 = 0;
for (val1 = 0;val1<3; val1++){
for (val2 = 0;val2<3; val2++){
System.out.println("Please type number for column "+(val1+1)+" and row "+(val2+1));
array1[val1][val2] = kb.nextInt();
}
}
System.out.println("\nThe inverse of this array is:");
for(val1 = 0;val1<3; val1+=1){
for (val2 = 0;val2<3; val2+=1){
System.out.print(array1[val1][val2]+" ");
}
System.out.println("");
}
}
}
The problem I'm having is outputing the inverse of the array. The first for loop captures the information perfectly, but the second set of for loop displays that same information in a 3x3 array, but not inverted. Help anyone?
I hope you don't mind, I changed the variables around completely since they are confusing. also changed the way you input stuff.
Scanner kb = new Scanner(System.in);
int[][] array1 = new int[3][3];
int[][] array2 = new int[3][3];
for (int y = 0; y < array1.length; y++) {
System.out.println("input 3 numbers for row " + (y + 1));
for (int x = 0; x < array1[y].length; x++) {
array1[y][x] = kb.nextInt();
}
}
System.out.println("\nThe inverse of this array is:");
for (int y = array1.length; --y >= 0;) {
for (int x = array1[y].length; --x >= 0;) {
System.out.printf("%3d", array1[y][x]);
}
System.out.println();
}
output
input 3 numbers for row 1
1 2 3
input 3 numbers for row 2
4 5 6
input 3 numbers for row 3
7 8 9
The inverse of this array is:
9 8 7
6 5 4
3 2 1
So instead of inputting each separately you put 3 on the same line and put a space between them.
Assuming you mean invert like so:
1 2 3
4 5 6
7 8 9
to
1 4 7
2 5 8
3 6 9
It's as simple as keeping the loop the same, but swapping the coordinates between the arrays:
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int[][] array1 = new int[3][3];
int val1 = 0;
int val2 = 0;
int[][] array2 = new int[3][3];
int val3 = 0;
int val4 = 0;
for (val1 = 0; val1 < 3; val1++) {
for (val2 = 0; val2 < 3; val2++) {
System.out.println("Please type number for column " + (val1 + 1) + " and row " + (val2 + 1));
array1[val1][val2] = kb.nextInt();
}
}
System.out.println("\nOriginal array is:");
for (val1 = 0; val1 < 3; val1 += 1) {
for (val2 = 0; val2 < 3; val2 += 1) {
System.out.print(array1[val1][val2] + " ");
}
System.out.println("");
}
System.out.println("\nInverted array is:");
for (int x = 0; x < 3; x += 1) {
for (int y = 0; y < 3; y += 1) {
System.out.print(array1[y][x] + " ");
}
System.out.println("");
}
System.out.println("\nThe Reversed array is:");
for (int x = 2; x >= 0; x -= 1) {
for (int y = 2; y >= 0; y -= 1) {
System.out.print(array1[x][y] + " ");
}
System.out.println("");
}
}
Edit: I added the reversed scenario you asked about in the comment below. You actually expected output like this:
9 8 7
6 5 4
3 2 1
How do I print out a table of numbers when the last number is given by the user, and the variables are added together to get new variables? For example, if the user gives the value 3 for numberOfRows, an example output would be;
1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Here's what I have so far:
int numberOfRows;
int numberOfVariables = numberOfRows*numberOfRows;
int i = 0;
boolean cycle = false;
if(i < numberOfVariables){
cycle = true;
i++;
} else if(i >= numberOfVariables){
cycle = false;
i++;
}
System.out.println(cycle);
for(cycle = true;;){
System.out.println(i + "\t");
i++;
}
I'm not sure how to continue.
public static void main(String args[]) throws NumberFormatException,
IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of rows : ");
int n = Integer.parseInt(reader.readLine());
for (int i = 0; i < n; i++) {
for (int j = i; j < n + i; j++) {
System.out.print(j + "\t");
}
System.out.println();
}
}
Output :
Enter number of rows : 6
0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Wnter row count");
int numberOfRows = scanner.nextInt();
for (int i = 0 ; i <= numberOfRows ; i++) {
for (int j = 0 ; j <= numberOfRows ; j++) {
System.out.print(i + j + " ");
}
System.out.println();
}
}
}
Output
Wnter row count
3
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
This question already has answers here:
Pascal's triangle 2d array - formatting printed output
(5 answers)
Closed 1 year ago.
The assignment is to create Pascal's Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the maximum number of rows the user wants printed.
public static void triangle(int maxRows) {
int r, num;
for (int i = 0; i <= maxRows; i++) {
num = 1;
r = i + 1;
for (int col = 0; col <= i; col++) {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
System.out.println();
}
}
I need to format the values of the triangle such that it looks like a triangle:
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
I can't for the life of me figure out how to do that. Please answer keeping in mind that I'm a beginner in Java programming.
public static long pascalTriangle(int r, int k) {
if (r == 1 || k <= 1 || k >= r) return 1L;
return pascalTriangle(r - 1, k - 1) + pascalTriangle(r - 1, k);
}
This method allows you to find the k-th value of r-th row.
This is a good start, where it's homework, I'll leave the rest to you:
int maxRows = 6;
int r, num;
for (int i = 0; i <= maxRows; i++) {
num = 1;
r = i + 1;
//pre-spacing
for (int j = maxRows - i; j > 0; j--) {
System.out.print(" ");
}
for (int col = 0; col <= i; col++) {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
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
In each row you will need to print:
n spaces
m numbers
n spaces
Your job is to figure out n (which will be zero in the last line) and m based on row number.
[This is more like a comment but I needed more formatting options than comments provide]
You need to print the spaces (like others have mentioned) and also as this is homework I'm leaving it to you but you might want to look at this handy little function
System.out.printf();
Here is a handy reference guide
Also note that you will need to take into account that some numbers are more than 1 digit long!
import java.util.*;
class Mine {
public static void main(String ar[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i < n; i++) {
int size = 1;
for (int j = 1; j <= i; j++) {
int a[] = new int[size];
int d[] = new int[size];
for (int k = 1; k <= size; k++) {
a[1] = 1;
a[size] = 1;
for (int p = 1; p <= size; p++) {
d[p] = a[p];
}
if (size >= 3) {
for (int m = 2; m < size; m++) {
a[m] = d[m] + d[m - 1];
}
}
}
for (int y = 0; y < size; y++) {
System.out.print(a[y]);
}
System.out.println(" ");
}
++size;
}
}
}
public class HelloWorld {
public static void main(String[] args) {
int s = 7;
int k = 1;
int r;
for (int i = 1; i <= s; i++) {
int num = 1;
r = i;
int col = 0;
for (int j = 1; j <= 2 * s - 1; j++) {
if (j <= s - i)
System.out.print(" ");
else if (j >= s + i)
System.out.print(" ");
else {
if (k % 2 == 0) {
System.out.print(" ");
} else {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
col++;
}
k++;
}
}
System.out.println("");
k = 1;
}
}
}
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
You can try this code in java. It's simple :)
public class PascalTriangle {
public static void main(String[] args) {
int rows = 10;
for (int i = 0; i < rows; i++) {
int number = 1;
System.out.format("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%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
Code perfectly prints pascal triangle:
public static void main(String[] args) {
int a, num;
for (int i = 0; i <= 4; i++) {
num = 1;
a = i + 1;
for (int j = 4; j > 0; j--) {
if (j > i)
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
if (j > 0)
num = num * (a - j) / j;
System.out.print(num + " ");
}
System.out.println();
}
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1