# Arrays

• 03-19-2012, 01:50 PM
helen1974
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);
}
}
• 03-19-2012, 02:01 PM
Tolls
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.
• 03-19-2012, 04:55 PM
Tolls
Re: Arrays
In reference to your PM:
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.
• 03-19-2012, 05:50 PM
helen1974
Re: Arrays
In your experience, would you consider that your shortened solution could be described as a "nested for loop"

H
• 03-19-2012, 05:53 PM
Tolls
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.
• 03-19-2012, 05:57 PM
helen1974
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
• 03-19-2012, 06:23 PM
helen1974
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
• 03-19-2012, 06:27 PM
Tolls
Re: Arrays
What does your code look like now?
(And don't forget the [code] tags [/code])
• 03-19-2012, 06:36 PM
helen1974
Re: Arrays
Am i correct in thinking that I start my code with
Code:

`and finish with`
example

Code:

` system.out.println("hello world")`
Helen
• 03-19-2012, 06:37 PM
helen1974
Re: Arrays
Okay, don't respond to my last message, I have answered my own question, lol
• 03-19-2012, 06:38 PM
helen1974
Re: Arrays
Okay Tolls, here it is mate

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);         } }```
• 03-19-2012, 06:55 PM
helen1974
Re: Arrays
Hi Tolls, have you received my code from thread 13 ?

Helen
• 03-19-2012, 07:06 PM
Tolls
Re: Arrays
Ah right.
If you look at your original code in post 1:
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.
• 03-19-2012, 07:28 PM
helen1974
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
• 03-19-2012, 07:31 PM
helen1974
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
• 03-20-2012, 10:33 AM
Tolls
Re: Arrays
Nice job, and good luck with it.