Users Online

· Guests Online: 34

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C++ Program to Generate Random Partition from Given Set

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.

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.91 seconds
10,819,167 unique visits