1. Member
Join Date
Sep 2011
Posts
10
Rep Power
0

## Finding maximum..

I have a text file that looks like this:

400 430 560
305 250 700
800 920 350
670 800 960

It's sorted into three arrays, each column is an array.
But I have to find the largest number, and this needs to be done by row. I'm confused :S

I know by using this code I can find the largest in one array:
Java Code:
```	int largeIndex = 0;

for(int i = 0; i < a1.length; i++){
if(a1[i] > a1[largeIndex]){
largeIndex = i;
}
}```
But then how do I change it so it goes through each number in each row?

2. ## Re: Finding maximum..

:confused:

Are you trying to find the largest number (display 1 value) or the largest number in each row (display 4 numbers)?

3. Member
Join Date
Sep 2011
Posts
10
Rep Power
0

## Re: Finding maximum..

Just 1 value. The largest number in any row

4. ## Re: Finding maximum..

In that case you will need nested loops. Outer loop for rows, inner loop for columns. Compare each value with largest so far.

5. Member
Join Date
Sep 2011
Posts
10
Rep Power
0

## Re: Finding maximum..

I thought about doing that, but wouldn't that only work for multi-dimensional arrays? Because in this case I have 3 1d arrays...
I apologize for my noobness haha

6. ## Re: Finding maximum..

Then use one loop for each array and carry the results forward from each loop.

7. Member
Join Date
Sep 2011
Posts
10
Rep Power
0

## Re: Finding maximum..

Yeah, I was thinking of finding the max in each array and then comparing to see which is largest...something along those lines

8. ## Re: Finding maximum..

That's one approach. That approach will get harder if you increase the number of arrays to search.
Another is to find the largest so far, carrying forward its value for each loop.

9. Member
Join Date
Sep 2011
Posts
10
Rep Power
0

## Re: Finding maximum..

Okay, thanks, I'll try that and see how it works out. :)

10. ## Re: Finding maximum..

Java Code:
```method one {
declare max
loop number of rows {
value = method two(current row)
if value is greater than max {
change max
}
}
display max
}```
Then method two simply finds and returns max of a 1d array.

11. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
3

## Re: Finding maximum..

Why don't you use 1 array for all the lines?
Then use a simple code as:
Java Code:
```int big = arr[0];
for(int i=0;i<arr.length;i++)
if(arr[i]>big)
big = arr[i];```
Doing int big = 0; is wrong! sinec your biggest number might be negative.

12. Member
Join Date
Sep 2011
Posts
10
Rep Power
0

## Re: Finding maximum..

I would have used 1 array, but the instructions on the assignment say the program should read the data from the file into 3 separate arrays. :/

13. ## Re: Finding maximum..

Gah!

I was working under the assumption that you had a 2D array. Neverless the pseudocode posted above would still work with some minor tweaking.

14. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
3

## Re: Finding maximum..

lol this is so stupid,
then just do as I did but to each line which means 3 damn loops :0
So for that you need 4 variables, bigAr1, bigAr2, bigAr3 and bigAr4.
After finding the biggest number in each line you will have to make tones of if's conditions to find the largest one between those 4.

15. Member
Join Date
Sep 2011
Posts
10
Rep Power
0

## Re: Finding maximum..

hahaha yeah that's pretty much what I was going for. thanks

16. ## Re: Finding maximum..

Originally Posted by tnrh1
lol this is so stupid,
then just do as I did but to each line which means 3 damn loops :0
So for that you need 4 variables, bigAr1, bigAr2, bigAr3 and bigAr4.
After finding the biggest number in each line you will have to make tones of if's conditions to find the largest one between those 4.
What a load of tosh. You only need one for loop and you only need one variable.

17. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
3

## Re: Finding maximum..

Originally Posted by Junky
What a load of tosh. You only need one for loop and you only need one variable.
Show me the a shorter solution if the arrays lengths are diffrent.

In case the arrays lengths is the same, here is a better solution I worked on now:
Java Code:
```int biggest = ar1[0];
for (int k=0;k<ar1.length;k++)
{
if(ar1[k]>biggest)
biggest = ar1[k];
if(ar2[k]>biggest)
biggest = ar2[k];
if(ar3[k]>biggest)
biggest = ar3[k];
if(ar4[k]>biggest)
biggest = ar4[k];
}```

18. ## Re: Finding maximum..

Java Code:
```class Foo {
declare max
declare arr1
declare arr2
declare arr3
declare arr4

public void stuff() {
//fill arrays
findMax(arr1);
findMax(arr2);
findMax(arr3);
findMax(arr4);
display max
}

private void findMax(arr) {
loop arr {
if current value is greater than max
change max
}
}
}
}```
One loop and one variable (excluding the four arrays).
Last edited by Junky; 09-06-2011 at 03:52 AM.

19. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
3

## Re: Finding maximum..

loop arr {
How can you run a loop on 4 arrays if the length of them is diffrent?Looking at your post number makes me think twice that I'm wrong but I still can't find an asnwer to that <<<<<<<

20. ## Re: Finding maximum..

Do you see how in the stuff method I call the findMax method 4 times, once with each array?

Page 1 of 2 12 Last

#### Posting Permissions

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