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.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Watch Advanced C Programming Videos
- Apply for C Internship
- Practice Computer Science MCQs
- Buy Computer Science Books
- Apply for Computer Science Internship