C++ Program to Generate Random Partition from Given Set
Posted by Superadmin on August 08 2022 07:16:43

C++ Program to Generate Random Partition from Given Set

 

 

This is a C++ program to generate random partition out of a given set of numbers or characters.

Problem Description

1. This algorithm generates a random partition of the given set of characters or integers.
2. The time complexity of this algorithm is O(n).

Problem Solution

1. This algorithm takes the input of a set of integers or characters.
2. It firstly generates the random partition of the length of the set.
3. Then starting from the beginning, it prints the number of element from the beginning, mentioned in the partition.
4. Exit.

Program/Source Code

C++ Program to generate random partition out of a given set of numbers or characters.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

#include<iostream>
#include<stdlib.h>
#include<string.h>
 
using namespace std;
 
int main()
{
	int n, i, j, l, ch;
 
	// Enter choice.
	cout<<"Enter 1 for string and 2 for integer array to generate array: ";
	cin>>ch;
 
	if(ch == 1)
	{
		char str[100];
		cout<<"Enter the string: ";
		cin>>str;
 
		n = strlen(str);
 
		cout<<"\nThe random partition of the given string is: \n";
		// Generate the random partition.
		l = 0;
		while(n > 0)
		{
			// Generate a random number from 1 to n.
			cout<<"\t{ ";
			i = rand()%n + 1;
			// Reduce the given number by i.
			n = n-i;
			// Print the first i characters from the 'l' counter.
			for(j = 0; j < i; j++)
			{
				cout<<str[l]<<" ";
				l++;
			}
 
			cout<<"}";
		}
	}
	else
	{
		cout<<"\nEnter the number of element in the integer array: ";
		cin>>n;
		int arr[n];
 
		cout<<"\nEnter the elements of the array: \n";
		for(i = 0; i < n; i++)
		{
			cout<<"Enter "<<i+1<<" element: ";
			cin>>arr[i];
		}
 
		cout<<"\nThe random partition of the given array is: \n";
		// Generate the random partition.
		l = 0;
		while(n > 0)
		{
			// Generate a random number from 1 to n.
			cout<<"\t{ ";
			i = rand()%n + 1;
			// Reduce the given number by i.
			n = n-i;
			// Print the first i numbers from the 'l' counter.
			for(j = 0; j < i; j++)
			{
				cout<<arr[l]<<" ";
				l++;
			}
			cout<<"}";
		}
	}
	return 0;
}
Program Explanation

1. Take the input of user’s choice, either 1 for processing string and 2 for an integer array.
2. If the choice is 1, then take the input of a string in str[].
3. Calculate the length of the string and assign l to ‘0’, it will traverse the string to print the characters.
4. Generate random integer partition of the length of the string.
5. For each partition ‘i’, print next ‘i’ characters from index value ‘l’.
6. Otherwise, take the input of the ‘n’ integer array.
7. Assign l to ‘0’, it will traverse the array to print the numbers.
8. Generate random integer partition of ‘n’.
9. For each partition ‘i’, print next ‘i’ integer from index value ‘l’.
10. Exit.