Set value by default in first position in index array - c++

Im trying set an array to have always same value in first position, but idk how to do that. for example array[10] always array[0] = 100, then continue add ohters number like: array[100,1,2,3.....], loop array[100,1,2,3.....] etc.
int main() {
int arrayNumber[10];
while (true)
{
for (int i = 0; i < 10; i++)
{
arrayNumber[0] = 100;
printf("%d\n", arrayNumber[i]);
Sleep(100);
}
}
}

Set the first value outside the loop and start the loop at 1.
arrayNumber[0] = 100;
for (int i = 1; i < arraysize; i++)
{
arrayNumber[i] = i;
}

int main() {
int arrayNumber[10] = {100};
for (int i = 1; i < 10; i++) {
arrayNumber[i] = i;
}
}
The first operator above declares the array and initializes the first it's element with the value 100, then the loop fills other elements with 1, 2, 3, ..., 9.
Since your asked about C++ let introduce C++-like solution below.
#include <numeric>
int main() {
int arrayNumber[10] = {100};
std::iota(arrayNumber + 1, arrayNumber + 10, 1);
}
Here the function iota fills the passed range in the array with sequentially increasing values, starting with 1.

Related

How to make different number of for loops run according to user input in C++

I am trying to make a table data structure in C++. Currently I have written the following code :
map<array<int, 3>, array<int, 6>> q_table;
void qtable(){
for (int i =-SIZE; i<SIZE; i++){ //SIZE is a constant say 10
for (int j =-SIZE; j<SIZE; j++){
for (int k =-SIZE; k<SIZE; k++){
q_table[{i,j,k}][0] = (rand() % 5)+1; //Initializing with random numbers
q_table[{i,j,k}][1] = (rand() % 5)+1;
q_table[{i,j,k}][2] = (rand() % 5)+1;
q_table[{i,j,k}][3] = (rand() % 5)+1;
q_table[{i,j,k}][4] = (rand() % 5)+1;
q_table[{i,j,k}][5] = (rand() % 5)+1;
//Here I am creating a map such which looks like :
// q_table[{0,0,0}][0] = -0.1;
// q_table[{0,0,0}][1] = 0.2;
// q_table[{0,0,0}][2] = -0.3;
// q_table[{0,0,0}][3] = -3.2;
// q_table[{0,0,0}][4] = -1.2;
// q_table[{0,0,0}][5] = 9.2;
// q_table[{0,0,1}][0] = 5.7;
// q_table[{0,0,1}][1] = -0.9;
// q_table[{0,0,1}][2] = 3.4;
// q_table[{0,0,1}][3] = 7.9;
// q_table[{0,0,1}][4] = 6.4;
// q_table[{0,0,1}][5] = 3.6; and so on
}
}
}
}
Here I have initialized the map(q_table) above by giving a constant value of 3 and 6 respectively. Accordingly I have made 3 for loops for its proper initialization.
Now I want to improve the project by taking the value from the user as an input to create the map during run time. I am not able to find a way to create the map during run time. The main challenge I am facing is since for loops are use to create the map then how can I write a function with unknown number of for loops if I don't know how many of them I would need.
Something along these lines (untested)
int dims = ...; // the number of dimensions
int size = ...; // All dimensions run 0 through size-1
std::vector<int> indices(dims); // starting with all zeros
bool done = false;
while (!done) {
// Do something with the tuple of indices
// Advance to next tuple
for (int dim = dims - 1; dim >= 0; --dim) {
if (++indices[dim] < size) break;
indices[dim] = 0;
done = (dim == 0);
}
}
If things are decided at runtime then you might want std::arrays as your key-value pairs.
There are many ways to index multidimensional arrays with a single for-loop. Going from a multidimensional array to a one-dimensional array is easy. If i, j, k are your indices, then you'll store this at position index=k*N*N+j*N+i; in your multidimensional array (where in your case, N=2*SIZE. If you meant to type for (int i =-SIZE; i<=SIZE; i++), then you'd have N=2*SIZE+1). So if you have an n-dimensional array and index[k] is an array of indices, you can pack this into a one-dimesional index with the following algorithm:
//dynamically allocated array of size depth
std::vector<int> index(depth,0);
// ... populate some interesting index here, where 0<=index[i]<N
//for depth=3, this would calculate 0*N*N*N+index[2]*N*N+index[1]*N+index[0].
int one_dimensional_index=0;
for(int i=depth-1; i>=0; i--) {
one_dimensional_index*=N;
one_dimensional_index+=index[i];
}
std::cout<<"Multidimensional index is stored at one-dimensional index: "<<one_dimensional_index<<std::endl;
But if I understand correctly, you want to do the inverse problem. This can be done by using integer division and modular arithmetic. (index[2]*N*N+index[1]*N+index[0])%N is just index[0], and (index[2]*N*N+index[1]*N+index[0])/N is just index[2]*N+index[1], so by doing division and modulo operations you can get the indices back. In the end, you'd have an implementation that looks something like this:
std::vector<int> indexFromMulti(int one_dimensional_index,int nn, int depth){
std::vector<int> index(depth,0);
for(int i=0; i<depth; i++) {
index[i]=(one_dimensional_index%N);
one_dimensional_index/=N;
}
return index;
}
Here is a full implementation that prints the following for size=1, depth=4, and breadth=4.
user#desktop:~$ g++ test.cpp
user#desktop:~$ ./a.out
qtable populated with:
qtable[{-1, -1, -1, -1}]=[4, 2, 3, 1];
qtable[{-1, -1, -1, 0}]=[3, 4, 5, 3];
qtable[{-1, -1, 0, -1}]=[1, 2, 3, 2];
(...)
qtable[{0, 0, -1, 0}]=[5, 3, 4, 5];
qtable[{0, 0, 0, -1}]=[3, 1, 5, 3];
qtable[{0, 0, 0, 0}]=[1, 1, 5, 3];
Source code:
#include <iostream>
#include <vector>
#include <map>
std::vector<int> indexFromMulti(int one_dimensional_index,int nn, int depth){
std::vector<int> index(depth,0);
for(int i=0; i<depth; i++) {
index[i]=(one_dimensional_index%nn);
one_dimensional_index/=nn;
}
return index;
}
//integer power from https://stackoverflow.com/a/1505791/13783653
int int_pow(int x, int p)
{
if (p == 0) return 1;
if (p == 1) return x;
int tmp = int_pow(x, p/2);
if (p%2 == 0) return tmp * tmp;
else return x * tmp * tmp;
}
//Function to print with pretty formatting
void print_qtable(std::map<std::vector<int>, std::vector<int>> qtable){
std::cout<<"qtable populated with: "<<std::endl;
for(auto iter = qtable.begin(); iter != qtable.end(); ++iter) {
std::cout<<"qtable[{";
for(size_t i=0;i<iter->first.size();i++){
std::cout<<iter->first[i];
if(i!=iter->first.size()-1)
std::cout<<", ";
}
std::cout<<"}]=[";
for(size_t i=0;i<iter->second.size();i++){
std::cout<<iter->second[i];
if(i!=iter->second.size()-1)
std::cout<<", ";
}
std::cout<<"];"<<std::endl;
}
}
std::map<std::vector<int>, std::vector<int>> qtable(int size, int depth, int breadth){
int nn=2*size;
int max_index=int_pow(nn,depth);
std::map<std::vector<int>, std::vector<int>> q_table;
for (int i=0;i<max_index;i++){
std::vector<int> key=indexFromMulti(i,nn,depth);
//change the interval [0,nn) to the interval [-size,size).
for(int j=0;j<depth;j++)
key[j]-=size;
//Fill in the value
std::vector<int> value(breadth,0);
for(int j=0;j<breadth;j++)
value[j] = (rand() % 5)+1;
q_table[key]=value;
}
return q_table;
}
int main() {
print_qtable(qtable(1,4,4));
return 0;
}

