Results 1 to 3 of 3
Thread: Help with Array/Method Exercises
- 11-02-2011, 03:37 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
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.
- 11-02-2011, 03:45 AM #2
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.
- 11-02-2011, 03:50 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
need some exercises
By JohnPringle83 in forum New To JavaReplies: 10Last Post: 05-11-2011, 07:48 PM -
OOP exercises
By sunde887 in forum New To JavaReplies: 6Last Post: 01-09-2011, 08:05 PM -
Exercises
By amzers in forum New To JavaReplies: 6Last Post: 12-12-2010, 09:58 AM -
Some exercises
By javaamateur in forum New To JavaReplies: 7Last Post: 12-02-2009, 12:00 AM -
Exercises
By lclclc in forum New To JavaReplies: 3Last Post: 09-14-2009, 10:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks