Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    putrify is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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. #3
    putrify is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Finding maximum..

    Just 1 value. The largest number in any row

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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. #5
    putrify is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default 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. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default Re: Finding maximum..

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

  7. #7
    putrify is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default 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. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default 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. #9
    putrify is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Finding maximum..

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

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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. #11
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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. #12
    putrify is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default 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. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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. #14
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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. #15
    putrify is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Finding maximum..

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

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Finding maximum..

    Quote Originally Posted by tnrh1 View Post
    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. #17
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Finding maximum..

    Quote Originally Posted by Junky View Post
    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. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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. #19
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 03-25-2011, 11:38 PM
  2. Finding maximum number at index position
    By Shyamz1 in forum New To Java
    Replies: 9
    Last Post: 10-27-2010, 08:14 PM
  3. Maximum subsequence
    By dxg in forum New To Java
    Replies: 12
    Last Post: 09-25-2010, 05:40 PM
  4. find maximum value from for loop
    By napi1234 in forum New To Java
    Replies: 15
    Last Post: 06-02-2010, 03:39 PM
  5. Maximum size of an array
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-20-2007, 11:11 AM

Posting Permissions

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