Inverting a 3 x 3 Array and displaying it Java - java

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

Related

how to print out the nth row of pascal's triangle using 1d arrays in java

it should be a program that will print the row of the pascal's triangle depending on the user input, so if the user enters 4, the output will be: 1 3 3 1. i've figured out how to print out the entire pascal's triangle, but i'm not sure how to adjust it so that it only prints out the nth row.
public class PascalTriangle
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number.");
int num = input.nextInt();
input.nextLine();
int x=1;
int y=0;
int[] pascal = new int[num];
int[] pascalTemp = new int[num];
pascal[0] = 1;
pascal[1] = 1;
for(int i=0; i<num; i++)
{
for(int j=0; j<=i; j++)
{
if(i==0)
System.out.print("1");
else
{
if(j==0 || j==i)
System.out.print("1 ");
else
{
pascalTemp[x] = pascal[y] + pascal[y+1];
System.out.print(pascalTemp[x]+ " ");
x++;
y++;
}
}
}
System.out.println();
pascalTemp[x] = 1;
if(i>1)
{
y=0;
pascal[y]=1;
for(x=1, y=1; y<=i; x++, y++)
pascal[y] = pascalTemp[x];
x=1;
y=0;
}
}
}
}
The r-th number of the n-th line (r = 0, 1, 2, 3, ...) is (n - 1)Cr = (n - 1)! / (r! * (n - 1 - r)!) . (n - 1)C0 is always 1. And (n - 1)C(r + 1) can be obtained from (n - 1)Cr. For example, when n = 4, it becomes as follows.
3C0 = 1 = 1
3C1 = 3C0 * 3 / 1 = 3
3C2 = 3C1 * 2 / 2 = 3
3C3 = 3C2 * 1 / 3 = 1
Multiplying numbers decrease by 1 and dividing numbers increase by 1.
If you program this, it will be as follows.
static void pascalNthRow(int n) {
for (int p = 1, m = n - 1, d = 1; d <= n; p = p * m / d, --m, ++d)
System.out.print(p + " ");
System.out.println();
}
and
pascalNthRow(3);
pascalNthRow(4);
pascalNthRow(5);
output
1 2 1
1 3 3 1
1 4 6 4 1

Java - printing unique number sequences

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.

Trouble understanding how for loops work in java

I'm new at java and I have to write a code for a multiplication table. I have the multiplication working for a square table but I need it to work for a reverse triangle. What do I need to add to the code to make it print like this following examples?
/*What I have:
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
What I want:
1 2 3 4
4 6 8
9 12
16
*/
import java.util.Scanner;
public class question3{
public static void main(String[] args){
System.out.print("Enter an integer between 1 and 10: ");
Scanner input = new Scanner(System.in);
int value = input.nextInt();
if(value < 0 || value > 10){
while(value < 0 || value > 10){
System.out.print("Enter an integer between 1 and 10: ");
value = input.nextInt();
if(value <= 10){
for(int x=1; x <= value; x++){
System.out.println();
for(int y=1; y<= value; y++){
int z=x*y;
System.out.printf(z + "\t");
}
}
}
}
}
else if (value <=10 && value >=0){
for(int x=1; x <= value; x++){
System.out.println();
for(int y=1; y<= value; y++){
int z=x*y;
System.out.printf(z + "\t");
}
}
}
System.out.println();
}
}
from the given information i assume that you want to make a table which display values
/* 1 2 3 4
4 6 8
9 12
16 */
here is the simple code which i have written , it also display the same table which you want.
int row,column ;
column = row = 4;
String space = " ";
for(int i=0;i<row;++i)
{
for(int x=0;x<2*i;++x )
{
System.out.print(space);
}
for(int y=1; y<=column; ++y)
{
System.out.print(space);
System.out.print((y+i)*(i+1));
}
column = column -1;
System.out.println();
}

Printing odd numbers in odd sequences

