Skip to main content
Post Reopened by Ampt, user53019, durron597, user22815, ratchet freak
The result is the sum for each combination. Not the sum of all combination.
Source Link

I have an array of elements:

int[] elem = new int[] {A, B, C};

I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I will use the notation:

  • {0,1} mean element can be selected or not
  • {1} mean element is always selected.

Therefore, I need to calculate the following value, which could be something like:

result = {0,1}*A + {1}*B + {0,1}*C;

I have tried to use nested for loops, like so:

total = 0;
for (int i=0;i<2;i++)
    for (int j=1;j<2;j++) // notice I start at 1, not 0, because B is {1}
        for (int k=0;k<2;k++)
            total +=Console.Write( i*A + j*B + k*C;k*C );

There are two problems with this:

  • These nested for loops are rather cumbersome
  • My example only has three elements, but the array could be of any size. For example, if the array had 10 elements, I would need to have 10 nested loops instead of three. I don't know to change the number of for loops based on the array size.

How would I iterate to get all sum values for all combinations in an arbitrary sized array?

I have an array of elements:

int[] elem = new int[] {A, B, C};

I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I will use the notation:

  • {0,1} mean element can be selected or not
  • {1} mean element is always selected.

Therefore, I need to calculate the following value, which could be something like:

result = {0,1}*A + {1}*B + {0,1}*C;

I have tried to use nested for loops, like so:

total = 0;
for (int i=0;i<2;i++)
    for (int j=1;j<2;j++) // notice I start at 1, not 0, because B is {1}
        for (int k=0;k<2;k++)
            total += i*A + j*B + k*C;

There are two problems with this:

  • These nested for loops are rather cumbersome
  • My example only has three elements, but the array could be of any size. For example, if the array had 10 elements, I would need to have 10 nested loops instead of three. I don't know to change the number of for loops based on the array size.

How would I iterate to get all sum values for all combinations in an arbitrary sized array?

I have an array of elements:

int[] elem = new int[] {A, B, C};

I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I will use the notation:

  • {0,1} mean element can be selected or not
  • {1} mean element is always selected.

Therefore, I need to calculate the following value, which could be something like:

result = {0,1}*A + {1}*B + {0,1}*C;

I have tried to use nested for loops, like so:

for (int i=0;i<2;i++)
    for (int j=1;j<2;j++) // notice I start at 1, not 0, because B is {1}
        for (int k=0;k<2;k++)
            Console.Write( i*A + j*B + k*C );

There are two problems with this:

  • These nested for loops are rather cumbersome
  • My example only has three elements, but the array could be of any size. For example, if the array had 10 elements, I would need to have 10 nested loops instead of three. I don't know to change the number of for loops based on the array size.

How would I iterate to get all sum values for all combinations in an arbitrary sized array?

Tried to improve the clarity
Source Link
durron597
  • 7.6k
  • 10
  • 38
  • 67

I have an array of elements.:

int[] elem = new int[] {A, B, C, D, E};

I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I will use the notation:

  • {0,1} mean element can be selected or not
  • {1} mean element is always selected.

Therefore, I need to calculate the following value, which could be something like:

result = {0,1}*A + {0,1}*B + {0,1}*C + {1}*D + {0,1}*E*C;

{0,1} mean element can be selected or not
{1} mean element is always selected.

I could do somethinghave tried to use nested for loops, like so:

fortotal (int= i=0;i<2;i++)0;
    for (int j=0;j<2;j++i=0;i<2;i++)
        for (int k=0;k<2;k++j=1;j<2;j++)
   // notice I start at 1, not 0, because forB (intis l=1;l<2;l++){1}
               for (int m=0;m<2;m++k=0;k<2;k++)
                 total result+= i*A + j*B + k*C + l*D + m*Ek*C;

There are two problems with this:

  • These nested for loops are rather cumbersome
  • TheMy example only has fivethree elements in, but the elem array and the nested loops to handle it - how do you handle it if elem array was determined at runtime? It could be five, or sixof any size. For example, or ten or whatever elements inif the array. The had 10 elements, I would need to have 10 nested loops only workinstead of three. I don't know to change the number of for oneloops based on the array size.

So how shouldHow would I iterate to get all sum values for all combinations in an arbitrary sized array?

I have an array of elements.

int[] elem = new int[] {A, B, C, D, E};

I need to calculate ALL the combinations of those elements

result = {0,1}*A + {0,1}*B + {0,1}*C + {1}*D + {0,1}*E

{0,1} mean element can be selected or not
{1} mean element is always selected.

I could do something like

