The assignment is as follows:
Write a program that would generate and print all r-subsets of a set S. The set S and the number r are provided by the user. Your program should also print the number of r-subsets of S. Subsets of a set can be generated using a mask. A good description of the method is presented at http://compprog.wordpress.com/2007/10/10/generating-subsets/. The page also contains a link to the code in C (sub.c) Please use a mask to generate all subsets of S and print the subsets that have r elements (r-subsets). Your program should do the following:
- Prompt the user to enter the size of set S, elements of S, and the number r. You may assume that the size of S is no more than 10. Assume that all the elements entered by the user are different (no duplicates). (You do not need to check that there are no duplicates.)
- Generate all subsets of S using a mask.
- For each subset generated in 2), compute the number of elements in the subset. If the number of elements in the subset is equal to r then output the subset.
- Output total number of r-subsets of S. Dialog with the user may look like the following (keep in mind that the order of elements in a set does not matter):
Please enter S: 1 2 3 4 5 6
Please enter r: 4
4-subsets of S are the following:
{ 1 2 3 4 }
{ 1 2 3 5 }
{ 1 2 4 5 }
{ 1 3 4 5 }
{ 2 3 4 5 }
{ 1 2 3 6 }
{ 1 2 4 6 }
{ 1 3 4 6 }
{ 2 3 4 6 }
{ 1 2 5 6 }
{ 1 3 5 6 }
{ 2 3 5 6 }
{ 1 4 5 6 }
{ 2 4 5 6 }
{ 3 4 5 6 }
There are 15 r-subsets
The instructor never talked about "masks" in class, so naturally I was confused about this. I tried to Google to figure it out, but to no avail. So, I decided to just try solving the problem in general. This is my code so far:
import java.util.*;
import java.util.Scanner;
public class Main {
public static void func(int[] ARR, int s, int R) {
for (int j = i + 1; j <= R; j++) {
System.out.println(ARR[i] + " " + ARR[j]);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Size of set S: ");
int S = input.nextInt();
int[] arr = new int[S];
System.out.print("Values of set S: ");
for (int i = 0; i < S; i++) {
arr[i] = input.nextInt();
}
System.out.print("Value of r: ");
int r = input.nextInt();
func(arr, S, r);
}
}
How do I go about making this code work/creating this "mask" thing that the instructor is asking us to implement?