Results 1 to 16 of 16

Thread: Arrays

  1. #1
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Arrays

    Hi All,

    Is there a shorter way to achieve the following code, its a simple array table which is 3 rows by 4 columns. Each number in the table is multiplied by 3 and then the max value position is given by identifying its position in the table, ie max_value is 33, its position is row 1, column 4

    Any help would be brilliant.

    import java.util.Scanner;
    public class page63
    {
    public static void main(String []args)
    {
    Scanner myinput = new Scanner(System.in);
    int [][] table = {{0,1,11,3},{8,9,10,11},{4,5,6,1}};
    int i,j;
    int NO_OF_ROWS = 3;
    int NO_OF_COLS = 4;
    int max_val = table[0][0];
    int max_row_pos = 0;
    int max_col_pos = 0;

    for(i=0; i < NO_OF_ROWS; i++)
    {
    for(j=0; j < NO_OF_COLS; j++)
    {
    table[i][j] = table[i][j]*3;
    }
    }
    for(i=0; i < NO_OF_ROWS; i++)
    {
    for(j=0; j < NO_OF_COLS; j++)
    {
    System.out.print(table[i][j] + "\t" );
    }
    System.out.println();
    }
    for(i=0; i < NO_OF_ROWS; i++)
    {
    for(j=0; j < NO_OF_COLS; j++)
    {
    if(table[i][j]>max_val)
    {
    max_val=table[i][j];
    max_row_pos=i+1;
    max_col_pos=j+1;
    }
    }
    }
    System.out.println(" ");
    System.out.println("Max value = "+max_val);
    System.out.println(" ");
    System.out.println("it's co-ordinates are row "+max_row_pos+" column "+max_col_pos);
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Arrays

    Please use [code] tags [/code] when posting code.
    That's quite difficult to follow otherwise.

    You could do it all in the first loop.
    Multiply, then print out, then check for maxVal.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Arrays

    In reference to your PM:
    Java Code:
    for(i=0; i < NO_OF_ROWS; i++) {
       for(j=0; j < NO_OF_COLS; j++) {
          // Do some stuff in here.
       }
    }
    You do the above three times.
    You could do it just the once, doing all three different things in there instead.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    In your experience, would you consider that your shortened solution could be described as a "nested for loop"

    H

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Arrays

    It's a nested loop since the inner loop (NO_OF_COLS one) is nested inside the outer loop (NO_OF_ROWS).
    With the three sets all you had was three nested loops.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    Thanks Tolls,

    Given that you believe it to be nested and given your previous advice, I will continue to work at it until I get it resolved, I just didn't want to be working at it if I was way off the mark and according to my lecturer, it must be a nested FOR loop that I use.

    Thanks for the help today.

    Helen x

  7. #7
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    Okay Tolls, I think I got it, "almost", I have eliminated all of the FOR loops except ONE. Problem is that all numbers in the Array are either printing in one long ROW or one long COLUMN, but not printing as a table, I have not touched any of the "println" commands, any ideas as to where I am gone wrong.

    Helen

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Arrays

    What does your code look like now?
    (And don't forget the [code] tags [/code])
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    Am i correct in thinking that I start my code with
    Java Code:
    and finish with
    example

    Java Code:
     system.out.println("hello world")
    Helen

  10. #10
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    Okay, don't respond to my last message, I have answered my own question, lol

  11. #11
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    Okay Tolls, here it is mate

    Java Code:
    import java.util.Scanner;
    public class page63
    {
    	public static void main(String []args)
    	{
    		Scanner myinput = new Scanner(System.in);
    		int [][]  table =  {{0,1,15,3},{8,9,10,11},{4,5,6,1}};
    		int i,j;
    		int NO_OF_ROWS = 3;
    		int NO_OF_COLS = 4;
    		int max_val = table[0][0];
    		int max_row_pos = 0;
    		int max_col_pos = 0;
    		
    		for(i=0; i < NO_OF_ROWS; i++)
    		{
    			for(j=0; j < NO_OF_COLS; j++)
    			{
    				table[i][j] = table[i][j]*3;
    				System.out.print(table[i][j]+"\t");		
    			
    			if(table[i][j]>max_val)
    				{
    					max_val=table[i][j];
    					max_row_pos=i;
    					max_col_pos=j;
    				}
    			}
    		}
    				System.out.println(" ");
    				System.out.println("Max value = "+max_val);
    				System.out.println(" ");
    				System.out.println("it's co-ordinates are row "+max_row_pos+" column "+max_col_pos);
    	}
    }

  12. #12
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    Hi Tolls, have you received my code from thread 13 ?

    Helen

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Arrays

    Ah right.
    If you look at your original code in post 1:
    Java Code:
    for(i=0; i < NO_OF_ROWS; i++)
    {
       for(j=0; j < NO_OF_COLS; j++)
       {
          System.out.print(table[i][j] + "\t" ); 
       }
       System.out.println();
    }
    You'll see you're missing the prinln() call at the end of the outer loop.
    That gives you your new line for each row.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    I cant see all the original code in post 1, nor do I have it in java, as I changed it to try to resolve my problem, but I am guessing that the println() I am missing should be placed around line number 36 of my java code ?

    Helen

  15. #15
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Arrays

    Tolls, your a genius and the best thing is you guided me as opposed to giving me the answer.

    I GOT IT TO WORK

    yippee

    lol

    Helen

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Arrays

    Nice job, and good luck with it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  2. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  5. Arrays
    By swim_fan08 in forum New To Java
    Replies: 7
    Last Post: 04-11-2009, 03:02 PM

Posting Permissions

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