Sorting an array to another array C++

My program have to sort an array in another array.
When I run the program it prints 1 2 3 -858993460 5 -858993460 7.
I can not understand where the mistake is in the code.
#include <iostream>
using namespace std;
int main()
{
const int N = 7;
int arr[N] = { 3, 17, 2, 9, 1, 5, 7 };
int max = arr[0];
for (int i = 1; i < N; i++)
{
if (max < arr[i])
max = arr[i];
}
int sort_arr[N];
for (int j = 0; j < N; j++)
{
sort_arr[arr[j] - 1] = arr[j];
}
for (int i = 0; i < N; i++)
{
cout << sort_arr[i] << " ";
}
return 0;
}
Okay lets face the problems in your code.
The "weird" numbers you see there, came from the uninitialzied array sort_arr. What do I mean by uninitialized? Well sort_arr is a little chunck somewhere in your memory. Since a program usually does not clear its memory and rather claims the memory it used as free, the chunk of sort_arr may contain bits and bytes set by another program. The numbers occure since these bytes are interpreted as an integer value. So the first thing to do would be to initialize the array before using it.
sort_arr[N] = { 0, 0, 0, 0, 0, 0, 0 };
Now why did these numbers occure? Well you're probably expecting your algorithm to set all values in sort_arr which would result in an sorted array, right? Well but your algorithm isn't working that well. See this line:
sort_arr[arr[j] - 1] = arr[j];
What happens when j is 1? arr[1] is then evaluated to 17 and 17 - 1 equals 16. So sort_arr[arr[1] - 1] is the same as sort_arr[16] which exceeds the bounds of your array.
If you want to program a sorting algorithm by your self than I would recommend to start with an simple bubble sort algorithm. Otherwise, if you only need to sort the array have a look at the algorithm header. It is fairly simple to use:
#include <iostream>
#include <algorithm>
#include <iterator> // << include this to use begin() and end()
using namespace std;
int main()
{
const int N = 7;
int arr[N] = { 3, 17, 2, 9, 1, 5, 7 };
int sort_arr[N] = { 0, 0, 0, 0, 0, 0, 0 };
copy(begin(arr), end(arr), begin(sort_arr));
sort(begin(sort_arr), end(sort_arr));
for (int i = 0; i < N; i++)
{
cout << sort_arr[i] << " ";
}
cout << endl;
}
By the way. You're looking for the biggest value in your array, right? After you have sorted the array sort_arr[N - 1] is the biggest value contained in your array.
If you want to sort a array into another array then one way is you make a copy of the array and then use the sort function in the standard library to sort the second array.
int arr[10];
int b[10];
for(int i=0;i<10;i++)
{
cin>>arr[i];
b[i]=arr[i];
}
sort(b,b+10);
// this sort function will sort the array elements in ascending order and if you want to change the order then just add a comparison function as third arguement to the sort function.
It seems that you think that sort_arr[arr[j] - 1] = arr[j] will sort arr into sort_arr. It won't.
Sorting is already written for you here: http://en.cppreference.com/w/cpp/algorithm/sort You can use that like this:
copy(cbegin(arr), cend(arr), begin(sort_arr));
sort(begin(sort_arr), end(sort_arr));
Live Example
My guess is this is an attempt to implement a type of counting sort. Note that variable length arrays aren't normally allowed in C++ or some versions of C. You could use _alloca() to allocate off the stack to get the equivalent of a variable length array: int * sort_arr = (int *)_alloca(max * sizeof(int)); .
#include <iostream>
using namespace std;
int main()
{
const int N = 7;
// assuming range of values is 1 to ...
int arr[N] = { 3, 17, 2, 9, 1, 5, 7 };
int max = arr[0];
for (int i = 1; i < N; i++)
{
if (max < arr[i])
max = arr[i];
}
int sort_arr[max];
for (int i = 0; i < max; i++)
{
sort_arr[i] = 0;
}
for (int j = 0; j < N; j++)
{
sort_arr[arr[j] - 1]++;
}
for (int i = 0; i < max; i++)
{
while(sort_arr[i])
{
cout << i+1 << " ";
sort_arr[i]--;
}
}
return 0;
}

