how to set up my for loop to print my array in reverse

Reply

Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

how to set up my for loop to print my array in reverse

 
0
  #1
Mar 10th, 2005
im having problems figuring out how to print this array in reverse.I believe it might be a logical issue. I don't want the answer. I'm thinkin my row must be initialized to the matrix.length. can someone give me a few hints as to go about this.



public static void printMatrixReverse(int [][]matrix)
{
int row, col;
for (row = 0 row < matrix.length;row++)
{
for (col=0;col < matrix[row].length;col++)
System.out.print(matrix[row][col] + "\t");
System.out.println();
}
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to set up my for loop to print my array in reverse

 
0
  #2
Mar 10th, 2005
Start at array.length - 1 instead of 0, and make sure that you don't actually access the -1th index.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: how to set up my for loop to print my array in reverse

 
0
  #3
Mar 10th, 2005
Hi everyone,

Originally Posted by SyLk
I don't want the answer
Do you want the question instead :mrgreen:

Just kidding

Is this what you want

  1.  
  2. public static void printMatrixReverse(int [][]matrix)
  3. {
  4. int row, col;
  5.  
  6. for (row = matrix.length; row >= 0 ; row--)
  7. {
  8.  
  9. for (col = matrix[row].length; col >= 0; col--)
  10. {
  11.  
  12. System.out.print(matrix[row][col] + "\t");
  13. System.out.println();
  14.  
  15. }
  16. }

Let me know if it works

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

Re: how to set up my for loop to print my array in reverse

 
0
  #4
Mar 10th, 2005
freesoft thats far from correct.

Narue can u explain to me why i would initialize row to matrix.length-1?

1 2 3
4 5 6
would'nt that cause my numbers to start at 5 instead of 6?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: how to set up my for loop to print my array in reverse

 
0
  #5
Mar 10th, 2005
Here's a way of doing it, works best if you sort the array first, but you don't have to.

  1. import java.util.*;
  2.  
  3. class DescendingOrder
  4. {
  5. public static void main(String[] arguments)
  6. {
  7. String[] names = {"a", "c", "f", "d", "e"};
  8. Arrays.sort(names);
  9.  
  10. System.out.println("Descending order:");
  11.  
  12. for (int a = 0; a < names.length; a++)
  13. {
  14. System.out.println(a + names[names.length
  15. - 1 - a]);
  16. }
  17. }
  18. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 26
Reputation: SyLk is an unknown quantity at this point 
Solved Threads: 0
SyLk SyLk is offline Offline
Light Poster

Re: how to set up my for loop to print my array in reverse

 
0
  #6
Mar 10th, 2005
Narue I got it done through a little bit of tial and error
but the concept of setting row = to matrix.length-1 is still puzzling
if u could clear this up for me i'd be very thankful.....TY


for (row=matrix.length-1;row>=0;row--)
{
for (col=matrix[row].length-1;col>=0;col--)
System.out.print(matrix[row][col] + "\t");
System.out.println();
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: how to set up my for loop to print my array in reverse

 
0
  #7
Mar 10th, 2005
Originally Posted by SyLk
Narue I got it done through a little bit of tial and error
but the concept of setting row = to matrix.length-1 is still puzzling
if u could clear this up for me i'd be very thankful.....TY


for (row=matrix.length-1;row>=0;row--)
{
for (col=matrix[row].length-1;col>=0;col--)
System.out.print(matrix[row][col] + "\t");
System.out.println();
}
I just gave you a clear example?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: how to set up my for loop to print my array in reverse

 
0
  #8
Mar 10th, 2005
>Let me know if it works
If you tested it then you would find out that it accesses your arrays out of bounds.

>Narue can u explain to me why i would initialize row to matrix.length-1?
Arrays in Java are zero based, which means that the indices go from 0 to length-1. If you initialize the index variable to matrix.length then you'll be accessing the array out of bounds and it will throw an exception. Consider this array:
  1. int[] a = {7,3,5,8,1};
The first item is 7 at a[0]. The fifth item is 1 at a[4]. a.length evaluates to 5, so what happens when you do this?
  1. public class ja {
  2. public static void main(String[] args)
  3. {
  4. int[] a = {7,3,5,8,1};
  5. System.out.println(a[5]);
  6. }
  7. }
Now what happens when you do the same thing in a different way?
  1. public class ja {
  2. public static void main(String[] args)
  3. {
  4. int[] a = {7,3,5,8,1};
  5. System.out.println(a[a.length]);
  6. }
  7. }
Now, just for grins, try this and see how it gives you the last item in the array:
  1. public class ja {
  2. public static void main(String[] args)
  3. {
  4. int[] a = {7,3,5,8,1};
  5. System.out.println(a[a.length - 1]);
  6. }
  7. }
That's why I used matrix.length-1.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: how to set up my for loop to print my array in reverse

 
0
  #9
Mar 10th, 2005
Mines better than yours :cheesy:

joking!
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: how to set up my for loop to print my array in reverse

 
0
  #10
Mar 11th, 2005
Hi everyone,

I FORGOT THE -1 SO SUE ME

For the love of god any programmer would know that was a careless mistake.

Seriously sylk don't tell me you didn't realize that?

Richard West
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC