I'm trying to find the mode for my program, a user inputs an amount of numbers from 0-100 as many as they want, I'm attempting to find the mode of these numbers but each time I attempt to find the mode it gives me back a 3, I've found everything else, I just need the help with the mode.
import java.util.Scanner;
public class deveation {
public static void main(String Args[]) {
Scanner kbReader = new Scanner(System.in);
int sum = 0;
int bob[] = new int[101];
int total = 0;
int a = 0;
int min = 0;
int max = 100;
int mode = 0;
boolean stay_in_loop = true;
while (stay_in_loop) {
System.out.println("Please enter interger(s) from 0-100: ");
int number = kbReader.nextInt();
if (number < 0) {
stay_in_loop = false;
}
else {
total++;
bob[number]++;
}
}
int median = total / 2 + 1;
while (median > 0) {
median -= bob[a];
a++;
}
a--;
boolean findit = true;
while (findit) {
if (bob[min] != 0)
findit = false;
else
min++;
}
boolean findme = true;
while (findme) {
if (bob[max] != 0)
findme = false;
else
max--;
}
for (int p = 0; p < 101; p++) {
if (bob[p] > mode) {
mode = bob[p];
}
for (int j = 0; j < 101; j++)
if (bob[j] <= mode)
//I don't know why I'm getting three for this
{
}
}
for (int i = 0; i < 101; i++) {
sum += bob[i] * i;
}
System.out.println(sum);
System.out.println(sum /= total);
System.out.println(a);
System.out.println(min);
System.out.println(max);
System.out.println(mode);
//You should start putting down these comments
}
}
Mode is the number(s) that is repeated most often. I would get rid of the inner for loop that you have.
for (int p = 0; p<101; p++) {
if (bob[p]>mode) {
mode=bob[p];
}
}
I am not sure why you say you are always getting three. At the end of the above loop, the mode variable will contain the largest count for a number in your bob array.
You can then loop back through the list (or store the values while looping through it) and print out the numbers that have a count that match your the value of your mode.
for (int p = 0; p < 101; p++) {
if (bob[p] == mode) {
System.out.println("Mode Number: " + p);
}
}
Remember that mode can be more than one number.
You make mode = bob[p], but bob[p] is just how many times the number appeared in your array. The mode should really be p.
For example, suppose that the bob array is:
[2, 1, 3, 1, 1, 2]
This means that 0 appears twice, 1 appears once, 2 appears three times and so on. The mode in this case is 2, which is given by the array index, and not by the value stored in the array.
To find the mode, then, we need to loop through the count array (bob), and keep two variables, the mode and the highest count until now. There is no need to loop twice or use nested loops.
int count = 0;
int mode = 0;
for (int p = 0; p < bob.length; p++) {
// If the count of the p element is greater than the greatest count until now
if (bob[p] > count) {
// Update the greatest count
count = bob[p];
// p is the new mode
mode = p;
}
}
Try using something like a hashmap where the key would the number and the value would be the number of occurences. And you can just keep track of the highest value. Also you should be able to do this in O(n) time meaning in one loop through the array. There are plenty of examples online of finding the mode.
Related
I am trying to calculate how many times two people in a group have the same birthday when given a size of the group. I am also given how many times the simulation is ran. I am trying to return the correct percentage for how many times we have two people share the same birthday out of the given amount of simulations.
I created an array first and then called a method to put the elements in a hashMap which would then show when there are two of the same values in the hashMap. However, I am not getting the correct percentage when running on Android Studio. In fact I am getting a percentage way off. I also declared a global static match variable of type int above this block.
/**
* sameBday: Create a word count mapping from an array
*/
public void sameBday(int[] valueHolder) {
Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
for(int number: valueHolder){
if(!myMap.containsKey(number)){
myMap.put(number, 1);
}
else if(myMap.containsKey(number)){
myMap.put(number, myMap.get(number) + 1);
match++;
break;
}
}
}
public double calculate(int size, int count) {
double percentage = 0.0;
int[] myArray = new int[size];
for(int i = 1; i <= count; i++){
Random r = new Random(i);
for(int j = 0; j < size; j++){
myArray[j] = r.nextInt(365) + 1;
}
sameBday(myArray);
if(i == count){
percentage = (match * (100.0/i));
}
}
return percentage;
}
Well your code is full of weird things, but that's OK we all did that. The first thing is Map, you don't need it. You can create just good old for loop and by additional check you will not compare the same person (it is i != j condition), but if you really want to do this by map you need to at the end of adding number (as key) to map check if some value of key is higher than 1, if true it's a match.
How to do something at the end of loop?
if(i == count){
percentage = (match * (100.0/i));
}
No, just do this after loop :)
//At the beginning there is int match = 0;
public void sameDayBirthday(int[] birthdays) {
for(int i = 0; i < birthdays.length; i++) {
for(int j = 0; j < birthdays.length; j++) {
if(birthdays[i] == birthdays[j] && i != j) {
match++;
return;
}
}
}
}
public double calculate(int size, int count) {
int[] birthdays = new int[size];
Random r = new Random();
for(int i = 1; i <= count; i++){ //looping through i counts (or 20 counts in this case
for(int j = 0; j < size; j++){ //looping through j times every i iteration
birthdays[j] = r.nextInt(365) + 1;
}
sameDayBirthday(birthdays);
}
return (match * (100.0/(double) count));
}
This code by calling calculate(23, 1000000) got me 50.7685% chance, for 22 persons 47.48690%
I am sorry if I offend you I didn't mean it. Leave a comment if you have questions.
I would use a HashSet and skip the sameBday function:
public double calculate(int size, int count) {
int match = 0;
Random r = new Random();
for(int i = 1; i <= count; i++){ //looping through i counts (or 20 counts in this case
Set<Integer> birthdays = new HashSet<Integer>(size);
for(int j = 0; j < size; j++){ //looping through j times every i iteration
Integer birthday = r.nextInt(365) + 1;
if (birthdays.contains(birthday)) {
match++;
break;
} else {
birthdays.add(birthday);
}
}
}
return (match * (100.0/count));
}
i'm new to this, Say if you typed 6 6 6 1 4 4 4 in the command line, my code gives the most frequent as only 6 and i need it to print out 6 and 4 and i feel that there should be another loop in my code
public class MostFrequent {
//this method creates an array that calculates the length of an integer typed and returns
//the maximum integer...
public static int freq(final int[] n) {
int maxKey = 0;
//initiates the count to zero
int maxCounts = 0;
//creates the array...
int[] counts = new int[n.length];
for (int i=0; i < n.length; i++) {
for (int j=0; j < n[i].length; j++)
counts[n[i][j]]++;
if (maxCounts < counts[n[i]]) {
maxCounts = counts[n[i]];
maxKey = n[i];
}
}
return maxKey;
}
//method mainly get the argument from the user
public static void main(String[] args) {
int len = args.length;
if (len == 0) {
//System.out.println("Usage: java MostFrequent n1 n2 n3 ...");
return;
}
int[] n = new int[len + 1];
for (int i=0; i<len; i++) {
n[i] = Integer.parseInt(args[i]);
}
System.out.println("Most frequent is "+freq(n));
}
}
Thanks...enter code here
Though this may not be a complete solution, it's a suggestion. If you want to return more than one value, your method should return an array, or better yet, an ArrayList (because you don't know how many frequent numbers there will be). In the method, you can add to the list every number that is the most frequest.
public static ArrayList<Integer> freq(final int[] n) {
ArrayList<Integer> list = new ArrayList<>();
...
if (something)
list.add(thatMostFrequentNumber)
return list;
}
The solutions looks like this:
// To use count sort the length of the array need to be at least as
// large as the maximum number in the list.
int[] counts = new int[MAX_NUM];
for (int i=0; i < n.length; i++)
counts[n[i]]++;
// If your need more than one value return a collection
ArrayList<Integer> mf = new ArrayList<Integer>();
int max = 0;
for (int i = 0; i < MAX_NUM; i++)
if (counts[i] > max)
max = counts[i];
for (int i = 0; i < MAX_NUM; i++)
if (counts[i] == max)
mf.add(i);
return mf;
Well you can just do this easily by using the HashTable class.
Part 1. Figure out the frequency of each number.
You can do this by either a HashTable or just a simple array if your numbers are whole numbrs and have a decent enough upper limit.
Part 2. Find duplicate frequencies.
You can just do a simple for loop to figure out which numbers are repeated more than once and then print them accordingly. This wont necessarily give them to you in order though so you can store the information in the first pass and then print it out accordingly. You can use a HashTable<Integer,ArrayList<Integer> for this. Use the key to store frequency and the ArrayList to store the numbers that fall within that frequency.
You can maintain a "max" here while inserting into our HashTable if you only want to print out only the things with most frequency.
Here is a different way to handle this. First you sort the list, then loop through and keep track of the largest numbers:
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int n[] = { 6, 4, 6, 4, 6, 4, 1 };
List<Integer> maxNums = new ArrayList<Integer>();
int max = Integer.MIN_VALUE;
Integer lastValue = null;
int currentCount = 0;
Arrays.sort(n);
for( int i : n ){
if( lastValue == null || i != lastValue ){
if( currentCount == max ){
maxNums.add(lastValue);
}
else if( currentCount > max ){
maxNums.clear();
maxNums.add(lastValue);
max = currentCount;
}
lastValue = i;
currentCount = 1;
}
else {
currentCount++;
}
System.out.println("i=" + i + ", currentCount=" + currentCount);
}
if( currentCount == max ){
maxNums.add(lastValue);
}
else if( currentCount >= max ){
maxNums.clear();
maxNums.add(lastValue);
}
System.out.println(maxNums);
}
}
You can try it at: http://ideone.com/UbmoZ5
A run is a sequence of adjacent repeated values . Write a program that generates a sequence of random die tosses and that prints the die values, marking only the longest run. The program should take as input the total number of die tosses (ex 10), then print:
1 6 6 3 (2 2 2 2 2) 5 2
Im quite confused on how to compare each number in order to get the correct output. Maybe using an array to store the values. Any answers or input will be of help thank you!
import java.util.Random;
import java.util.Scanner;
public class Dice
{
Random generator = new Random();
Scanner keyboard = new Scanner(System.in);
public void DiceCount()
{
int count;
int sides = 6;
int number;
System.out.println("How many die? ");
count = keyboard.nextInt();
for(int i=0; i < count; i++)
{
number = generator.nextInt(sides);
System.out.print(number);
}
}
}
First, replace int number; with int[] numbers = new int[count];. Next, replace number = ... with numbers[i] = ....
This will give you an array of random numbers (don't print them yet!). As you generate your numbers, note how many equal numbers you get in a row (add a special counter for that). Also add variable that stores the length of the longest run so far. Every time you get a number that's equal to the prior number, increment the counter; otherwise, compare the counter to the max, change the max if necessary, and set the counter to 1. When you update the max, mark the position where the run starts (you can tell from the current position and the length of the run).
Now it's time to detect the longest run: go through the numbers array, and put an opening parenthesis where the run starts. Put a closing parenthesis when you reach the end of the run, and finish the printing to complete the output for the assignment.
import java.util.Random;
import java.util.Scanner;
public class Dice {
Random generator = new Random();
Scanner keyboard = new Scanner(System.in);
public void DiceCount() {
int sides = 6;
System.out.println("How many die? ");
int count = keyboard.nextInt();
int[] array = new int[count];
int longestLength = 1, currentLength = 1, longestLengthIndex = 0, currentLengthIndex = 1;
int currentNum = -1;
for (int i = 0; i < count; i++) {
array[i] = generator.nextInt(sides);
System.out.print(array[i] + " ");
if (currentNum == array[i]) {
currentLength++;
if (currentLength > longestLength) {
longestLengthIndex = currentLengthIndex;
longestLength = currentLength;
}
} else {
currentLength = 1;
currentLengthIndex = i;
}
currentNum = array[i];
}
System.out.println();
for (int i = 0; i < count; i++)
System.out.print((i == longestLengthIndex ? "(" : "") + array[i] + (i == (longestLengthIndex + longestLength - 1) ? ") " : " "));
}
}
Note: this will only take the first longest range. So if you have 1123335666 it will do 112(333)5666.
If you need 112(333)5(666) or 1123335(666) then I leave that to you. It's very trivial.
import java.util.Random;
public class Dice {
public static void main(String[] args) {
//make rolls
Random rand = new Random();
int[] array = new int[20];
int longestRun = 1;
int currentRun = 1;
int longestRunStart = 0;
int currentRunStart = 1;
System.out.print("Generated array: \n");
for (int i = 0; i < array.length; i++) {
array[i] = rand.nextInt(6); //add random number
System.out.print(array[i] + " "); //print array
if (i != 0 && array[i - 1] == array[i]) {
//if new number equals last number...
currentRun++; //record current run
if (currentRun > longestRun) {
longestRunStart = currentRunStart; //set index to newest run
longestRun = currentRun; //set above record to current run
}
} else {
//if new number is different from the last number...
currentRun = 1; //reset the current run length
currentRunStart = i; //reset the current run start index
}
}
//record results
System.out.print("\nIdentifying longest run: \n");
for (int i = 0; i < longestRunStart; i++) { System.out.print(array[i] + " "); } //prints all numbers leading up to the run
System.out.print("( "); //start parentheses
for (int i = longestRunStart; i < (longestRunStart + longestRun); i++) { System.out.print(array[i] + " "); } //prints the run itself
System.out.print(") "); //end parentheses
for (int i = (longestRunStart + longestRun); i < 20; i++) { System.out.print(array[i] + " "); } //all remaining numbers
}
}```
I'm trying to write a java method which finds all the modes in an array. I know there is a simple method to find the mode in an array but when there are more than one single mode my method outputs only one of them. I've tried to find a way but am nit sure how to approach this problem. Can anyone help me out to find all the modes in the array? Thanks.
Yes here is my code which outputs only one mode even if multiple modes exist.
public static int mode(int a[]){
int maxValue=0, maxCount=0;
for (int i = 0; i < a.length; ++i){
int count = 0;
for (int j = 0; j < a.length; ++j){
if (a[j] == a[i]) ++count;
}
if (count > maxCount){
maxCount = count;
maxValue = a[i];
}
}
return maxValue;
}
okay here's an example:
30
30
30
34
34
23
In this set of numbers there is only one mode, which is 30.
30
30
30
34
34
34
23
But in this set there are two modes, 30 and 34. I want my code to be able to output both of them, whereas it only prints one. It prints only 30.
The following code will return you an Integer[] containing the modes. If you need an int[] instead, you still need to convert the Integer instances to ints manually. Probably not the most efficient version, but its matches closely to your code
public static Integer[] mode(int a[]){
List<Integer> modes = new ArrayList<Integer>( );
int maxCount=0;
for (int i = 0; i < a.length; ++i){
int count = 0;
for (int j = 0; j < a.length; ++j){
if (a[j] == a[i]) ++count;
}
if (count > maxCount){
maxCount = count;
modes.clear();
modes.add( a[i] );
} else if ( count == maxCount ){
modes.add( a[i] );
}
}
return modes.toArray( new Integer[modes.size()] );
}
After a long night of programming, I finality got a program that will print out the mode/modes of an array. Or will even tell you if there isn't a mode (say, if no input occurred more than once or all the inputs occurred the same amount of times: ex. 1, 1, 2, 2, 3, 3, 4, 4). Some limitations of the program are that you must enter more than one number, and you cannot enter more than 10000 numbers or a negative number (if you wanted to enter a negative number, you would just have to tweak all the for loops involving the values[][] array. Some cool things about my program are that it prints out how many times each of you inputs occurred along with the mode of your array. And all of the print outs grammar change according to the amount of info (Ex. The mode of your array is 2; The modes of your array are 1 & 2; The modes of your array are 0, 2, 5, & 8). There is also a bubble sort function example in the program for anyone who thought that they needed a sorter function in their mode program. Hope this helps, I included a lot of pseudo code to help anyone who doesn't see how my logic progress throughout the program. (FYI: It is java, and was compiled in BlueJ)
import java.util.Scanner;
public class Mode
{
public static void main (String args [])
{
Scanner scan = new Scanner(System.in);
int MAX_INPUTS = 10000; boolean flag = false;
System.out.print ("Input the size of your array: ");
int size; // How many nubers will be in the user array
do
{
size = scan.nextInt();
if (size == 1)
{
System.out.print ("\nError. You must enter a number more than 1.\n\n");
continue;
}
else if (size > MAX_INPUTS || size < 0)
{
System.out.print ("\nError. You muste enter a number less than " + MAX_INPUTS + " or greater than 0\n\n");
continue;
}
else
flag = true; // a ligit answer has been entered.
}
while (flag != true);
int array[] = new int[size], values[][] = new int[2][MAX_INPUTS + 1], ticks = 0;
System.out.print ("\nNow input the numbers for your array.\n\n");
/* Taking inputs from the user */
while (ticks < size)
{
System.out.print ("Number " + (ticks + 1) + ": ");
array[ticks] = scan.nextInt();
if (array[ticks] > MAX_INPUTS || array[ticks] < 0)
{
System.out.print ("\nError. Number cannot be greater than " + MAX_INPUTS + " or less than 0\n\n");
continue;
}
++ticks;
}
/*
* values[][] array will hold the info for how many times numbers 0 - 10000 appear in array[]. Column 0 will hold numbers from 0 -1000, and column 1 will hold the number of
* of repititions the number in column 0 occured in the users inputed array.
*/
for (int i = 0; i < MAX_INPUTS; ++i) // Initalize Column zero with numbers starting at zeor, and ending and MAX_INPUTS.
values[0][i] = i;
for (int i = 0; i < size; ++i) // Find the repatitions of the numbers in array[] that correspond to the number in column zere of values[][].
for (int j = 0; j < MAX_INPUTS; ++j)
if (array[i] == j)
++values[1][j];
sort (array, size);
System.out.print ("\n\nHere are the numbers you entered.\n\n"); // show the values the user entered in ascending order.
for (int i = 0; i < size; ++i)
{
if (i == size - 1) // the last inputed number
System.out.print (array[i]); // don't allow an extra comma.
else
System.out.print (array[i] + ", ");
}
// Show the user how many times each of the values he/she entered occured.
System.out.print ("\n\nThis is the amount of times each of the values you entered occured:\n");
for (int i = 0; i < MAX_INPUTS; ++i)
{
if (values[1][i] == 1)
System.out.print (i + " was entered " + values[1][i] + " time\n"); // avoid: 2 was entered 1 times
else if (values[1][i] != 0)
System.out.print (i + " was entered " + values[1][i] + " times\n"); // avoid: 2 was entered 2 time
}
/* -------------------------------------------------------------------- | Finding the Mode/Modes | -------------------------------------------------------------------- */
/* The process begins with creating a second array that is the exactly the same as the values[][] (First for loop). Then I sort the duplicate[] array to find the mode
* (highest number in the duplicate[]/values[][] arrays. Int max is then assigned the highest number. Remembering that the values[][] array: column 0 contains numbers ranging
* from 1 to 10000, it keeps track of where the numbers in column were originally located, in which you can compare to the duplicate array which is sorted. Then I can set
* up a flag that tells you whether there is more than one mode. If so, the printing of these modes will look neater and the grammar can be changed accordingly.
*/
int duplicate[] = new int [10001], mode[] = new int [size], max, mode_counter = 0;
boolean multi_mode = false, all_same;
for (int i = 0; i < MAX_INPUTS; ++i)
duplicate[i] = values[1][i]; // copy values array.
sort (duplicate, MAX_INPUTS);
max = duplicate[MAX_INPUTS - 1]; // the last number in the sorted array is the greatest.
all_same = test (duplicate, MAX_INPUTS, size, max); // this is the test to see if all the numbers in the user array occured the same amount of times.
int c = 0; // a counter
/* The mode of the user inputed array will be recorded in the values array. The sort of the duplicate array told me what was the higest number in that array. Now I can
* see where that highest number used to be in the original values array and recored the corresponding number in the column zero, which was only filled with numbers 0 -
* 10000. Thus telling me the mode/modes.
*/
for (int i = 0; i < MAX_INPUTS; ++i)
{
if (values[1][i] == max)
{
mode[c++] = values[0][i];
++mode_counter;
}
}
if (mode[1] != 0) //mode[0] (the first cell, has a number stored from the last for loop. If the second cell has a number other than zero, that tells me there is more than 1 mode.
multi_mode = true;
if (multi_mode == false)
System.out.print ("\nThe mode of your array is " + mode[0]); // For correct grammer.
else if (all_same == true)
System.out.print ("\nAll of the numbers entered appeared the same amount of times. "); // See the boolean function for more details
else // If here there is more than one mode.
{
System.out.print ("\nThe modes of yoru array are ");
for (int i = 0; i < mode_counter; ++i)
{
if (mode_counter > 2 && i == (mode_counter - 1)) // If there is more than two modes and the final mode is to be printed.
System.out.print ("& " + mode[i]);
else if (mode_counter == 2)
{ // This is true if there is two modes. The else clause will print the first number, and this will print the amper sign and the second mode.
System.out.print (mode[0] + " & " + mode[1]);
break;
}
else
System.out.print (mode[i] + ", ");
}
}
}
public static void sort (int list[], int max) // Its the bubble sort if you're wondering.
{
int place, count, temp;
for (place = 0; place < max; ++place)
for (count = max - 1; count > place; --count)
if (list[count - 1] > list[count])
{
temp = list[count-1];
list[count - 1] = list[count];
list[count] = temp;
}
}
/* The test to see if there isn't a mode. If the amount of the mode number is the same as the amount of numbers there are in the array is true, or if the size entered by the
* user (input) modulo the mode value is equal to zero (say, all the numbers in an array of ten were entered twice: 1, 1, 2, 2, 3, 3, 4, 4, 5, 5). */
public static boolean test (int list[], int limit, int input, int max)
{
int counter = 0, anti_counter = 0;
for (int i = 0; i < limit; ++i)
if (list[i] == max)
++counter; // count the potential modes
else if (list[i] !=0 && list[i] != max)
++anti_counter; // count every thing else except zeros.
if (counter == input || (input % max == 0 && anti_counter == 0) )
return true;
else
return false;
}
}
Even though this probably isn't the most efficient solution, it will print all the highest modes:
public static int mode(int a[]) {
int maxValue=-1, maxCount=0;
for (int i = 0; i < a.length; i++) {
int count = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] == a[i])
count++;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}
//loop again and print only the highest modes
for (int i = 0; i < a.length; i++) {
int count = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] == a[i])
count++;
}
}
if (count == maxCount) {
System.out.println(a[i]);
}
}
}
Hope this helps. This is my answer. Hope it helps
public List mode(double[] m) {
HashMap<Double, Double> freqs = new HashMap<Double, Double>();
for (double d : m) {
Double freq = freqs.get(d);
freqs.put(d, (freq == null ? 1 : freq + 1));
}
List<Double> mode = new ArrayList<Double>();
List<Double> frequency = new ArrayList<Double>();
List<Double> values = new ArrayList<Double>();
for (Map.Entry<Double, Double> entry : freqs.entrySet()) {
frequency.add(entry.getValue());
values.add(entry.getKey());
}
double max = Collections.max(frequency);
for(int i=0; i< frequency.size();i++)
{
double val =frequency.get(i);
if(max == val )
{
mode.add(values.get(i));
}
}
return mode;
}
Handling multiple mode values:
Here initially I am taking a map to get the frequencies of each element. After finding the max repeating value I am storing its map value.
While iterating to the map I am checking if this value is repeating for some key and is not 1, if it is then I am adding that key as well. This will get me the multiple modes
public static void mode(int a[]){
int count=0;
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<a.length;i++)
{
map.put(a[i],map.getOrDefault(a[i],0)+1);
}
Set<Integer> set=new HashSet<>();
int maxValue=0;
for(int i=0;i<a.length;i++)
{
int ct=0;
for(int j=0;j<a.length;j++)
{
if(a[i]==a[j])
ct++;
}
if(ct>count)
{
count=ct;
maxValue=a[i];
}
}
set.add(maxValue);
int k=map.get(maxValue);
if(k!=1)
{
for(Map.Entry m:map.entrySet())
{
if((int)m.getValue()==k)
set.add((int)m.getKey());
}
}
System.out.println("Mode: "+set);
}
I am trying to write a program that generates all the subsets of an entered set in java. I think i nearly have it working.
I have to use arrays (not data structures)
The entered array will never be greater than 20
Right now when i run my code this is what i get:
Please enter the size of A: 3
Please enter A: 1 2 3
Please enter the number N: 3
Subsets:
{ }
{ 1 }
{ 1 2 }
{ 1 2 3 }
{ 2 3 }
{ 2 3 }
{ 2 }
{ 1 2 }
this is the correct number of subsets (2^size) but as you can see it prints a few duplicates and not some of the subsets.
Any ideas where I am going wrong in my code?
import java.util.Scanner;
public class subSetGenerator
{
// Fill an array with 0's and 1's
public static int [] fillArray(int [] set, int size)
{
int[] answer;
answer = new int[20];
// Initialize all elements to 1
for (int i = 0; i < answer.length; i++)
answer[i] = 1;
for (int a = 0; a < set.length; a++)
if (set[a] > 0)
answer[a] = 0;
return answer;
} // end fill array
// Generate a mask
public static void maskMaker(int [] binarySet, int [] set, int n, int size)
{
int carry;
int count = 0;
boolean done = false;
if (binarySet[0] == 0)
carry = 0;
else
carry = 1;
int answer = (int) Math.pow(2, size);
for (int i = 0; i < answer - 1; i++)
{
if (count == answer - 1)
{
done = true;
break;
}
if (i == size)
i = 0;
if (binarySet[i] == 1 && carry == 1)
{
binarySet[i] = 0;
carry = 0;
count++;
} // end if
else
{
binarySet[i] = 1;
carry = 1;
count++;
//break;
} // end else
//print the set
System.out.print("{ ");
for (int k = 0; k < size; k++)
if (binarySet[k] == 1)
System.out.print(set[k] + " ");
System.out.println("}");
} // end for
} // maskMaker
public static void main (String args [])
{
Scanner scan = new Scanner(System.in);
int[] set;
set = new int[20];
int size = 0;
int n = 0;
// take input for A and B set
System.out.print("Please enter the size of A: ");
size = scan.nextInt();
if (size > 0)
{
System.out.print("Please enter A: ");
for (int i = 0; i < size; i++)
set[i] = scan.nextInt();
} // end if
System.out.print("Please enter the number N: ");
n = scan.nextInt();
//System.out.println("Subsets with sum " + n + ": ");
System.out.println("Subsets: ");
System.out.println("{ }");
maskMaker(fillArray(set, size), set, n, size);
} // end main
} // end class
The value of i always goes from 0 to N-1 and then back to 0. This is not useful to generate every binary mask you need only one time. If you think about it, you need to move i only when you have generate all possible masks up to i-1.
There is a much easier way to do this if you remember every number is already internally represented in binary in the computer and everytime you increment it Java is doing the adding and carrying by itself. Look for bitwise operators.