There's this problem from my programming class that I can't get right... The output must be all odd numbers, in odd amounts per line, until the amount of numbers per line meets the odd number that was entered as the input. Example:
input: 5
correct output:
1
3 5 7
9 11 13 15 17
If the number entered is even or negative, then the user should enter a different number. This is what I have so far:
public static void firstNum() {
Scanner kb = new Scanner(System.in);
int num = kb.nextInt();
if (num % 2 == 0 || num < 0)
firstNum();
if (num % 2 == 1)
for (int i = 0; i < num; i++) {
int odd = 1;
String a = "";
for (int j = 1; j <= num; j++) {
a = odd + " ";
odd += 2;
System.out.print(a);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
firstNum();
}
The output I'm getting for input(3) is
1 3 5
1 3 5
1 3 5
When it really should be
1
3 5 7
Can anyone help me?
Try this:
public static void firstNum() {
Scanner kb = new Scanner(System.in);
int num = kb.nextInt();
while (num % 2 == 0 || num < 0) {
num = kb.nextInt();
}
int odd = 1;
for (int i = 1; i <= num; i += 2) {
String a = "";
for (int j = 1; j <= i; j++) {
a = odd + " ";
odd += 2;
System.out.print(a);
}
System.out.println();
}
}
if (num % 2 == 1) {
int odd = 1;
}
for (int i = 0; i < num; i++) {
String a = "";
for (int j = 1; j <= odd; j++) {
a = odd + " ";
odd += 2;
System.out.print(a);
}
System.out.println();
}
You should assign odd before for loop.
In inner for loop compare j and odd together.
For questions like this, usually there is no need to use and conditional statements. Your school probably do not want you to use String as well. You can control everything within a pair of loops.
This is my solution:
int size = 7; // size is taken from user's input
int val = 1;
int row = (size / 2) + 1;
for (int x = 0; x <= row; x++) {
for (int y = 0; y < (x * 2) + 1; y++) {
System.out.print(val + " ");
val += 2;
}
System.out.println("");
}
I left out the part where you need to check whether input is odd.
How I derive my codes:
Observe a pattern in the desired output. It consists of rows and columns. You can easily form the printout by just using 2 loops.
Use the outer loop to control the number of rows. Inner loop to control number of columns to be printed in each row.
The input number is actually the size of the base of your triangle. We can use that to get number of rows.
That gives us: int row = (size/2)+1;
The tricky part is the number of columns to be printed per row.
1st row -> print 1 column
2nd row -> print 3 columns
3rd row -> print 5 columns
4th row -> print 7 columns and so on
We observe that the relation between row and column is actually:
column = (row * 2) + 1
Hence, we have: y<(x*2)+1 as a control for the inner loop.
Only odd number is to be printed, so we start at val 1 and increase val be 2 each time to ensure only odd numbers are printed.
(val += 2;)
Test Run:
1
3 5 7
9 11 13 15 17
19 21 23 25 27 29 31
33 35 37 39 41 43 45 47 49
You can use two nested loops (or streams) as follows: an outer loop through rows with an odd number of elements and an inner loop through the elements of these rows. The internal action is to sequentially print and increase one value.
a loop in a loop
int n = 9;
int val = 1;
// iterate over the rows with an odd
// number of elements: 1, 3, 5...
for (int i = 1; i <= n; i += 2) {
// iterate over the elements of the row
for (int j = 0; j < i; j++) {
// print the current value
System.out.print(val + " ");
// and increase it
val += 2;
}
// new line
System.out.println();
}
a stream in a stream
int n = 9;
AtomicInteger val = new AtomicInteger(1);
// iterate over the rows with an odd
// number of elements: 1, 3, 5...
IntStream.iterate(1, i -> i <= n, i -> i + 2)
// iterate over the elements of the row
.peek(i -> IntStream.range(0, i)
// print the current value and increase it
.forEach(j -> System.out.print(val.getAndAdd(2) + " ")))
// new line
.forEach(i -> System.out.println());
Output:
1
3 5 7
9 11 13 15 17
19 21 23 25 27 29 31
33 35 37 39 41 43 45 47 49
See also: How do I create a matrix with user-defined dimensions and populate it with increasing values?
Seems I am bit late to post, here is my solution:
public static void firstNum() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the odd number: ");
int num = scanner.nextInt();
if (num % 2 == 0 || num < 0) {
firstNum();
}
if (num % 2 == 1) {
int disNum = 1;
for (int i = 1; i <= num; i += 2) {
for (int k = 1; k <= i; k++, disNum += 2) {
System.out.print(disNum + " ");
}
System.out.println();
}
}
}

Pascal's Triangle Format [duplicate]

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

Categories

Resources