I´m trying to create a 3 dimensional array which should have three horizontal boxes which two rows and 3 columns in each. As it is the output get me two vertical boxes with 3 rows and 3 columns, I have tried to change the integers around but it seems to just get me in deeper trouble.
And on top of this I´ve been trying to add all the integers in the array to get and print out the mean/average value but I can´t really find/understand how to do this.
int[][][] ett= {{ {10,12,14}, {16,18,20}, {22,24,26} },
{ {11,13,15}, {17,19,21}, {23,25,27} } };
//want this to print out as it looks, three "boxes" with two rows and three columns
for (int i= 0; i<ett.length;i++){
for (int j= 0; j<ett[i].length ;j++){
for (int k=0; k<ett[i][j].length;k++){
System.out.print(ett[i][j][k]+" ");
}
System.out.println();
}
System.out.println();
}
int medel=0;
//medel = all integers in array added, 10+12+14...
// divide medel with number of integers in array (18)
System.out.println("Medelvärdet: "+medel);
}//main
You only have to get rid of one println :
for (int i= 0; i<ett.length;i++){
for (int j= 0; j<ett[i].length ;j++){
for (int k=0; k<ett[i][j].length;k++){
System.out.print(ett[i][j][k]+" ");
}
// System.out.println(); remove this println
}
System.out.println();
}
and you'll get this :
10 12 14 16 18 20 22 24 26
11 13 15 17 19 21 23 25 27
You might want to add spaces between the blocks :
for (int i= 0; i<ett.length;i++){
for (int j= 0; j<ett[i].length ;j++){
for (int k=0; k<ett[i][j].length;k++){
System.out.print(ett[i][j][k]+" ");
}
System.out.print(" ");
}
System.out.println();
}
which will give you this:
10 12 14 16 18 20 22 24 26
11 13 15 17 19 21 23 25 27
Related
While I was writing the code, I had to print out total 25 numbers, so I put the number 25 inside the numberArray, however when I run the program, it dd not print out 25 numbers. Instead the number of the random numbers kept changing and did not stick to the specific value. What should I do in this case to make my code print out 25 numbers?
public static void printArray(int[] ear) {
System.out.println("ODD NUMBERS : ");
for (int e = 0; e<ear.length ; e ++) {
ear[e] = (int)(Math.random()* 100);
if(ear[e]%2!=0)
System.out.print(ear[e] + " ");
}
System.out.println("\n" + "EVEN NUMBERS : ");
for (int e = 0; e<ear.length ; e ++) {
ear[e] = (int)(Math.random()* 100);
if(ear[e]%2==0)
System.out.print(ear[e] + " ");
}
}
public static void main(String[] args) {
int[] numberArray = new int[25];
printArray(numberArray);
}
}
As stated in the code, you are only printing the ODD and EVEN numbers of the array. That's why you get different outcomes on each run.
If you change the code to print the array index instead of the value you can see that you are printing
ODD NUMBERS :
4 10 12 13 14 15 18 19 22 23 24
EVEN NUMBERS :
0 1 3 4 5 6 9 13 16 17 18 19 20 22 23
So the instructions are to create a matrix 10x10 and randomize values between 1~20 for each index
Then ask the user to enter 6 numbers between 1~20, and use those numbers to create
another 2x3 Matrix.
Then the program has to check if the 10x10 matrix contains the 2x3 matrix from the user.
Also i'm not allowed to use functions.
Example for input:
Enter the1 number in the matrix: 17
Enter the2 number in the matrix: 17
Enter the3 number in the matrix: 17
Enter the4 number in the matrix: 5
Enter the5 number in the matrix: 13
Enter the6 number in the matrix: 14
Output:
The random matrix:
14 14 3 18 2 10 19 10 3 3
2 17 15 16 5 17 7 17 15 10
13 1 3 9 5 4 11 9 1 8
17 14 13 9 8 1 18 3 17 18
12 17 5 14 13 4 16 14 13 4
8 12 8 19 6 5 3 3 14 18
16 16 17 9 9 10 17 3 8 5
13 8 6 17 6 17 17 7 19 5
5 14 6 15 11 11 13 17 17 17
17 13 13 18 11 4 15 5 13 14
The matrix you entered:
17 17 17
5 13 14
The random matrix contains the users matrix.
My Code so far =
int[][] big = new int[10][10];
int[][] small =new int [][] {{the1,the2,the3},{the4,the5,the6}};
for(int i = 0; i < big.length; i++ )
{
for(int j = 0; j < big[i].length; j++)
{
big[i][j]= (int)((Math.random()*20)+1);
}
}
for(int i =0; i < big.length; i++)
{
for(int j = 0; j < big.length; j++)
{
Boolean isEqual=true;
for(int k = 0; k < 2 && isEqual; k++)
{
for(int m = 0; m < 3; m++)
{
if (big[i+k][j+m]!=small[k][m])
{
isEqual=false;
break;
}
}
}
}
}
My general idea was basically running through the indexes of the big matrix while checking for equal numbers, and if one found, the routine continues, otherwise it breaks, and going to the next index in the big matrix.
Your general idea is correct and should work for the described scenario. The only issue you are having is your break statement doesent work as you think.
According to nested loop, if you put break statement in inner loop, compiler will jump out from inner loop and continue the outer loop again. If you need to jump out from the outer loop using break statement given inside inner loop you can use a labelled loop, i.e give your loop a name and use it with the break statement.
In your code above you shold not only breake the inner most loop but also the one above:
public static void main(String[] args) {
int[][] big = new int[10][10];
int[][] small = new int[][]{{12, 13, 14}, {13, 14, 15}};
for (int i = 0; i < big.length; i++) {
for (int j = 0; j < big[i].length; j++) {
big[i][j]= (int)((Math.random()*20)+1);
}
}
// just added this loop to print the random array so you can check the output
for(int[] row : big){
System.out.println(Arrays.toString(row));
}
for(int i = 0; i<big.length-1; i++){
for(int j = 0; j<big[0].length-2; j++){
boolean isEqual = true;
LoopIwannaBreak:for(int k = 0; k<small.length; k++){
for(int m = 0; m<small[0].length; m++){
if(big[i+k][j+m] != small[k][m]){
isEqual = false;
break LoopIwannaBreak;
}
}
}
// added this to tell at which index the match was found
if(isEqual){
System.out.println("found at big["+i+"]["+j+"]");
}
}
}
}
I am trying to shift elements in a 2D array (specifically a square matrix) so that the result is a "staircase" pattern:
Original:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Objective:
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
I've managed to write a program that can perform the task in a 1D array for the first line of the matrix (see below), but I can't seem to translate the code so that it functions in a 2D array.
How do I get my code to function in a 2D array in Java?
And, for later, how would I go about incrementing the change so that, as the row increases (from upper to lower levels of the matrix), the number of shifts also increases?
public class StaircaseMatrix
{
public static void main(String[] args)
{
int[] num = {1,2,3,4,5};
shiftRight(num);
System.out.println("After shifting the array is:");
for (int x = 0; x < num.length; x++)
System.out.print(num[x] + " ");
}
public static void shiftRight(int[] list)
{
int last = list[list.length - 1];
for (int j = list.length - 1; j > 0; j--) {
list[j] = list[j - 1];
}
list[0] = last;
}
}
You call shiftRight() 0 times for array2D[0], 1 time for array2D[1], 2 times for array2D[2], and so on, using a for loop.
My problem is when I sort a list it will get the last element of the array wrong, ending up with it at the beginning of the array. In my example it fails to sort the last element which is 9, ending up printed first ahead of small numbers such as 0 and 1. Here is my code:
public class ty {
public static void main(String[]argus){
int []numbers={45,23,34,545,56,23,4,1,66,0,9};
int j;
for( int i=0;i<numbers.length;i+=1){
int first=0;
for(j=0;j<=i;j+=1){
if(numbers[j]>=first){
first=numbers[j];
numbers[j]=numbers[i];
numbers[i]=first;}
}//inner loop
}//first loop
for(int i=0;i<numbers.length;i+=1){
System.out.print(numbers[i]+" ");}
}
}
//the output is 9 0 1 4 23 23 34 45 56 66 545
As you see, they are in order except for the 9 at the start which is out of place.
You have the wrong output as a result of a logical error. You have mistakes in the Selection sort technique. Here's how to do it:
int []numbers={45,23,34,545,56,23,4,1,66,0,9};
for(int i=0; i<numbers.length-1; i+=1){
int m = i;
for(int j=i+1; j<numbers.length; j+=1){
if(numbers[m]>numbers[j])
m = j;
}
int temp = numbers[m];
numbers[m] = numbers[i];
numbers[i] = temp;
}
for(int i=0; i<numbers.length; i+=1)
System.out.print(numbers[i]+"\t");
This will work correctly and give the output:
0 1 4 9 23 23 34 45 56 66 545
I need to print out the following data using a multi-dimensional array:
5 4 3 2 1
10 9 8 7 6
15 14 13 12 11
20 19 18 17 16
25 24 23 22 21
The programming language that I am using is Java. This is what I have so far:
public class Problem3 {
public static void main(String[] args) {
int[][] prob3 = new int[5][5];
for(int row = 0; row < prob3.length; row++){
System.out.println();
for(int col = 0; col < prob3[row].length; col++){
prob3[row][col] = row * 5 + col + 1;
System.out.print(prob3[row][col] + " ");
}
}
}
}
When I print this to the screen I get this:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
I am not sure how to manipulate the numbers so they display how I want them. I really want to understand how this works. Let me know if I am doing this completely wrong. Thanks for the help in advance.
If you want iterate through the columns backward, you have to set you start value of the column-loop to the last index, check whether it's still bigger or equal to 0 and decrease col every iteration.
Like that:
int[][] prob3 = new int[5][5];
for (int row = 0; row < prob3.length; row++) {
System.out.println();
for (int col = prob3[row].length - 1; col >= 0; col--) {
prob3[row][col] = row * 5 + col + 1;
System.out.print(prob3[row][col] + " ");
}
}