MEMBERS OF POWER SET

package powerset;

import java.util.Scanner;

public class Powerset {

    //Function to increment binary string...

    static String incr_bin (String binary){

        char bin[] = new char[100];

        int size_bin, i;

        size_bin = binary.length();

        bin = binary.toCharArray();

        bin[size_bin-1]++;

        for(i=size_bin-1; i>=0; i--){

            if (i != 0){

                if(bin[i] > '1'){

                    bin[i]='0';

                    bin[i-1]++;

                }

            }

        }

        if (bin[0]>'1'){

            for(i=0;i<size_bin;i++){

                bin[i]='0';

            }

        }

        binary = new String (bin);

        return binary;

    }

    public static void main(String[] args) {

        //Declarations

        Scanner in = new Scanner (System.in);

        int a[] = new int [100];

        int size_a, i, count=0;

        String binary;

        //Input

        System.out.println("Enter the number of elements in A : ");

        size_a = in.nextInt();

        char bin[] = new char [size_a];

        System.out.println("Enter the elements in A : ");

        for(i=0; i<size_a; i++){

            a[i] = in.nextInt();

            bin[i] = '0';

        }

        binary = new String(bin);

        //Calculating and Setting up subsets

        System.out.println("MEMBERS OF POWER SET :");

        do{

            System.out.print("\n{.");

            count = 0;

            binary = incr_bin(binary);

            bin = binary.toCharArray();

            for(i=0; i<size_a; i++){

                if (bin[i] == '0') count++;

                if (bin[i] == '1') System.out.print(a[i] + "  ");

            }

            System.out.println("}");

        }while(count!=size_a);    

    }

}
MEMBERS OF POWER SET MEMBERS OF POWER SET Reviewed by Unknown on 03:42 Rating: 5

No comments:

Powered by Blogger.