This is a C Program to perform partitioning at random index and generate two sets for given set of numbers or characters
Here is source code of the C Program to Generate Random Partition out of a Given Set of Numbers or Characters. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
int main(int argc, char **argv) {
int i;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
int noc = rand() % 2;
// if noc is equal to generate numbers
if (noc == 1) {
int N = 10;
int sequence[N];
printf("The Original set of numbers are:\n ");
for (i = 0; i < N; i++) {
sequence[i] = rand() % (100 - 1 + 1) + 1;
printf("%d ", sequence[i]);
}
int partition_index = rand() % 11;
printf("\nThe two sequences are: ");
printf("{ ");
for (i = 0; i < N; i++) {
if (i == partition_index)
printf(" } and { ");
printf("%d ", sequence[i]);
}
printf("}");
printf("\nPartitioning around index %d", partition_index);
}
// else generate characters
else {
int N = 10;
char sequence[N];
printf("The Original set of characters are:\n ");
for (i = 0; i < N; i++) {
sequence[i] = (char) (rand() % (123 - 97 + 97) + 97);
printf("%c", sequence[i]);
}
int partition_index = rand() % 11;
printf("\nThe two sequences are: ");
printf("{ ");
for (i = 0; i < N; i++) {
if (i == partition_index)
printf(" } and { ");
printf("%c", sequence[i]);
}
printf("}");
printf("\nPartitioning around index %c", partition_index);
}
return 0;
}
Output:
$ gcc RandomPartition.c $ ./a.out The Original set of numbers are: 52 49 7 78 82 78 30 27 51 66 The two sequences are: { 52 49 7 78 82 78 } and { 30 27 51 66 } Partitioning around index 6
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Check Computer Science Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check C Books
- Practice BCA MCQs