How to Delete all the element which are at some index of first array and the index is taken from second array?

I want to Write a function which takes 2 arrays-
One array is the source array and the other array is the array of indices.
I want to delete all those elements present at the indices of the source array taking the indices from the second array.
Suppose First array is : {12,5,10,7,4,1,9} and index array is : {2,3,5}.
Then the elements at index 2,3,5. i.e. 10, 7 and 1 are deleted from first array.
So first array becomes : {12,5,4,9}.
If the indices array is sorted,then my O(N) solution is:
#include<iostream>
using namespace std;
int main()
{
int arr[]={12,5,10,7,4,1,9},n=7,indices[]={2,3,5},m=3;
int j=0,k=0;
for(int i=0;i<n,k<m;i++)
{
if(i!=indices[k])
arr[j++]=arr[i];
else
k++;
}
for(i=0; i<j; i++)
cout<<arr[i]<<" ";
return 0;
}
How to do it in O(n) if the indices array is not sorted ?
According to the comments:
Is there any value that will never appear in arr but is representable by int?
You can take that as int max.
Now you can use removeIndices
#include<iostream>
#include<limits>
int removeIndices(int* arr, int n, int* indices, int m){
const int NEVER_IN_ARRAY = std::numeric_limits<int>::max();
for(int i = 0; i < m; i++)
arr[indices[i]] = NEVER_IN_ARRAY;
for(int from = 0, to = 0; from < n; from++)
if(arr[from] != NEVER_IN_ARRAY)
arr[to++] = arr[from];
return n - m;
}
int main(){
int arr[] = {12, 5, 10, 7, 4, 1, 9}, n = 7, indices[] = {2, 3, 5}, m = 3;
int newSize = removeIndices(arr, n, indices, m);
for(int i = 0; i < newSize; i++)
std::cout << arr[i] << " ";
return 0;
}
Edit: With
#include<algorithm>
#include<functional>
We can do:
int removeIndices(int* arr, int n, int* indices, int m){
const int NEVER_IN_ARRAY = std::numeric_limits<int>::max();
std::for_each(indices, indices + m, [arr](int index){ arr[index] = NEVER_IN_ARRAY; });
int* p = std::remove_if(arr, arr + n, std::bind2nd(std::equal_to<int>(), NEVER_IN_ARRAY));
return p - arr;
}
loop thru filter array and mark dead elements with tombstones
create a new array, and copy step-by-step while skipping tombstones
if it's possible use a tombstone value, for example if it is guranteed that -1 doesn't appear in the input then -1 can be the tombstone value
if this is not possible use an array of boolean markers, init them to false
in-place filtering after marking:
for(int i=0,j=0;j<n;i++,j++){
if( a[j] == TOMBSTONE ){
i--; // the loop will add +1
continue;
}
if(i==j)
continue; // skip, no need to write
arr[i]=arr[j];
}
arr input length: n
arr new length: i
May be you want something like this:
#include<iostream>
#define INVALID 99999 //to mark the elements who will disappear
using namespace std;
int main()
{
int arr[] = {0,1,2,3,4,5,6,7,8,9,10};
int indices = {3,1,5};
int indices_len = 3;
int arr_len = 3;
for(int i=0; i<indices_len; i++){
arr[indices[i]] = INVALID;
}
int invalid_count=0;
for(int i=0; i<arr_len; i++){
if(arr[i] == INVALID){
invalid_count++;
}
arr[i-invalid_count] = arr[i];
}
return 0;
}
You must add the result to a new array 1-just iterate over all all elements if the index is in the to delete array continue else copy it to the new array, you can look at the CArray class from MFC it has RemoveAt method
PseudoCode
int old_arr[MAX_SIZE], new_arr[MAX_SIZE];
bool to_del[MAX_SIZE] = {0};
int index_to_del[MAX_SIZE];
for (size_t i = 0; i < MAX_SIZE; ++i)
to_del[index_to_del[i]] = true;
size_t new_size = 0;
for (size_t i = 0; i < MAX_SIZE; ++i)
if (!to_del[i])
new_arr[new_size++] = old_arr[i];
Edit
The above snippet consumes extra space.
If we had to do it in-place, then every time, we delete an element we will have to shift all consecutive elements by 1. In worst case, this could be O(n**2). If you want to do it in-place without yourself copying array elements, you could use vector.
If deletes outnumber reads, then consider using multiset
Here is a solution that does it in-place, does not allocate memory on the heap, does not require flag values, and does it in O(N+M) time:
#include <cstddef>
template<std::size_t N>
std::size_t removeIndices( int(&src)[N], std::size_t srcSize, int const* removal, std::size_t removeSize )
{
bool remove_flags[N] = {false};
for( int const* it = removal; it != removal+removeSize; ++it ) {
remove_flags[*it] = true;
}
int numberKept = 0;
for( int i = 0; i < srcSize; ++i ) {
if( !remove_flags[i] ) {
if (numberKept != i)
src[numberKept] = src[i];
++numberKept;
}
}
return numberKept;
}
Note that it needs full access to the source array, because I create a temporary stack buffer of bool that is the same size. In C++1y, you'll be able to do this without that compile time knowledge using variable length arrays or similar types.
Note that some compilers implement VLAs via (hopefully partial) C99 compatibility already.

