Help convert c to java Program must run exactly like the c o
Help convert c++ to java.
Program must run exactly like the c++ one. Will rate and thumb up, thanks!
----------------------------------------------
#include
using namespace std;
int main()
{
//Declare all required varaibles
int n,r;
int m,p;
int set[50];
int includedNumbers[25],excludedNumbers[25];
int subSet[50];
//Read n and r values from user
cout<<\"\ Enter n value : \";
cin>>n;
//Filling array
for(int i=0;i {
set[i] = i+1;
}
cout<<\"\ Enter r value : \";
cin>>r;
cout<<\"\ Enter m value (Included numbers count) : \";
cin>>m;
//Read included numbers
cout<<\"Enter m array values (Included number) : \ \";
for(int i=0;i {
cin>>includedNumbers[i];
}
cout<<\"\ Enter p value (Excluded numbers count) : \";
cin>>p;
//Read excluded numbers
cout<<\"Enter p array values (Included number) : \ \";
for(int i=0;i {
cin>>excludedNumbers[i];
}
cout<<\"\ All subsets are : \ \";
//Find subset of length r
for (int i = 0; i < (1< {
int subSetSize = 0;
//Finding subset
for (int j = 0; j < n; j++)
{
//Precondition to check subset
if ((i & (1 << j)) > 0)
{
subSet[subSetSize] = set[j];
subSetSize++;
}
}
//check if subset or length r
if(subSetSize==r)
{
//Check if all included number present
bool includedPresent = true;
for(int l=0;l {
int found = false;
for(int k=0;k {
if(includedNumbers[l]==subSet[k])
{
found = true;
}
}
//if found is false indicates that any of includedNumbers not present in subset
if(found==false)
{
//making includePresent to false and break the loop
includedPresent = false;
break;
}
}
//Check if all excluded number present
bool excludedNotPresent = true;
for(int l=0;l {
int found = false;
for(int k=0;k {
if(excludedNumbers[l]==subSet[k])
{
found = true;
}
}
//if found is true indicates that any of excludedNumbers present in subset
if(found==true)
{
//making excludedNotPresent to false and break loop
excludedNotPresent = false;
break;
}
}
//check if included present and excluded not present
if(includedPresent && excludedNotPresent)
{
cout<<\"{ \";
for(int k=0;k {
cout< if(k+1!=subSetSize)
{
cout<<\",\";
}
}
cout<<\"}\"< }
}
}
return 0;
}
----------------------------------------------
output:
Solution
Please follow the code and comments for description :
CODE :
import java.util.Scanner; // required imports for the code to run
public class ClassConvCPP { // class to run the code
public static void main(String[] args) { // driver method
Scanner sc = new Scanner(System.in); // scanner class to get the data from the user and the console
//Declare all required variables
int n, r;
int m, p;
int set[] = new int[50];
int includedNumbers[] = new int[25];
int excludedNumbers[] = new int[25];
int subSet[] = new int[50];
//Read n and r values from user
System.out.print(\"\ Enter n value : \");
n = sc.nextInt();
//Filling the array
for (int i = 0; i < n; i++) {
set[i] = i + 1;
}
System.out.print(\"\ Enter r value : \"); // prompt the message
r = sc.nextInt(); // get the data
System.out.print(\"\ Enter m value (Included numbers count) : \"); // prompt the message
m = sc.nextInt(); // get the data
//Read included numbers
System.out.print(\"Enter m array values (Included number) : \ \"); // prompt the message
for (int i = 0; i < m; i++) {
includedNumbers[i] = sc.nextInt(); // get the data
}
System.out.print(\"\ Enter p value (Excluded numbers count) : \"); // prompt the message
p = sc.nextInt(); // get the data
//Read excluded numbers
System.out.print(\"Enter p array values (Included number) : \ \"); // prompt the message
for (int i = 0; i < m; i++) {
excludedNumbers[i] = sc.nextInt(); // get the data
}
System.out.print(\"\ All subsets are : \ \"); // prompt the message
//Find subset of length r
for (int i = 0; i < (1 << n); i++) {
int subSetSize = 0;
//Finding subset
for (int j = 0; j < n; j++) {
//Precondition to check subset
if ((i & (1 << j)) > 0) {
subSet[subSetSize] = set[j];
subSetSize++;
}
}
//check if subset or length r
if (subSetSize == r) {
//Check if all included number present
boolean includedPresent = true;
for (int l = 0; l < m; l++) {
boolean found = false;
for (int k = 0; k < subSetSize; k++) {
if (includedNumbers[l] == subSet[k]) {
found = true;
}
}
//if found is false indicates that any of includedNumbers not present in subset
if (found == false) {
//making includePresent to false and break the loop
includedPresent = false;
break;
}
}
//Check if all excluded number present
boolean excludedNotPresent = true;
for (int l = 0; l < p; l++) {
boolean found = false;
for (int k = 0; k < subSetSize; k++) {
if (excludedNumbers[l] == subSet[k]) {
found = true;
}
}
//if found is true indicates that any of excludedNumbers present in subset
if (found == true) {
//making excludedNotPresent to false and break loop
excludedNotPresent = false;
break;
}
}
//check if included present and excluded not present
if (includedPresent && excludedNotPresent) {
System.out.print(\"{ \");
for (int k = 0; k < subSetSize; k++) {
System.out.print(subSet[k]);
if (k + 1 != subSetSize) {
System.out.print(\",\");
}
}
System.out.println(\"}\");
}
}
}
}
}
OUTPUT :
Enter n value : 10
Enter r value : 6
Enter m value (Included numbers count) : 3
Enter m array values (Included number) :
1
3
5
Enter p value (Excluded numbers count) : 3
Enter p array values (Included number) :
7
8
9
All subsets are :
{ 1,2,3,4,5,6}
{ 1,2,3,4,5,10}
{ 1,2,3,5,6,10}
{ 1,3,4,5,6,10}
Hope this is helpful.




