Results 1 to 20 of 26
Thread: Finding maximum..
 09062011, 02:08 AM #1Member
 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; } }
 09062011, 02:13 AM #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)?
 09062011, 02:14 AM #3Member
 Join Date
 Sep 2011
 Posts
 10
 Rep Power
 0
Re: Finding maximum..
Just 1 value. The largest number in any row
 09062011, 02:19 AM #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.
 09062011, 02:23 AM #5Member
 Join Date
 Sep 2011
 Posts
 10
 Rep Power
 0
Re: Finding maximum..
I thought about doing that, but wouldn't that only work for multidimensional arrays? Because in this case I have 3 1d arrays...
I apologize for my noobness haha
 09062011, 02:27 AM #6
Re: Finding maximum..
Then use one loop for each array and carry the results forward from each loop.
 09062011, 02:32 AM #7Member
 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
 09062011, 02:34 AM #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.
 09062011, 02:38 AM #9Member
 Join Date
 Sep 2011
 Posts
 10
 Rep Power
 0
Re: Finding maximum..
Okay, thanks, I'll try that and see how it works out. :)
 09062011, 02:39 AM #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 }
 09062011, 02:53 AM #11Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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];
 09062011, 02:58 AM #12Member
 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. :/
 09062011, 03:01 AM #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.
 09062011, 03:16 AM #14Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
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.
 09062011, 03:21 AM #15Member
 Join Date
 Sep 2011
 Posts
 10
 Rep Power
 0
Re: Finding maximum..
hahaha yeah that's pretty much what I was going for. thanks
 09062011, 03:27 AM #16
 09062011, 03:42 AM #17Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
Re: Finding maximum..
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]; }
 09062011, 03:48 AM #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 } } } }
Last edited by Junky; 09062011 at 03:52 AM.
 09062011, 03:53 AM #19Senior Member
 Join Date
 Aug 2011
 Posts
 251
 Rep Power
 4
Re: Finding maximum..
loop arr {
 09062011, 03:56 AM #20
Similar Threads

Finding minimum and maximum of 'n' numbers...please help!
By snw19 in forum New To JavaReplies: 8Last Post: 03262011, 12:38 AM 
Finding maximum number at index position
By Shyamz1 in forum New To JavaReplies: 9Last Post: 10272010, 08:14 PM 
Maximum subsequence
By dxg in forum New To JavaReplies: 12Last Post: 09252010, 05:40 PM 
find maximum value from for loop
By napi1234 in forum New To JavaReplies: 15Last Post: 06022010, 03:39 PM 
Maximum size of an array
By Hasan in forum New To JavaReplies: 1Last Post: 05202007, 11:11 AM
Bookmarks