Counting occurrences of numbers in an array?
-
I am trying to write a program which randomly picks 100 integers from 0 - 99, displays the numbers, and counts the occurrences of of each number and lists them. I have successfully written the code to generate the random array of 100 integers, but my counts are way off, and at the end of my count line, it keeps printing an array reference number, not the value. The code is listed below public class CountNumbers { /** * @param args */ public static void main(String[] args) { // Declare and create an array int[] numbers = createArray(); //Display the array System.out.println("The random integers between 0 and 9 are:"); displayArray(numbers); //count the occurrences of each number int[] counts = countOccur(numbers); //Display Counts System.out.println(); System.out.println("The occurrences of each number are:"); displayCounts(counts); System.out.println(counts); } // create an array of numbers public static int[] createArray() { // Declare an array of characters and create it int[] numbers = new int[100]; //Create numbers randomly and assign //them to the array for (int i = 0; i < numbers.length; i++) numbers[i] = (int)(Math.random() * 10); //return the array return numbers; } // Display the array of characters public static void displayArray(int[] numbers) { //Display the characters in the array 20 on each line for (int i = 0; i < numbers.length; i++) if ((i + 1) % 20 == 0) System.out.println(numbers[i]); else System.out.print(numbers[i] + " "); } // count the occurrences of each number public static int[] countOccur(int[] numbers) { //Declare an array of 10 integers int [] counts = new int[10]; //for each specific number in the array, count it for (int i = 0; i < counts.length; i++) counts[numbers[i]]++; return counts; } public static void displayCounts(int[] counts) { for(int i = 0; i < 10; i++) { if ((i + 1) % 10 == 0) System.out.println(i + ": " + counts[i]+ " | "); else System.out.print(i + ": " + counts[i] + " | " + " "); } } }
-
Answer:
This looks like homework so I'm not just going to hand you the answer, but here's a hint. The reason why your counts are way off is this: for (int i = 0; i < counts.length; i++) counts[numbers[i]]++; How many times does that loop repeat? Is that enough for it to examine every item in the 'numbers' array? The reason why the array's identity is printed at the very end is this line: System.out.println(counts); The only reason I can think of for why that line even exists is that it might be a leftover from your first pass at writing an outline of the program. The fix should be obvious.
Mike at Yahoo! Answers Visit the source
Related Q & A:
- How can I change a value in an array?Best solution by Stack Overflow
- How to pass javascript jQuery variable value in php array?Best solution by Stack Overflow
- How to post an array of complex objects (that has an array of complex objects) with jQuery?Best solution by Stack Overflow
- how can i remove an array from an array?Best solution by Stack Overflow
- Counting cards in blackjack?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.