Incrementing 2D Arrays vertically and horizontally? - java

I am trying to make a method where a 2D array is created in the main, which can be any dimension. After that, we are supposed to make 2 methods, one which increments each next value in the array by a given number labeled step. One method is supposed to increment the rows, and the other increments it by columns.
This is what I have:
public static void main (String [] args){
int [][] fillRightArray = new int [5][8];
fillRight(fillRightArray, 2);
int [][] fillDownArray = new int[5][8];
fillDown(fillDownArray, -2);
For the fill right method, this is the what the output should be:
2 4 6 8 10 12 14 16
18 20 22 24... //all the way to 80, since the array has 40 elements (40*2=80)
This is my method:
public static void fillRight (int [][] fillRightArray, int step){
for (int i = 0; i< fillRightArray.length; i++){
for (int j = 0; j< fillRightArray[i].length; j++){
fillRightArray[i][j] += step*(j+1);
System.out.print(fillRightArray[i][j] + " ");
}
System.out.print("\n");
}
But for some reason, my output is:
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14 16
Any idea as to why this is happening? Same thing happens when I go with the fillDown method, the output is supposed to be:
2 12 22....
4 14 24....
6 16 26....
8 18 28....
10 20 30.... all the way to 80
But instead I get:
2 2 2 2 2
4 4 4 4 4
6 6 6 6 6
8 8 8 8 8
10 10 10 10 10

Your code doesn't work because you aren't taking into account the previous cell.
Your logic for determining the value of a specific cell is fillRightArray[i][j] += step*(j+1); .
This line only considers the value of j to determine the value of a cell within your array, when it should also consider the value of i (explicitly or implicitly).
You should add a counter that keeps track of how many cells you have set, and set the next cells value based on the number of cells that have already been set.
Your fillRight method should instead look like this:
public static void fillRight (int [][] fillRightArray, int step){
int count = 0;
for (int i = 0; i< fillRightArray.length; i++){
for (int j = 0; j< fillRightArray[i].length; j++){
count++;
fillRightArray[i][j] += step*count;
System.out.print(fillRightArray[i][j] + " ");
}
System.out.print("\n");
}
}

You're constantly repeating the same work and going back to 0 each time.
In your fillRight, each row is doing 2,4,6,8,10 because you're doing step*(j+1), where j resets to 0 after each iteration of the loop. You need to find a way to include i as well as j when setting the value, or just have a counting variable that is incremented each step
Like this:
step += 2
fillRightArray[i][j] = step
That way you don't need to worry about position.

Related

Home work about Matrix containing another matrix ( Check if true or false)

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+"]");
}
}
}
}

dividing matrix into four sub-blocks

i want devide matrix into four sub-blocks equally by vertically and horizontallty in java (Here, we suppose that m and nare even numbers) .
for example we have matrix:
1 2 3 4 5 6
7 8 9 1 2 8
1 2 3 4 5 6
4 5 6 7 8 9
1 4 7 2 5 8
3 6 9 7 2 5
I want to display the last block that is:
7 8 9
2 5 8
7 2 5
how i can resolve this problem in java.
Iterate over the lower-right part of the matrix. Here is an example for a square matrix. I am sure you will be able to make it more generic for non-square quadrants or to get other quadrants than the lower-right one.
public int[][] getQuadrantOfSquareMatrix(int[][] matrix) {
int newDimension = matrix.length / 2;
int[][] toReturn = new int[newDimension][newDimension];
for (int i = 0; i < newDimension; i++) {
for (int j = 0; j < newDimension; j++) {
toReturn[i][j] = matrix[i + newDimension][j + newDimension];
}
}
return toReturn;
}

Java: incrementally shifting elements in a 2D array to the right

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.

Nested Loops - A Third One?

Write a program that prints the following on the Screen:
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10
5 6 7 8 9 10
6 7 8 9 10
7 8 9 10
8 9 10
I'm having a little trouble correcting the nested loops - I have it to look like that, the numbers won't stop at 10 though. The code prints this:
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
I'm a beginner programmer and need help with this - do you need a third loop inside of the nested loop already? Any help is appreciated! Here is my code so far:
import java.util.Scanner;
public class LoopProgram
{
public static void main(String args [])
{
for (int e=0; e<8; e++)
{
for (int f=1; f<=10; f++)
{
System.out.print(f + e + " ");
}
System.out.println();
}
}
}
Two loops are enough.
The outer loop runs eight times, you've got that working already.
Your inner loop, however, always runs ten times, which is not what you want. You want the inner loop to run 10 times first, then only 9 times, then only 8 times etc. The trick here is to change the start or end value of that inner loop, like this:
for (int e=0;e<8;e++) {
for (int f=e+1; f<=10; f++) {
...
}
}
I think this is what you are looking for.
for(int e=1;e<9;e++) {
for(int f=e;f<=10;f++) {
System.out.print(f+" ");
}
System.out.println();
}
This ouputs exactly what you asked.
Try this for the second loop:
for(int f=e;f<=10;f++)
and for output
System.out.print(f+" ");
Here's how I would do it
for (int i = 1; i <= 8; i++) {
for (int j = i; j <= 10; j++)
System.out.print(j + " ");
System.out.println();
}
Three loops is severe overkill. This can be done with a single loop and two counters.
public class LoopProgram {
static final int LIMIT_1 = 8;
static final int LIMIT_2 = 10;
public static void main(String[] args) {
int a = 1, b = 2;
while(b <= (LIMIT_1 + 1)) {
System.out.print(a);
if(a < LIMIT_2) {
System.out.print(" ");
a++;
} else {
System.out.println();
a = b;
b++;
}
}
}
}
You only need two loops:
The outer one will run eight times and the inner one will begin at the value of the outer counter (thats what makes each line count up to ten but always start at the first value of last line + 1) and run while it's counter is less than 10.
Something like that:
for (int i = 0; i < 8; i++) {
for (int e = i; e < 10; e++) {
//print e + 1
}
}
You have to print e + 1 so that your lines do not begin at 0, but at one, going up to 10.
This is more of an explanation of how to work this sort of thing out for yourself. In the commonest cases of a for-loop, you should ask yourself three questions about the index variable:
What is the first value I want it to have?
Under what conditions do I want to do another iteration?
How should it change from iteration to iteration?
For your inner loop, the answers are:
e
f <= 10
f++
From that, it is easy to construct the loop, and you already have several examples of it written for you.

How can I go through a 2D Array via the secondary diagonal?

As I said, I would like to "scroll" through a Multi Dimensional array via the secondary diagonal, my desired input to be: (Case a) [It can be in either C++ or Java, it doesn't matter]
NOTE+EDIT: The order is not random. It starts from the 1 at the bottom and goes its way up.
Is this possible?
If not then at least half of the code? (Case b)
// Case a:
16 15 13 10
14 12 9 6
11 8 5 3
7 4 2 1
// Case b:
0 0 0 1
0 0 9 6
0 8 5 3
7 4 2 1
You can do this with a simple loop (this is Java):
int size = 4;
int[][] matrix = new int[size][size];
// . . .
for (int i = 0; i < size; ++i) {
doSomethingWith(matrix[i][size - i - 1]);
}
int[][] a={{7,6,4,1},{5,3,9,6},{2,8,5,3},{7,4,2,1}};
for(int i=0; i<a.length; i++)
{
for(int j=a[i].length-1; j>=a[i].length-(i+1); j--)
{
System.out.print(a[i][j]+",");
}
System.out.println();
}

Categories

Resources