# Finding maximum..

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-06-2011, 02:08 AM
putrify
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:
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?
• 09-06-2011, 02:13 AM
Junky
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)?
• 09-06-2011, 02:14 AM
putrify
Re: Finding maximum..
Just 1 value. The largest number in any row
• 09-06-2011, 02:19 AM
Junky
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.
• 09-06-2011, 02:23 AM
putrify
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
• 09-06-2011, 02:27 AM
Norm
Re: Finding maximum..
Then use one loop for each array and carry the results forward from each loop.
• 09-06-2011, 02:32 AM
putrify
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
• 09-06-2011, 02:34 AM
Norm
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.
• 09-06-2011, 02:38 AM
putrify
Re: Finding maximum..
Okay, thanks, I'll try that and see how it works out. :)
• 09-06-2011, 02:39 AM
Junky
Re: Finding maximum..
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.
• 09-06-2011, 02:53 AM
tnrh1
Re: Finding maximum..
Why don't you use 1 array for all the lines?
Then use a simple code as:
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.
• 09-06-2011, 02:58 AM
putrify
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. :/
• 09-06-2011, 03:01 AM
Junky
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.
• 09-06-2011, 03:16 AM
tnrh1
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.
• 09-06-2011, 03:21 AM
putrify
Re: Finding maximum..
hahaha yeah that's pretty much what I was going for. thanks
• 09-06-2011, 03:27 AM
Junky
Re: Finding maximum..
Quote:

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.
• 09-06-2011, 03:42 AM
tnrh1
Re: Finding maximum..
Quote:

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:
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];            }```
• 09-06-2011, 03:48 AM
Junky
Re: Finding maximum..
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).
• 09-06-2011, 03:53 AM
tnrh1
Re: Finding maximum..
Quote:

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 <<<<<<<
• 09-06-2011, 03:56 AM
Junky
Re: Finding maximum..
Do you see how in the stuff method I call the findMax method 4 times, once with each array?
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last