Writing bucket sort in c++

A book I have says this:
a) Place each value of the one-dimensional array into a row of the bucket array based on the value's ones digit. For example, 97 is placed in row 7, 3 is placed in row 3, and 100 is placed in row 0. This is called a "distribution pass."
b) Loop through the bucket array row by row, and copy the values back to the original array. This is called a "gathering pass." The new order of the preceding values in the one-dimensional array is 100, 3, and 97.
c) Repeat this process for each subsequent digit position.
I am having a lot of trouble trying to understand and implement this. So far I have:
void b_sort(int sarray[], int array_size) {
const int max = array_size;
for(int i = 0; i < max; ++i)
int array[i] = sarray[i];
int bucket[10][max - 1];
}
I'm thinking that in order to sort them by ones, tens, hundreds, etc, I can use this:
for(int i = 0; i < max; ++i)
insert = (array[i] / x) % 10;
bucket[insert];
where x = 1, 10, 100, 1000, etc. I am totally lost on how to write this now.
Here's a bucket sort based on the info in the OP question.
void b_sort(int sarray[], int array_size) {
const int max = array_size;
// use bucket[x][max] to hold the current count
int bucket[10][max+1];
// init bucket counters
for(var x=0;x<10;x++) bucket[x][max] = 0;
// main loop for each digit position
for(int digit = 1; digit <= 1000000000; digit *= 10) {
// array to bucket
for(int i = 0; i < max; i++) {
// get the digit 0-9
int dig = (sarray[i] / digit) % 10;
// add to bucket and increment count
bucket[dig][bucket[dig][max]++] = sarray[i];
}
// bucket to array
int idx = 0;
for(var x = 0; x < 10; x++) {
for(var y = 0; y < bucket[x][max]; y++) {
sarray[idx++] = bucket[x][y];
}
// reset the internal bucket counters
bucket[x][max] = 0;
}
}
}
Notes Using a 2d array for the bucket wastes a lot of space... an array of queues/lists usually makes more sense.
I don't normally program in C++ and the above code was written inside the web browser, so syntax errors may exist.
The following code uses hex digits for a bucket sort (for BITS_PER_BUCKET=4). Ofcourse it is meant to be instructive, not productive.
#include <assert.h>
#include <stdio.h>
#define TEST_COUNT 100
#define BITS_PER_BUCKET 4
#define BUCKET_COUNT (1 << BITS_PER_BUCKET)
#define BUCKET_MASK (BUCKET_COUNT-1)
#define PASS_COUNT (8*sizeof(int)/BITS_PER_BUCKET)
int main(int argc, char** argv) {
printf("Starting up ...");
assert((PASS_COUNT*BITS_PER_BUCKET) == (8*sizeof(int)));
printf("... OK\n");
printf("Creating repeatable very-pseudo random test data ...");
int data[TEST_COUNT];
int x=13;
int i;
for (i=0;i<TEST_COUNT;i++) {
x=(x*x+i*i) % (2*x+i);
data[i]=x;
}
printf("... OK\nData is ");
for (i=0;i<TEST_COUNT;i++) printf("%02x, ",data[i]);
printf("\n");
printf("Creating bucket arrays ...");
int buckets[BUCKET_COUNT][TEST_COUNT];
int bucketlevel[BUCKET_COUNT];
for (i=0;i<BUCKET_COUNT;i++) bucketlevel[i]=0;
printf("... OK\n");
for (i=0;i<PASS_COUNT;i++) {
int j,k,l;
printf("Running distribution pass #%d/%d ...",i,PASS_COUNT);
l=0;
for (j=0;j<TEST_COUNT;j++) {
k=(data[j]>>(BITS_PER_BUCKET*i)) & BUCKET_MASK;
buckets[k][bucketlevel[k]++]=data[j];
l|=k;
}
printf("... OK\n");
if (!l) {
printf("Only zero digits found, sort completed early\n");
break;
}
printf("Running gathering pass #%d/%d ...",i,PASS_COUNT);
l=0;
for (j=0;j<BUCKET_COUNT;j++) {
for (k=0;k<bucketlevel[j];k++) {
data[l++]=buckets[j][k];
}
bucketlevel[j]=0;
}
printf("... OK\nData is ");
for (l=0;l<TEST_COUNT;l++) printf("%02x, ",data[l]);
printf("\n");
}
}
a rewrite of Louis's code in C++11 with STL queues.
void bucket_sort(vector<int>& arr){
queue<int> buckets[10];
for(int digit = 1; digit <= 1e9; digit *= 10){
for(int elem : arr){
buckets[(elem/digit)%10].push(elem);
}
int idx = 0;
for(queue<int>& bucket : buckets){
while(!bucket.empty()){
arr[idx++] = bucket.front();
bucket.pop();
}
}
}
}

