1. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## 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.

3. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## Re: Arrays

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.

4. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## Re: Arrays

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

H

5. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## 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.

6. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## 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. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## Re: Arrays

What does your code look like now?
(And don't forget the [code] tags [/code])

9. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## 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. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## Re: Arrays

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

11. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## 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. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

Helen

13. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## 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.

14. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## 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. Member
Join Date
Mar 2012
Location
Ireland
Posts
18
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
13,126
Rep Power
23

## Re: Arrays

Nice job, and good luck with it.

#### Posting Permissions

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