How to make a mode program in Java that checks for multiple modes?
-
The mode of a list of numbers is the number listed more ofter. Write a program that takes 10 numbers as input and displays the mode of these numbers. Your program should use parallel arrays and a manhood that takes an array of numbers as a parameter and returns the maximum value in the array. this is my code so far: public class ProjModeNEM { public static void main(String[] args) { cui reader = new cui(); int [] a = new int[10]; int [] occur = new int [10]; int length = a.length; int i = 0; int j = 0; while(i < a.length) { a[i] = reader.getInt("Enter a number to fill the value of the "+i+"th place in the array:"); i++; } for(i = 0; i < a.length; i++) { for(j = 0; j < a.length; j++) { if (a[i] == a[j]) { occur[i]++; } } } if( for(i = 0; i < occur.length; i++) { if(length != 1) { if(occur[i] == length) { System.out.println("The mode of the inputed array is "+a[i]+" with "+occur[i]+" occurences."); break; } else { length--; } } else { System.out.println("There is no mode in the array."); break; } } } } My code does check for the maximum number and will print that number and the number of times it occurs but my problem is that what if the user inputs something like this:{1,2,1,2,1,2,1,2,1,2}. The mode of that is 1 and 2. How could I change my program to account for that situation and similar ones like: {1,2,3,1,2,3,1,2,3,7}?
-
Answer:
I have no idea what the hell you are talking about when you say mode, but you could try to remove double values when you insert them into the array, and place a sentinel value (like -1) to indicate the position after the last position. If you read that value, instead of printing it abort the loop with break. Though I gotta say it's 12pm here and I'm probably thinking too complicated.
Yahoo! Answers Visit the source
Related Q & A:
- How can I make a Spinner only by java?Best solution by stackoverflow.com
- How to run a external program in PHP?Best solution by Stack Overflow
- how to create a new syntax in java?Best solution by Stack Overflow
- How to record a tv program?Best solution by Super User
- How to insert a date variable in java?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.