• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need help to solve a matrix  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to solve a problem. It works for m x m  matrix not for m x n .so the problem is, a m x m or m x n matrix has only one 0 in somewhere. If we encounter 0 we have to make 0 towards to its row and column. For an example:

1 2 3 5
4 5 6 8
0 7 9 1
should give:

0 2 3 5
0 5 6 8
0 0 0 0

Here is my code:

 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your loop termination conditions for zeroing out a row/column are wrong. Trace through those loops carefully.

Think: What do arr.length and arr[i].length represent? Are those the appropriate values to use in each loop?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, it's good that you extracted the code to display the array into a separate method, print(). That's a good practice that avoids duplication and it makes your other code easier to follow.

You can clean up the code in the print() method a little bit.  If you don't pass anything to println(), it will simply print a new line. You don't need to pass an empty string like what you're doing. Also, there's no need for that if statement. Just move the println statement outside of the inner for-loop. That is, after printing all the elements in a row, you print a new line before you start printing the elements of the next row.
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. You are right I don't need that inner if condition. But still I could not solve it.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please TellTheDetails? (that's a link).  What did you try?  What happened?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of arr.length and arr[i].length, which of these represents the row length and which one represents the column height?  Do these match the intent of the loop that each of these is used in? Like I said, trace through those two loops carefully and make sure you FULLY understand what's going on. Don't just eyeball it because your mind wants to believe your code is correct even when it's not.  Be the computer and try to follow your instructions as you have written them.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember, it works if you have a square matrix.  When you have a square matrix, arr.length and arr[i].length are the same. Your problem only manifests itself when you have rectangular matrix, when arr.length and arr[i].length are *not* equal.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mentioned earlier that extracting code out to its own method is a good way to avoid duplication. It's also a good way to avoid getting lost in the details of your implementation and making your intent more obvious.

You could extract each of those loops to their own method and write simpler, more high-level abstract code in your main() method. Doing this might have helped you catch your logic error more easily:
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here the code now and I am in the limbo. Need help.

 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell the details.  What happens when you run the program?  Why are you in limbo?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Hasan wrote:Here the code now and I am in the limbo. Need help.

You were already given the hints that you need to solve your problem. Did you read the rest of my responses? Answer those questions I asked and trace through your code carefully when you are dealing with a rectangular array. Don't expect us to spoon feed you the answer. You have to do your work.
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Junilu Lacar if I were like you I would not use that words "Don't expect us to spoon feed you the answer". Respect people or know how not to hurt some one's feeling. I tried I could not solve it. That's why I am here.  You sir opened javaranch to help people not to hurt people. If it pleases, here is the answer:

arr.length --> 3
arr[i].length --> 4
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose I should have just directed you to our wiki page - Show Some Effort

From my end, I'm not sure you've given enough consideration to the responses you've received. You haven't answered the questions I asked, you haven't responded to Knute's request (twice) to tell the details of what's happening, and you haven't shown that you traced through those loops and analyzed the values of the loop indices that you're using, which what I have been suggesting. If you feel slighted by me telling you that we're not here to hand out answers to you, then I apologize, I probably could have phrased it differently. However, the fact still remains that I don't see you demonstrating that you have shown effort in reading through and considering the tips you've already been given.

The purpose of the questions asked of you was not so much for you to give us an answer but for you to think of the answer and how those answer relates to your problem.  Given arr.length == 3, does that represent the number of elements in a row or the number of elements in a column? Is that how you're using arr.length in your loop?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!