Forums Register Login

Looping Help GreenHorns

+Pie Number of slices to send: Send
Hi all, I thought I'm good with all these looping stuff, but i'm not. Consider a matrix like this.
[1 2 3]
[4 5 6]
[7 8 9]
print in this zig-zag order, so output will be 3 2 6 1 5 9 4 8 7. Provide solution for this.

Also, if there is any complete reference or sites to learn how to completely play around this loops, Share it. Please Share it !!
+Pie Number of slices to send: Send
See Control Flow Statements in Oracle's Java Tutorials.

Visualize Idiot wrote:Provide solution for this.


Why don't you try to write some code, and post it here? Then we can help you to get it to work if you get stuck. You learn most by programming yourself, instead of just looking at what someone else has made.
+Pie Number of slices to send: Send
Before you try writing the code, try writing out in ENGLISH how you'd do it. Pretend you are giving directions to a 10 year old child. once you have it written out, go back and simplify it several times, making the steps smaller and smaller.

Try and be as precise as possible. List out any requirements. For example, as stated, your program could be solved with nine explicit println statements.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Hi all, thanks for making me try a program. This is what i tried and I'm going nowhere with this program :'(


package myPractice;

public class ZigZagMatrix {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] arr={{1,2,3},{4,5,6},{7,8,9}};

int row=0,col=2;
while(row<2)
{
System.out.println(arr[row][col]);
// if(row<col&&col<=2)
// row=0;col=0;
if(row<col)
col--;

if(row==col)
row++;col++;
if(row==col&&row==2&&col==2)
row=row-1;col=0;
if(row>col)
row++;col++;
if(row>col && row==2)
System.exit(1);
}

}

}
+Pie Number of slices to send: Send

Step 1: Use the code tags, like this





Step 2: break the problem up more, focus on getting it to print the last element of array 1, then build it up from there. Stick in a println after each bit and you will get to see where the logic is not working as you thought.

Step 3: If after that you are still stuck (don't worry, it happens to everyone) come back here and ask specific questions. Just saying it does not work will not really get you any help as we won't know what the problem is. Plus when you describe a problem in detail, often an idea will occur to you as to why you have gone wrong.


Good luck

S
+Pie Number of slices to send: Send
 

Visualize Idiot wrote:


Like Jesper told you earlier please check your Purple Mooseages Please note this is not optional. Failure to do so might even lead to termination of your account.
+Pie Number of slices to send: Send


Help needed !!
+Pie Number of slices to send: Send
Hello!

I am not sure that you follow naming convention on this site.
I do not think that making such a nickname can help you to express yourself.

In your task there is one good hint : when row==col you are on diagonal of your matrix.
+Pie Number of slices to send: Send
Break those in pieces.
You'll need to print items in diagonal (down-right) order.
Your "starting points" will be fields in array on top and left edge.
How do you define top and left edge?
Top would be fields [0,0], [0,1], [0,2]...
Right would be [1,0], [2,0]...
Note that second [0,0] would be redundant.
You'll need to figure out how to arrange this in appropriate order (because you want to start at topmost, rightmost point).

Then, for each "starting point" do the following:
Print a point it and add [+1, +1] to its coordinates. If none of new point's coordinates exceeds size of the array, pint the point.
Terminate the loop otherwise.

That's just one of many ways to do that.
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1018 times.
Similar Threads
Something i'm extremely confused on
Random Numbers II
struct in java
Swap sublist of an arraylist with the sublist of another arraylist
Recursion Functions: The proper condition to stop it
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:10:31.