• Post Reply Bookmark Topic Watch Topic
  • New Topic

Diagonal Difference  RSS feed

 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to post my code to get it reviewed and ask for suggestions to improve it.

The first line of input will be the size of the matrix followed by the input array. The goal is to just output the result as efficiently as possible, so I don't even have to have an array if I can help it.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ways to improve include:

1) break your code into methods. Having all your code in a single method (and main in particular) is bad form.
2) Comment the code. If nothing else, you should have a block stating what the program does.
3) it's a little confusing...what is a "diagonal difference"? Also, if your data is a matrix, why don't you use a more appropriate data type than an array?
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, there are no private methods and no comments because I had to do this on an online editor. I had to submit the code in that method only, so please excuse me.

Sorry for not being clear about the specification. I am trying to find the absolute difference between the sum of the diagonals.

The input is from standard input. For example,

2
-6 9
14 8

(or)

3
2 -3 5
-5 8 -10
45 35 -2
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also you should have some print statements so the user knows what to enter at each prompt.

Stop using online editors; I have not yet found one which is actually any good. Use a decent text editor on your computer and install Java.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the lack of context. Here is the source: Source. Hence the use of online editor, programming in main method etc..
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Sorry, there are no private methods and no comments because I had to do this on an online editor. I had to submit the code in that method only, so please excuse me.


I just tried to run a program with a private method and it worked fine. FYI.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that information, Knute!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!