_Jenny

Untitled

Sep 18th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.84 KB | Source Code | 0 0
  1. #include <stdio.h>
  2.  
  3. void printSubsequences(int arr[], int n, int index, int subsequence[], int length) {
  4.     if (index == n) {
  5.         // print the subsequence
  6.        
  7.         for (int i = 0; i < length; i++) {
  8.             printf("%d ", subsequence[i]);
  9.         }
  10.         printf("\n");
  11.         return;
  12.     }
  13.    
  14.     // include the current element in the subsequence
  15.     subsequence[length] = arr[index];
  16.     printSubsequences(arr, n, index + 1, subsequence, length + 1);
  17.    
  18.     // exclude the current element from the subsequence
  19.     printSubsequences(arr, n, index + 1, subsequence, length);
  20. }
  21.  
  22. int main() {
  23.     int arr[] = {1, 0, 3};
  24.     int n = sizeof(arr) / sizeof(arr[0]);
  25.     int subsequence[n];
  26.    
  27.     printf("All subsequences of the set are:\n");
  28.     printSubsequences(arr, n, 0, subsequence, 0);
  29.     return 0;
  30. }
  31.  
  32.  
Advertisement
Add Comment
Please, Sign In to add comment