# Thread: Help with Array/Method Exercises

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.

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.

3. Member
Join Date
Sep 2011
Posts
21
Rep Power
0

## 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?

#### Posting Permissions

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