Results 1 to 3 of 3
Thread: Two D array structure
- 05-05-2011, 02:28 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 31
- Rep Power
- 0
Two D array structure
i have a project due tomm and am really in a pickle...i can't seem to configure the array to display just the diagnols are the corners...
here is the output im currently getting...
1 2 3
4 5 6
7 8 9
5
3 5 7
1 5 9
1 3 7 9
the first two outputs are right but i need it to be:
(xs are blank spaces)
1x3
xxx
7x9
and
1xx
x5x
xx9
and
xx3
x5x
7xx
what am i doing wrong?
Java Code:public class Project3 { public static void main(String[] args) { int a [][] = {{1, 2, 3} , {4, 5, 6} , {7, 8, 9}}; All(a); Center(a); //5 DiagRight(a); // 3, 5, 7 DiagLeft(a); Corner(a); } public static void All(int [][] x) { for (int R=0; R<=2; R++) { for (int C=0; C<=2; C++) { System.out.print(x[R][C] + " "); } System.out.println(" "); } } public static void Center(int [][] x){ for (int R=1; R<2; R++) { for (int C=1; C<2; C++) { System.out.println(" "); System.out.print(x[R][C] + " "); } System.out.println(" "); } } public static void DiagLeft(int [][] x){ System.out.println(" "); for (int R=0; R<=2; R++){ for (int C=0; C<=2; C++) { if(R==C) { System.out.print(x[R][C] + " "); } } } } public static void DiagRight(int [][] x){ System.out.println(" "); for (int R=0; R<=2; R++){ for (int C=0; C<=2; C++) { if(R == 1 && C == 1 || R == 2 && C == 0 || R == 0 && C == 2) { System.out.print(x[R][C] + " "); } } } } public static void Corner(int [][] x){ System.out.println(" "); for (int R=0; R<=2; R++){ for (int C=0; C<=2; C++) { if(R != 1 && C != 1) { System.out.print(x[R][C] + " "); } } } } }
- 05-05-2011, 05:30 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Java Code:public static void DiagRight(int [][] x){ System.out.println(" "); for (int R=0; R<=2; R++){ for (int C=0; C<=2; C++) { if(R == 1 && C == 1 || R == 2 && C == 0 || R == 0 && C == 2) { System.out.print(x[R][C] + " "); }[color=blue] else { // figure out what you should print if you are // *not on the diagonal* }[/color] } [color=blue]// Don't forget a newline[/color] } }
Also, fix the variable and method names. These start with a lowercase letter in Java.
- 05-05-2011, 11:01 AM #3
the answer is here: 2-D array help
All that a fellow passenger on this earth expects from you is your loving words and support. Never talk in a way that hurts others or insults them. Sarvejana Sukhinobavanthu.
Similar Threads
-
Help on Serializing an array data structure
By fonso gfx in forum New To JavaReplies: 3Last Post: 10-19-2009, 02:54 AM -
Creating a Data Structure to Hold an Array and Distance Value
By rdoane in forum New To JavaReplies: 11Last Post: 04-01-2009, 03:43 AM -
int array - any other data structure that can handle large sets?
By RR_QQ in forum New To JavaReplies: 7Last Post: 02-11-2009, 09:14 PM -
Java Array of Structure
By PAffiliates in forum New To JavaReplies: 1Last Post: 01-28-2008, 06:08 AM -
Use if then else structure, help
By paul in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks