Results 1 to 15 of 15
  1. #1
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default How can I find out the largest number ... ?

    How can I pick out the largest number in a file ? The numbers in the file are from a range of 0.1 to 0.5 and there's are 100 of differently ranged numbers ...

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Add them all to an array, then loop through the array. Store the currently highest number (start with 0.0 or -1.0) in a variable, and when one of the numbers is higher than that, replace it. Something like this...

    Let's say this is your file:
    0.1
    0.3
    0.2
    0.2
    0.4
    0.2
    0.45
    0.3

    So you loop through each one:
    double highest = -1.0;
    is 0.1 higher than -1.0? yes, so highest = 0.1
    is 0.3 higher than 0.1? yes, so highest = 0.3
    is 0.2 higher than 0.3? no, so highest = 0.3
    is 0.2 higher than 0.3? no, so highest = 0.3
    is 0.4 higher than 0.3? yes, so highest = 0.4
    is 0.2 higher than 0.4? no, so highest = 0.4
    is 0.45 higher than 0.4? yes, so highest = 0.45
    is 0.3 higher than 0.45? no, so highest = 0.45

    Of course, that's just a general idea, and that logic would be put into a loop through the array instead of line-by-line. But it should give you a good idea of what to do.

  3. #3
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    The above method is correct from sorting algorithm point of view (of which there are many).
    However, if its not necessary for you to implement your own algorithm (bubble/inline sort), you can use the standard Collections.sort() method.

    Since your file contains doubles, Double objects already have a natural ordering so there is no need to write your own compareTo method.

    What you need to do is:
    1. Load all the numbers from the file and add them to an
    Java Code:
    ArrayList<Double> yourArrayList;
    (of type double).
    2. Run Collections.sort(yourArrayList);
    3. Thats it! Your arrayList is now sorted and the last number is the largest one.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Maximus-EVG View Post
    2. Run Collections.sort(yourArrayList);
    3. Thats it! Your arrayList is now sorted and the last number is the largest one.
    Sorting takes O(n*log(n)) steps while finding a maximum value would only take O(n) steps.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Maximus-EVG View Post
    The above method is correct from sorting algorithm point of view (of which there are many).
    My method doesn't sort them... it does not change or manipulate the array, nor create a new one. It records the highest value as it iterates through the entire thing. No sorting involved.

  6. #6
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    My method doesn't sort them... it does not change or manipulate the array, nor create a new one. It records the highest value as it iterates through the entire thing. No sorting involved.
    lol you're right, my thoughts got ahead of my reading, sorry about that!

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Maximus-EVG View Post
    lol you're right, my thoughts got ahead of my reading, sorry about that!
    Skimming can be deadly! Make sure you always drink 1%!

  8. #8
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    My method doesn't sort them... it does not change or manipulate the array, nor create a new one. It records the highest value as it iterates through the entire thing. No sorting involved.
    Can you give mean example like how the code looks like ? Sorry I'm just keep up Java and I had no ideas how can i start :(

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Most people won't give you the exact code, I will give you some pseudo code though
    Java Code:
    declare array list
    loop
      read line
      parse line
      //split if necessary
      store in array list
    end loop
    declare max value
    loop
      if condition
        max = i
      end if
    end loop
    return max

  10. #10
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Most people won't give you the exact code, I will give you some pseudo code though
    Java Code:
    declare array list
    loop
      read line
      parse line
      //split if necessary
      store in array list
    end loop
    declare max value
    loop
      if condition
        max = i
      end if
    end loop
    return max
    The array list is the array for my ranged of number ?

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The array list is to store all the information in memory, can be done like this as well
    Java Code:
    loop
       read line 
       split line 
       loop
         if condition
           reassign max
         end if
       end loop
    end loop
    return max

  12. #12
    wookyoo is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    well... for the question
    I declare two variables like result and compare value.
    also i used one loop and in the loop two values are compared
    if the compare is higher than the result then store result as compare value.

    i will show you just little bit more specific code but not exact one...


    value /* string value from the file or buffer */
    result /* convert value to double */
    value /* string value from the file or buffer */
    compare /* convert value to double and it is same way as result one */

    loop /* like for loop until there is no number anymore in the file or loop 98 times since your file has 100 numbers and got two numbers already. */
    if(result < compare)
    {
    result = compare;
    }
    value /* string value from the file or buffer */
    compare /* convert value to double and it is same way as result one */
    }

    finally show the result one.

  13. #13
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    oh ok ...
    I had found this code something in google:
    Here's the code :
    Java Code:
    {
            Scanner input=new Scanner(System.in);
            System.out.println("Enter positive real numbers: ");
            Integer array[]=new Integer[10];
            for(int i=0;i<array.length;i++){
                array[i]=input.nextInt();
            }
            System.out.println("Sorted Array in reverse order:");
            Arrays.sort(array,Collections.reverseOrder());
            for(int i=0;i<array.length;i++){
                System.out.println(array[i]);
            }
    
        }
    As you can see the code requires the user to type in the number ..
    How can i convert :
    Java Code:
    for (int j = 0; j < bitSet2.length() - 1 || j < result1.length() - 1; j++) {
    
                        if (bitSet2.get(j)) {
                            writer.append("1");
                            writer.append(",");
                            writer.flush();
                        } else {
                            writer.append("0");
                            writer.append(",");
                            writer.flush();
                        }
    
                    }
    
                    
                    nlast = (nlast / (ntarget + nresult - nlast));
                    float round = Round(nlast, 3);
    
                    writer.append(round);
                    writer.append("\n");
    
                    
                }
    As this code, the number(round) would be printed out into the file. How can this code to be modify so that it will be similar to the above ?

  14. #14
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Excuse me for sounding old fashioned, but why don't you write your own code based on the concepts and pseudocode provided? That way, you won't get in trouble for plagiarism, you will have actually learned the material, and you will feel better about having accomplished something.

  15. #15
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    See I agree, writing your own code would be far better here. I think the idea of cycling through the array with a conditional element (is it bigger than current biggest) would be the best idea for larger arrays. On small scale things the difference in required processes won't do much, but larger scale will take longer if you sort it first and then go for the last number.
    Also, sorting required the Java sort method, using the conditional you write the whole thing yourself so you can feel smart ;)
    ~Karenthian

Similar Threads

  1. Find the second largest number in the array
    By radhi16 in forum New To Java
    Replies: 5
    Last Post: 01-13-2011, 05:08 PM
  2. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  3. Replies: 12
    Last Post: 10-31-2010, 06:14 PM
  4. Find max number in 2D array?
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 04:27 PM
  5. Find a number from a string
    By florentp in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 09:01 PM

Posting Permissions

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