Results 1 to 3 of 3
  1. #1
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default 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] + " ");
             	}
           }
        }
        }
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    ranjiths's Avatar
    ranjiths is offline Member
    Join Date
    Sep 2009
    Location
    India
    Posts
    19
    Rep Power
    0

    Default

    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

  1. Help on Serializing an array data structure
    By fonso gfx in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 03:54 AM
  2. Replies: 11
    Last Post: 04-01-2009, 04:43 AM
  3. Replies: 7
    Last Post: 02-11-2009, 10:14 PM
  4. Java Array of Structure
    By PAffiliates in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 07:08 AM
  5. Use if then else structure, help
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •