Ask your own question, for FREE!
Computer Science 26 Online
OpenStudy (anonymous):

how can i write a program that gets a set of n integers from the user and tells them the frequency of each number. Ex: Input: 3. 7. 2. 5. 8. 1. 3. 5. 5. 8 Output: 1 1 2 1 3 2 5 3 7 1 8 2 Im having trouble finding the frequency. Working in Java

OpenStudy (anonymous):

Well, for an input such as that, first you'd have to parse the string. Write a method that takes the input the user gives you, changes it from a string to a series of integers, and drop that into an array. From there, it should be easy to iterate through the array, count the occurrences of each number, sort them, and then output them as you've indicated. Good luck! Write some pseudocode if you're having trouble.

OpenStudy (anonymous):

Wrote a program that does what you asked, commented heavily. //////////////////////////////////////////////////////////// import java.util.Arrays; public class Main { public static void main (String[] args) { //input here is hardcoded, change that String input = "4;0;5;-0;0;7;-0;-7;2;6;0;2;3;5;-3;7;5;6;3;-3;-4;-5;7;-6;1;5;2;-2;3;1;-6;1;1;-3;4;-5;0;1;-5;-2;0;-7;5;-1;1;4;5;-1;1;6"; //creates an array of the numbers, split at the semicolons String[] sNums = input.split(";"); //turns that array into a sorted array of ints int[] nums = new int[sNums.length]; try { for (int ai = 0; ai < sNums.length; ai++) { nums[ai] = Integer.parseInt(sNums[ai]); //parses each String } Arrays.sort(nums); //sorts it } catch (Exception e) { /* handle it if you want */ } //now for the actual work //goes through the sorted array //starting at first number, add one to count //going to second, check if it's same as the last one //if so, add to count //if not, set count to 1 and print the last number and the last count int currentNum = Integer.MAX_VALUE; //current number it's on; set to max int as default int count = 0; //total of current number for (int ai = 0; ai < nums.length; ai++) { if (ai == 0) { //just starting the loop System.out.println("Number\t\t# Times Appeared"); currentNum = nums[ai]; //new current number count += 1; //add one, could also just set to one since it's always zero at this point } else if (nums[ai] != currentNum) { //if number isn't the same as the last one System.out.println(currentNum + "\t\t" + count); //print the last one currentNum = nums[ai]; //new current number count = 1; //set to 0 because new number and add 1 because at least one } else if (ai + 1 == nums.length) { //last entry in array System.out.println(((nums[ai] != currentNum) ? nums[ai] + "\t\t1" : currentNum + "\t\t" + (count + 1))); //print the last one; the shorthand if just decides whether or not the number is unique } else { //if it isn't just started and if the current number is the same as the last count += 1; } } } } ////////////////////////////////////////////////////////////

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!