Wednesday, 22 November 2017

C++ Program to Generate a Random Subset by Coin Flipping


Code:

#include   iostream
#include   stdlib.h

using namespace std;

int main()
{
int i, n;
cout<<"\nEnter the number of element array have: ";
cin>>n;

int arr[n];
cout<<"\n";

// Take the input of the array.
for(i = 0; i < n; i++)
{
cout<<"Enter "<
cin>>arr[i];
}

// Print the subset using .
cout<<"\nThe random subset of the given set is: \n\t{ ";
for(i = 0; i < n; i++)
{
// Randomly generates 0 or 1 as coin flipping and print the array element if it is 1.
if(rand()%2 == 1)
cout<
}
cout<<"}";

return 0;
}


Output:

Case 1:
Enter the number of element array have: 5

Enter 1 element: 1
Enter 2 element: 2
Enter 3 element: 3
Enter 4 element: 4
Enter 5 element: 5

The random subset of the given set is:
        { 1 2 5 }
Case 2:
Enter the number of element array have: 7

Enter 1 element: 1
Enter 2 element: 5
Enter 3 element: 8
Enter 4 element: 2
Enter 5 element: 3
Enter 6 element: 4
Enter 7 element: 9

The random subset of the given set is:
        { 1 5 3 }



More C++ Programs:












100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...