Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * count how many numbers in the array are between 1 - 10, 11 - 20, etc.
- * kind-of like making a histogram
- */
- public static void tally(int[] array) {
- int i;
- int[] tally = new int[10]; //table to hold range counts
- /*
- * the for loop iterates over the entire array, testing every element for its range
- * the value of the element is subtracted by 1, and then integer-divided by 10
- * (remember, integer division floors)
- * this obtains the index of the total table to increment
- * total 1-10 is index 0, 11-20 is index 1, etc.
- * ---
- * some example math problems (remember, integer division)
- * (10 - 1) / 10 = 0
- * (22 - 1) / 10 = 2
- * ---
- * and kids say they'll never use math :)
- */
- for(i = 0; i < array.length; i++) {
- tally[(array[i] - 1) / 10]++;
- }
- /*
- * iterate over the total table and print the values
- * range numbers are calculated based on i
- */
- for(i = 0; i < 10; i++) {
- System.out.printf("%3d - %3d: %3d\n", (i * 10) + 1, (i + 1) * 10, tally[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement