Results 1 to 20 of 26
Thread: Finding maximum..
- 09-06-2011, 02:08 AM #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:But then how do I change it so it goes through each number in each row?Java Code:int largeIndex = 0; for(int i = 0; i < a1.length; i++){ if(a1[i] > a1[largeIndex]){ largeIndex = i; } }
- 09-06-2011, 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)?
- 09-06-2011, 02:14 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 10
- Rep Power
- 0
Re: Finding maximum..
Just 1 value. The largest number in any row
- 09-06-2011, 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.
- 09-06-2011, 02:23 AM #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
- 09-06-2011, 02:27 AM #6
Re: Finding maximum..
Then use one loop for each array and carry the results forward from each loop.
- 09-06-2011, 02:32 AM #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
- 09-06-2011, 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.
- 09-06-2011, 02:38 AM #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. :)
- 09-06-2011, 02:39 AM #10
Re: Finding maximum..
Then method two simply finds and returns max of a 1d array.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 }
- 09-06-2011, 02:53 AM #11
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
Re: Finding maximum..
Why don't you use 1 array for all the lines?
Then use a simple code as:
Doing int big = 0; is wrong! sinec your biggest number might be negative.Java Code:int big = arr[0]; for(int i=0;i<arr.length;i++) if(arr[i]>big) big = arr[i];
- 09-06-2011, 02:58 AM #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. :/
- 09-06-2011, 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.
- 09-06-2011, 03:16 AM #14
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
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 #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
- 09-06-2011, 03:27 AM #16
- 09-06-2011, 03:42 AM #17
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
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]; }
- 09-06-2011, 03:48 AM #18
Re: Finding maximum..
One loop and one variable (excluding the four arrays).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; 09-06-2011 at 03:52 AM.
- 09-06-2011, 03:53 AM #19
Senior Member
- Join Date
- Aug 2011
- Posts
- 249
- Rep Power
- 2
Re: Finding maximum..
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 <<<<<<<loop arr {
- 09-06-2011, 03:56 AM #20
Similar Threads
-
Finding minimum and maximum of 'n' numbers...please help!
By snw19 in forum New To JavaReplies: 8Last Post: 03-25-2011, 11:38 PM -
Finding maximum number at index position
By Shyamz1 in forum New To JavaReplies: 9Last Post: 10-27-2010, 08:14 PM -
Maximum subsequence
By dxg in forum New To JavaReplies: 12Last Post: 09-25-2010, 05:40 PM -
find maximum value from for loop
By napi1234 in forum New To JavaReplies: 15Last Post: 06-02-2010, 03:39 PM -
Maximum size of an array
By Hasan in forum New To JavaReplies: 1Last Post: 05-20-2007, 11:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks