Results 1 to 3 of 3
  1. #1
    stateofyouboi is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default simple array processing(apparently!)

    any help at all with this would be great,ive spent hours googling yesterday but to no avail

    int [] nums = {10,20,10,20,60,10,5,60,5,60}

    1. (i)find the maximum in the array (ii)the position of where it occurs first and last
    2. find 2nd largest element in array
    3. find the maximum absolute difference between adjacent pairs of elements in the array
    4. determine IF 2 arrays are identical
    5. reverse the order of the array elements

    i was able to do q1 part (i) then i havent a clue!

    int highest = 0;

    for (int i=0; i<nums.length; i++) {
    if(nums[i] > highest)
    highest = nums[i];
    }
    System.out.println(highest);

    when it mentions determine if 2 arrays are identical i think it could be referring to my last exercise sheet which had the same array, im not sure. The lecturer will be going through this with us next week but i need all the help i can get so im trying to stay ahead of things if you know what i mean :) even help with one or two might point me int he right direction!

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    your 1. requirement is ok. for the 2. reguirement the implementation would be very easy if your array is already sorted, so before you assign the highest you could get the value inside highest and this value will be the 2nd largest. for sorting make a call to Arrays.sort(nums). the 3. can be also be implemented in simple for-loop. for 4. the question is, if you want to list all identical arrays. and for the 5. i would write a comparator the sort the ints in reverse order. for a ascending order the returned value is calculated by firstInt.compareTo(secondInt) and for a descending order a simply secondInt.compareTo(firstInt) can be used. but watch out: before you can call the compareTo you must convert your int to the class Integer. perhaps somebody can suggest an easier way.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The code you posted found the highest value in the array. For part (ii) what you need is another variable, say highestNdx, that "remembers" the index where you set or updated the highest

    Java Code:
    int highest = 0;
    int highestNdx = 0;
    
    for (int i=0; i<nums.length; i++) {
    if(nums[i] > highest)
        highest = nums[i];
        highestNdx = ???
    }
    -----------------------------

    If you can't use Arrays.sort() then the code to find the second highest is much the same as the code to find the highest value. All you need is a slightly more complicated condition for the if statement which expresses the condition "if current num is bigger than highest AND i is not equal to highestNdx" - basically you do search for the highest again ignoring the index position that you found initially.

    (With some ingenuity you could combine both searches into one for loop, but I don't think that's the intent here.)

Similar Threads

  1. instruction apparently not executed
    By rippon in forum AWT / Swing
    Replies: 4
    Last Post: 11-30-2010, 02:30 AM
  2. The field "name" doesnt apparently exsist :/
    By Addez in forum New To Java
    Replies: 6
    Last Post: 10-23-2010, 12:15 AM
  3. Processing input - Very simple but I can't do it
    By Hitsuin in forum New To Java
    Replies: 1
    Last Post: 08-23-2010, 04:31 PM
  4. My Simple Array Does Not Work!
    By Simplev_v in forum New To Java
    Replies: 16
    Last Post: 09-07-2009, 03:43 PM
  5. Simple array questions
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 02-15-2009, 06:57 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
  •