Results 1 to 5 of 5
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default OK Guys! My new and improved code.

    Hey, i appreciate all the help i got from users here. Ignore my other thread, but there is just one thing im not sure how to go about doing. Right now, i only display the result 2d array. But i need to find a way to display all the arrays like this :

    1 2 3
    1 2 3
    1 2 3
    +
    1 1 1
    1 1 1
    1 1 1
    =
    2 3 4
    2 3 4
    2 3 4

    Java Code:
    import java.util.Scanner;
    
    public class Lab3
    {
    	public static void main(String [] args)
     	{
     		Scanner keyboard = new Scanner(System.in);
     		int menuChoice = 0;
     		int rows = 0, cols = 0;
     		int[][] arrayOne = null;
     		int[][] arrayTwo = null;
    
    		//If the user does not want to run the program, they enter 3, if they enter 1 or 2, they are prompted to enter their data.
    		do
    		{
    			System.out.println("Choose from the menu:" + "\nAdd two arrays: 1" +"\nSubtract two arrays: 2" + "\nExit: 3");
    			menuChoice = keyboard.nextInt();
    
    			if(menuChoice != 3)
    			{
    				System.out.println("How many rows are there?");
    				rows = keyboard.nextInt();
    
    				System.out.println("How many columns are there?");
    				cols = keyboard.nextInt();
    
    				arrayOne = new int[rows][cols];
    				arrayTwo = new int[rows][cols];
    			}
    
    			switch(menuChoice)
    			{
    				case 1:
    				{
    					System.out.println("Enter data for first array.");
    					arrayOne = arrayInput(arrayOne);
    					System.out.println("Enter data for second array.");
    					arrayTwo = arrayInput(arrayTwo);
    					arrayAdd(arrayOne, arrayTwo);
    					break;
    				}
    				case 2:
    				{
    					System.out.println("Enter data for first array.");
    					arrayOne = arrayInput(arrayOne);
    					System.out.println("Enter data for second array.");
    					arrayTwo = arrayInput(arrayTwo);
    					arraySub(arrayOne, arrayTwo);
    					break;
    				}
    				case 3:
    				{
    					System.exit(0);
    					break;
    				}
    			}
    		}while(menuChoice != 3);
    	}
    	
    	//Pass one array into the method and user enters a value for each location in the 2D array. It returns the filled array.
    	public static int[][] arrayInput(int[][] array)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		
    		for(int row = 0; row < array.length; row++)
    		{
    			for(int col = 0; col < array.length; col++)
    			{
    				array[row][col] = keyboard.nextInt();
    			}
    		}
    		return array;
    	}
    	
    	//Passes both arrays to the method and adds them together and displays the result.
    	public  static void arrayAdd(int[][] arrayOne, int[][] arrayTwo)
    	{
    		for(int i = 0; i < arrayOne.length; i++)
    		{
    			for(int j = 0; j < arrayOne[i].length; j++)
    			{
    				System.out.print(arrayOne[i][j] + arrayTwo[i][j]+ " ");
    			}
    			System.out.println();
    		}		
    	}
    	
    	//Passes both arrays to the method and subtracts them and displays the result.
    	public static void arraySub(int[][] arrayOne, int[][] arrayTwo)
    	{
    		for(int i = 0; i < arrayOne.length; i++)
    		{
    			for(int j = 0; j < arrayOne[i].length; j++)
    			{
    				System.out.print(arrayOne[i][j] - arrayTwo[i][j]+ " ");
    			}
    			System.out.println();
    		}
    	}
    }

  2. #2
    Join Date
    Apr 2010
    Location
    Aberystwyth
    Posts
    6
    Rep Power
    0

    Default

    to print the three arrays you would just use

    Java Code:
    //Passes both arrays to the method and adds them together and displays the result.
    	public  static void arrayAdd(int[][] arrayOne, int[][] arrayTwo)
    	{
                    for(int i = 0; i < arrayOne.length; i++)
    		{
    			for(int j = 0; j < arrayOne[i].length; j++)
    			{
    				System.out.print(arrayOne[i][j]+" ");
    			}
    			System.out.println();
    		}
                    System.out.println("+");
                    for(int i = 0; i < arrayOne.length; i++)
    		{
    			for(int j = 0; j < arrayOne[i].length; j++)
    			{
    				System.out.print(arrayTwo[i][j]+ " ");
    			}
    			System.out.println();
    		}
                    System.out.println("=");
    		for(int i = 0; i < arrayOne.length; i++)
    		{
    			for(int j = 0; j < arrayOne[i].length; j++)
    			{
    				System.out.print(arrayOne[i][j] + arrayTwo[i][j]+ " ");
    			}
    			System.out.println();
    		}		
    	}
    is this what you meant?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    It's better to give some comment rather giving a code in very beginning lol. So our OPs will learn a lot about those things.

  4. #4
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    ya, that works, but i feel like it can be simplified into a method, so the code isnt repetitive

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. hi guys
    By pvsnmurthy in forum Introductions
    Replies: 0
    Last Post: 04-16-2009, 09:37 AM
  2. Hi guys,
    By pereira.lawrence.java in forum Introductions
    Replies: 0
    Last Post: 03-17-2009, 04:55 AM
  3. Hello Guys...
    By balaram.bocha in forum Introductions
    Replies: 0
    Last Post: 02-03-2009, 07:11 PM
  4. Hello Guys!
    By jeraldjamescapao in forum New To Java
    Replies: 3
    Last Post: 11-23-2008, 08:39 AM
  5. hi guys!!!
    By surenani in forum Introductions
    Replies: 4
    Last Post: 01-23-2008, 06:24 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
  •