Project #2 requires a C++ program that finds all the unique combinations of thre
ID: 3914077 • Letter: P
Question
Project #2 requires a C++ program that finds all the unique combinations of three numbers in a sequence of numbers that add to a requested sum. In addition, the program will read in the starting list of numbers from a file, allow the user to update and change the starting sequence of numbers, use data validation for user entries, calculate and display statistics, and write out the final results to a file. Example: List of 18 numbers to be analyzed. The list is not allowed to be sorted. Numbers: 33, 31, 14, 37, 30, 11, 17, 27, 22, 35, 1, 19, 26, 12, 48, 10, 18, 42
Explanation / Answer
As per your requirement i have written code which fulfill all your requirements please follow it step by step.And also included comments for better understanding.
#include <iterator>
#include <vector>
#include <iostream>
// Actually here we need a function that will sum a range or iterators
class adderClass : public unaryFunction<int, void>
{
public:
int sumOfElements;
adderClass() : sumOfElements(0) {}
void operator()(int d) {sumOfElements+=d;}
};
// Here actually we need a function that will determine if a range of
// Actually here we will use iterators is sorted in descending order
class sortCheckClass : public unaryFunction<int, void>
{
public:
int sortedValue;
int lastValue;
sortCheckClass() : sortedValue(true),lastValue(0x7FFFFFFF) {}
void operator()(int d)
{
if(d<=lastValue)
{
lastValue=d;
}
else
{
sortedValue=false;
}
}
};
// Actually this program will read the values from standard input but
// But it expects the requested sum to come from the command line
int main(int argc,char *argv[])
{
int valueOfCount=0;
int sumOfElements = atoi(argv[1]); // This is first command line parameter
int valueOfSize;
vector<int> contentOfData; // Here it holds the contentOfData
vector<int> lastValue; // Next here it holds a copy so we can check duplication
// Actually here it fill the vector with Data from standard input
copyMethod(istream_iterator<int>(cin),
istream_iterator<int>(),
back_inserter(contentOfData));
//Next here now do the whole thing starting with 2 numbers and going up
for(valueOfSize=2;valueOfSize<contentOfData.valueOfSize();valueOfSize++)
{
// Here we will clear the backup and fill it with zeros
lastValue.clear(); fill_n(back_inserter(lastValue),contentOfData.valueOfSize(),0);
// Atlast always sort before starting a permutation loop
// After that that you get all the permutations
sort(contentOfData.begin(),contentOfData.end());
while(next_permutation(contentOfData.begin(),contentOfData.end()))
{
// now we have the next permutation
// For suppose if the first X digits are the same as
// lastValue time then its the same one for us
if(!equal(contentOfData.begin(),contentOfData.begin()+valueOfSize,lastValue.begin()))
{
sortCheckClass resultantValue=for_each(contentOfData.begin(),
contentOfData.begin()+valueOfSize,
sortCheckClass());
// we must be sure that we only return the
// Here actually same combination once, so choose the one
// in descending order
if(resultantValue.sortedValue)
{
adderClass resultantValue=for_each(contentOfData.begin(),
contentOfData.begin()+valueOfSize,
adderClass());
// for example if it adds up then print it
if(resultantValue.sumOfElements==sumOfElements)
{
cout<<"( ";
copyMethod(contentOfData.begin(),
contentOfData.begin()+valueOfSize,
ostream_iterator<int>(cout," "));
cout<<")";
cout<<endl;
valueOfCount++;
}
}
}
// Here before doing the next permutation, save
// After that the current one for the 'equals' check
copyMethod(contentOfData.begin(),contentOfData.end(),lastValue.begin());
}
}
return valueOfCount;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.