Question

Write a C program, allow user to input elements. Given a finite set of integers, find...

Write a C program, allow user to input elements.

Given a finite set of integers, find all the subsets with the sum of the elements greater than k.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

//function to print the subset
void getSubset(int set[], int n, int r, int idx, int element[], int i, int size)
{
   if (idx == r)
   {
   int sum = 0;
   for (int j = 0; j < r; j++)
   {
   sum = sum + element[j];
   }
if(sum>size)
{
printf("{ ");
       for (int j = 0; j < r; j++)
       {
           printf("%d ", element[j]);
       }
       printf("}");
       printf("\n");
}
       return;
   }

   if (i >= n)
       return;
   element[idx] = set[i];
   getSubset(set, n, r, idx + 1, element, i + 1, size);
   getSubset(set, n, r, idx, element, i + 1, size);
}

//function to display the subset
void displaySubset(int set[], int n, int r, int size)
{
   int element[r];
   getSubset(set, n, r, 0, element, 0, size);
}

int main()
{
//variable declaration
int setSize, k;
  
//input the set size
printf("Enter the size of the set: ");
scanf("%d", &setSize);
int set[setSize];
  
//input the set element
printf("Enter the set elements: ");
for(int i=0; i<setSize; i++)
{
scanf("%d", &set[i]);
}
  
//input the maximum sum of the elements
printf("Enter the maximum sum of the elements: ");
scanf("%d", &k);
//display the subset
   printf("The subsets are: \n");
   for(int size=1; size<setSize; size++)
   displaySubset(set, setSize, size, k);
   return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write a C program, allow user to input elements. Given a finite set of integers, find...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT