Results 1 to 3 of 3
  1. #1
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Help with Array/Method Exercises

    I've got a couple of questions that I don't fully understand.

    1) Create a method initialize() to take an array of integers and two values, downLimit and upLimit to initialize each elements of the array to a random number between downLimit and upLimit. (Think about generalization)

    Use: Create a new array of integers with the 50 elements and initialize its contents to a random value between 10 to 20.

    2) Takes the array created in question 1, and counts how many times each number occurred in the array, and then print the result.
    For example: if the given array is {12, 11, 15, 12, 11, 12} then the output should be:

    11 occurs = 2
    12 occurs = 3
    12 occurs = 1

    I'm not sure how I should approach this. There is a formula to take a random number between a minimum and maximum, I do know that, but when I have to count how many times an integer occurs, it stumbles me. Should I loop through the first array and collect all of the unique numbers into an empty array, so that I can use that to count the occurances?

    Hope you guys can help. Thanks.

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

    Default Re: Help with Array/Method Exercises

    For part 2 you can sort the array. Check if this is allowed. If the array is sorted then it is a simple matter of counting the occurrence of each number. Simply loop over the array starting with the second value and keep checking if the number is the same as the previous. If it is increment a count, else print count and reset it back to 1.

  3. #3
    Zora is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: Help with Array/Method Exercises

    I haven't learned how to sort arrays yet, and my instructor suggested that I should only use what we've learned. For loops, while loops, if/else statements, methods, and arrays. Can you show me another way?

Similar Threads

  1. need some exercises
    By JohnPringle83 in forum New To Java
    Replies: 10
    Last Post: 05-11-2011, 08:48 PM
  2. OOP exercises
    By sunde887 in forum New To Java
    Replies: 6
    Last Post: 01-09-2011, 09:05 PM
  3. Exercises
    By amzers in forum New To Java
    Replies: 6
    Last Post: 12-12-2010, 10:58 AM
  4. Some exercises
    By javaamateur in forum New To Java
    Replies: 7
    Last Post: 12-02-2009, 01:00 AM
  5. Exercises
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-14-2009, 11:20 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
  •