C Program to Generate Random Partition from Given Set

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.

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5.  
  6. int main(int argc, char **argv) {
  7.     int i;
  8.     time_t seconds;
  9.     time(&seconds);
  10.     srand((unsigned int) seconds);
  11.  
  12.     int noc = rand() % 2;
  13.     // if noc is equal to generate numbers
  14.     if (noc == 1) {
  15.         int N = 10;
  16.         int sequence[N];
  17.         printf("The Original set of numbers are:\n  ");
  18.         for (i = 0; i < N; i++) {
  19.             sequence[i] = rand() % (100 - 1 + 1) + 1;
  20.             printf("%d ", sequence[i]);
  21.         }
  22.  
  23.         int partition_index = rand() % 11;
  24.         printf("\nThe two sequences are: ");
  25.         printf("{ ");
  26.         for (i = 0; i < N; i++) {
  27.             if (i == partition_index)
  28.                 printf(" } and { ");
  29.             printf("%d ", sequence[i]);
  30.         }
  31.         printf("}");
  32.         printf("\nPartitioning around index %d", partition_index);
  33.  
  34.     }
  35.     // else generate characters
  36.     else {
  37.         int N = 10;
  38.         char sequence[N];
  39.         printf("The Original set of characters are:\n  ");
  40.         for (i = 0; i < N; i++) {
  41.             sequence[i] = (char) (rand() % (123 - 97 + 97) + 97);
  42.             printf("%c", sequence[i]);
  43.         }
  44.  
  45.         int partition_index = rand() % 11;
  46.         printf("\nThe two sequences are: ");
  47.         printf("{ ");
  48.         for (i = 0; i < N; i++) {
  49.             if (i == partition_index)
  50.                 printf(" } and { ");
  51.             printf("%c", sequence[i]);
  52.         }
  53.         printf("}");
  54.         printf("\nPartitioning around index %c", partition_index);
  55.  
  56.     }
  57.     return 0;
  58. }

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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.