for (int i=0;i<2;i++)
    for (int j=0;j<2;j++)
        for (int k=0;k<2;k++)
            for (int l=1;l<2;l++)
               for (int m=0;m<2;m++)
                  result i*A + j*B + k*C + l*D + m*E

There are two problems with this:

  • These nested for loops are rather cumbersome
  • The example has five elements in the elem array and the nested loops to handle it - how do you handle it if elem array was determined at runtime? It could be five, or six, or ten or whatever elements in the array. The nested loops only work for one array size.

So how should I iterate to get all sum values for all combinations in an arbitrary sized array?

I have an array of elements:

int[] elem = new int[] {A, B, C};

I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I will use the notation:

  • {0,1} mean element can be selected or not
  • {1} mean element is always selected.

Therefore, I need to calculate the following value, which could be something like:

result = {0,1}*A + {1}*B + {0,1}*C;

I have tried to use nested for loops, like so:

total = 0;
for (int i=0;i<2;i++)
    for (int j=1;j<2;j++) // notice I start at 1, not 0, because B is {1}
        for (int k=0;k<2;k++)
            total += i*A + j*B + k*C;

There are two problems with this:

  • These nested for loops are rather cumbersome
  • My example only has three elements, but the array could be of any size. For example, if the array had 10 elements, I would need to have 10 nested loops instead of three. I don't know to change the number of for loops based on the array size.

How would I iterate to get all sum values for all combinations in an arbitrary sized array?

added 4 characters in body
Source Link

I have an array of elements.

int[] elem = new int[] {A, B, C, D, E};

I need to calculate ALL the combinations of those elements

result = {0,1}*A + {0,1}*B + {0,1}*C + {1}*D + {0,1}*E

{0,1} mean element can be selected or not
{1} mean element is always selected.

I could do something like

for (int i=0;i<2;i++)
    for (int j=0;j<2;j++)
        for (int k=0;k<2;k++)
            for (int l=1;l<2;l++)
               for (int m=0;m<2;m++)
                  result i*A + j*B + k*C + l*D + m*E

There are two problems with this:

  • These nested for loops are rather cumbersome
  • The example has five elements in the elem array and the nested loops to handle it - how do you handle it if elem array was determined at runtime? It could be five, or six, or ten or whatever elements in the array. The nested loops only work for one array size.

So how should I iterate to get all values for an arbitrary sized array?So how should I iterate to get all sum values for all combinations in an arbitrary sized array?

I have an array of elements.

int[] elem = new int[] {A, B, C, D, E};

I need to calculate ALL the combinations of those elements

result = {0,1}*A + {0,1}*B + {0,1}*C + {1}*D + {0,1}*E

{0,1} mean element can be selected or not
{1} mean element is always selected.

I could do something like

for (int i=0;i<2;i++)
    for (int j=0;j<2;j++)
        for (int k=0;k<2;k++)
            for (int l=1;l<2;l++)
               for (int m=0;m<2;m++)
                  result i*A + j*B + k*C + l*D + m*E

There are two problems with this:

  • These nested for loops are rather cumbersome
  • The example has five elements in the elem array and the nested loops to handle it - how do you handle it if elem array was determined at runtime? It could be five, or six, or ten or whatever elements in the array. The nested loops only work for one array size.

So how should I iterate to get all values for an arbitrary sized array?

I have an array of elements.

int[] elem = new int[] {A, B, C, D, E};

I need to calculate ALL the combinations of those elements

result = {0,1}*A + {0,1}*B + {0,1}*C + {1}*D + {0,1}*E

{0,1} mean element can be selected or not
{1} mean element is always selected.

I could do something like

for (int i=0;i<2;i++)
    for (int j=0;j<2;j++)
        for (int k=0;k<2;k++)
            for (int l=1;l<2;l++)
               for (int m=0;m<2;m++)
                  result i*A + j*B + k*C + l*D + m*E

There are two problems with this:

  • These nested for loops are rather cumbersome
  • The example has five elements in the elem array and the nested loops to handle it - how do you handle it if elem array was determined at runtime? It could be five, or six, or ten or whatever elements in the array. The nested loops only work for one array size.

So how should I iterate to get all sum values for all combinations in an arbitrary sized array?

Post Closed as "Needs details or clarity" by Arseni Mourzenko, gnat, user22815, user53019, durron597
Reword to bring out the key problem
Source Link
user40980
user40980
Loading
deleted 4 characters in body; edited title
Source Link
gnat
  • 20.8k
  • 29
  • 115
  • 295
Loading
Source Link
Loading