Question

Write a program that will read a list of numbers and a desired sum, then determine...

Write a program that will read a list of numbers and a desired sum, then determine the subset of numbers in the list that yield that sum if such a subset exists.

Answer from Data Structures (2nd Edition) Chapter 5.6, Problem 6PP: Does not fully answer the question. Specifically, ". . . then determine the subset of numbers in the list that yield that sum if such a subset exists" is not answered in provided solution. It should show the actual subsets that can be made to add up to the sum.

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

Code -


#include <bits/stdc++.h>
using namespace std;

bool** dp;

void displaySubset(const vector<int>& v)
{
   for (int i = 0; i < v.size(); ++i)
       printf("%d ", v[i]);
   printf("\n");
}

//it is a recursive function to print the subset
void printSubset(int list[], int i, int sum, vector<int>& p)
{
//if we reach end and sum is nonzero and we will print only of the list[0] is equal to sum
   if (i == 0 && sum != 0 && dp[0][sum])
   {
       p.push_back(list[i]);
       displaySubset(p);
       return;
   }

   //condition to check sum is zero
   if (i == 0 && sum == 0)
   {
       displaySubset(p);
       return;
   }

   //sum can be achieved by ignoring the current element
   if (dp[i-1][sum])
   {
       //create a new vector
       vector<int> b = p;
       printSubset(list, i-1, sum, b);
   }

   //sum can be achieved by considrring current element
   if (sum >= list[i] && dp[i-1][sum-list[i]])
   {
       p.push_back(list[i]);
       printSubset(list, i-1, sum-list[i], p);
   }
}
//function print all the subset
void printAllSubset(int list[], int n, int sum)
{
   if (n == 0 || sum < 0)
   return;

   dp = new bool*[n];
   for (int i=0; i<n; ++i)
   {
       dp[i] = new bool[sum + 1];
       dp[i][0] = true;
   }

   // Sum list[0] can be achieved with single element
   if (list[0] <= sum)
   dp[0][list[0]] = true;

   //fill the rest of entries in dp
   for (int i = 1; i < n; ++i)
       for (int j = 0; j < sum + 1; ++j)
           dp[i][j] = (list[i] <= j) ? dp[i-1][j] ||
                                   dp[i-1][j-list[i]]
                                   : dp[i - 1][j];
   if (dp[n-1][sum] == false)
   {
       printf("There are no subsets with sum %d\n", sum);
       return;
   }

   vector<int> p;
   printSubset(list, n-1, sum, p);
}

// Driver code
int main()
{
   int list[] = {1, 2, 3, 4, 5,6,7,8,9};
   int n = sizeof(list)/sizeof(list[0]);
   int sum = 15;
cout<<"For sum " <<sum<<" subsets are "<<endl;
   printAllSubset(list, n, sum);
   return 0;
}

Screenshots-

Add a comment
Know the answer?
Add Answer to:
Write a program that will read a list of numbers and a desired sum, then determine...
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
  • Write a program that will read a list of numbers and a desired sum, then determine the subset of ...

    Write a program that will read a list of numbers and a desired sum, then determine the subset of numbers in the list that yield that sum if such a subset exists. Answer from Data Structures (2nd Edition) Chapter 5.6, Problem 6PP: Does not fully answer the question. Specifically, ". . . then determine the subset of numbers in the list that yield that sum if such a subset exists" is not answered in provided solution. It should show the...

  • Write a program that will read a list of numbers and a desired sum, then determine...

    Write a program that will read a list of numbers and a desired sum, then determine the subset of numbers in the list that yield that sum if such a subset exists. Answer from Data Structures (2nd Edition) Using JAVA Chapter 5.6, Problem 6PP: Does not fully answer the question. Specifically, ". . . then determine the subset of numbers in the list that yield that sum if such a subset exists" is not answered in provided solution. It should...

  • Adding Two Numbers) Write a program that defines macro SUM with two arguments, x and y,...

    Adding Two Numbers) Write a program that defines macro SUM with two arguments, x and y, and use SUM to produce the following output: the sum of x and y is 13 Please type answer out fully

  • Create a program that will determine the even number(s) from a list of numbers. Your program...

    Create a program that will determine the even number(s) from a list of numbers. Your program should create and call a function FindEven that accepts a list of numbers and returns a list of only the even numbers in sorted order. Note that the original list is given in the starter code. def body(): numberlist = [1, 1000, 5, -3, 2, 16 # Write your code here and notice level # Do NOT include: if __name__ == been provided for...

  • Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load...

    Write a C PROGRAM that will read in 10 floating-point numbers from the keyboard and load them into an array. Add up all the values and store the answer in a variable. Find the largest and smallest number in the array. Put each into its own variable. Print to the screen the sum, largest value, and smallest value. Copy the array to a second array in reverse order. Print out the second array. Read in 20 integer numbers, all in...

  • Write a C++ program to read an unknown number of integer values and find the sum...

    Write a C++ program to read an unknown number of integer values and find the sum of only negative integers. Use variable n to store the user input. Hint: Check Example6E.cpp (Lecture 6) Note: Use the "Check" button to verify your answer. You can modify your code and check until you get it right. For example: Test Input Result -11 A list of integers -10 12 1 14 -1

  • please use C++ write a program to read a textfile containing a list of books. each...

    please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...

  • Lab 1 Q1. Write a Python program with the following function building block. •All input values...

    Lab 1 Q1. Write a Python program with the following function building block. •All input values are to be taken fro m the user. •Ensure to check for possible error cases like mismatch of data type and input out of range, for every function.       Function 1: countVowels(sentence) – Function that returns the count of vowels in a sentence. Function 2: Sum of all even numbers between two numbers, identify number of parameters   Q2. Write a Python program that reads a...

  • C++ Write a program with the following elements: in main() -opens the 2 files provided for...

    C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...

  • In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, t...

    In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, then present a menu to the user, and at the end print a final report shown below. You may(should) use the structures you developed for the previous assignment to make it easier to complete this assignment, but it is not required. Required Menu Operations are: Read Students’ data from a file to update the list (refer to sample...

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
ADVERTISEMENT