Optimizing subset sum implementation

I'm working on a solution to a variant of the subset sum problem, using the below code. The problem entails generating subsets of 11 ints from a larger set (superset) and check if it matches a specific value (endsum).
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int endsum = 0, supersetsize = 0, done = 0;
int superset[] = {1,30,10,7,11,27,3,5,6,50,45,32,25,67,13,37,19,52,18,9};
int combo = 0;
int searchForPlayerInArray(int arr[], int player) {
for (int i=0; i<11; i++) {
if (arr[i] == player) {
return 1;
}
}
return 0;
}
int sumOfArray(int arr[]) {
int res = 0;
for (int i=0; i<11; i++) {
res+=arr[i];
}
return res;
}
void printArray(int arr[], int arrSize) {
for (int j=0; j<arrSize; j++) {
printf("%2d ",arr[j]);
}
printf("= %d\n",endsum);
}
void permute(int subset[], int pos, int sspos) {
if (done) { //when a correct solution has been found, stop recursion
return;
}
if (sspos == supersetsize) { // out of possible additions
return;
}
if (pos == 11) { //is the current subset 11 ints long?
int res = sumOfArray(subset);
combo++;
if (res == endsum) { //if the sum of the array matches the wanted sum, print
printArray(subset,11);
done = 1;
}
return;
}
for (int i=sspos; i<supersetsize; i++) {
//assert(pos < 11);
//assert(i+1 <= supersetsize);
subset[pos] = superset[i];
permute(subset,pos+1,i+1);
}
}
int main(void) {
endsum = 110;
supersetsize = 20;
int *arr;
arr = malloc(supersetsize*sizeof(int));
int i;
for (i=0; i<supersetsize; i++) {
arr[i] = 0;
}
permute(arr,0,0);
printf("Combinations: %d",combo);
return 0;
}
Although this solution works for small supersets (<15) it is slow and inefficient because it generates every possible permutation instead of just the unique ones. How can I optimize it to generate only unique subsets?
Edit: Complete source code added by popular demand.
One way to only generate unique subsets is to add the elements from the superset in order, and use an additional argument to permute (eg. supersetPos) to indicate where you are in the superset. This generates sorted permutations which will be unique.
EDIT: Code that AFAIK runs correctly on your sample:
#include <stdio.h>
int superset[] = {
1, 30, 10, 7, 11,
27, 3, 5, 6, 50,
45, 32, 25, 67, 13,
37, 19, 52, 18, 9
};
int supersetsize = 20;
int endsum = 110;
int done = 0;
int sumOfArray(int array[]) {
int sum = 0;
for(int i = 0; i < 11; i++)
sum += array[i];
return sum;
}
void permute(int subset[], int pos, int sspos) {
if (pos == 11) { //is the current subset 11 ints long?
if (sumOfArray(subset) == endsum) { //if the sum of the array matches the wanted sum, print
for (int j=0; j<11; j++) {
printf("%d ",subset[j]);
}
printf("\n");
done = 1;
}
return;
}
for (int i=sspos; i<supersetsize; i++) {
subset[pos] = superset[i];
permute(subset,pos+1,i+1);
if (done) { //when a correct solution has been found, stop recursion
return;
}
}
}
int main() {
int subset[11] = {0};
permute(subset, 0, 0);
}
I don't think there is a way to generate the unique subsets in better than exponential time.
To solve subset-sum efficiently you want to use dynamic programming. There are some pseudo-polynomial time algorithms for subset-sum that work this way. This Wikipedia article might help.
you can try my code ( I tried to only give a psudo code and not to solve your homework completely):
// array is all the numbers you are looking from them
// length is the number of arrays
// pos is the position of the slot you are going to fill
// max is nomber of slots you have to fill (in your case since you are going for the 11 sets you have to set this value as 11
// sum is the sum of all the values selected until now
// searchbegin is the first element you can pick from your array (I'm using this variable to only generate subarrays of the superset (array))
// target is the targetvalue you are looking for.
void generate_all(int []array, int length, int pos,int max, int sum,int searchbegin,int target)
{
if max = pos
if sum = target
printselectedresults();
for i:searchbegin->length-max+pos
if (sum + array[i] < target)
{
addtoresults(i);
generate_all(array,length,pos+1,max,sum+array[i],i+1,target);
removefromresults(i);
}
}
with all this information I think you can easily Implement this code your target language and use it.
in my function all the permutations generated are subarrays of superset so no permutation can be generated twice, and also every one is generated at least one time.