• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping Help GreenHorns  RSS feed

 
Visualize Idiot
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Visualize Idiot
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}

}

}
 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Visualize Idiot
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Help needed !!
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!