J
Just For Fun...
Can someone translate the code below to VBA.
int N, M, P; /* the given values of N, M, and P */
int S[N+1]; /* Given set S -- not using array element 0 */
int c[M+1]; /* indicates which indexes are in the current subset */
int SUM = 0; /* current sum */
int tsum; /* temporary for a partial sum of newly added elements */
int count = 0; /* number of subsets whose SUM is < P
int i;
int j=1;
c[0] = -1;
for i=1 to M by 1 /* loop sets initial subset and its SUM */
{
c = i;
SUM = SUM + S;
}
while (j > 0 AND SUM < P)
{
count = count + 1;
j = M;
while (c[j] == N - M + j) /* back up throwing elements out */
{
SUM = SUM - S[c[j]];
j = j - 1;
}
next_update: /* goto label */
SUM = SUM - S[c[j]];
c[j] = c[j] + 1;
tsum = S[c[j]];
for i = j+1 to M by 1 /* advance adding elements in */
{
c = c[i-1] + 1;
tsum = tsum + S[c];
}
if (SUM + tsum >= P)
{
j = j - 1; /* backup 1 level */
if (j <= 0) break; /* break out of while loop */
goto next_update;
}
SUM = SUM + tsum;
}
print "the number of subsets is," count;
int N, M, P; /* the given values of N, M, and P */
int S[N+1]; /* Given set S -- not using array element 0 */
int c[M+1]; /* indicates which indexes are in the current subset */
int SUM = 0; /* current sum */
int tsum; /* temporary for a partial sum of newly added elements */
int count = 0; /* number of subsets whose SUM is < P
int i;
int j=1;
c[0] = -1;
for i=1 to M by 1 /* loop sets initial subset and its SUM */
{
c = i;
SUM = SUM + S;
}
while (j > 0 AND SUM < P)
{
count = count + 1;
j = M;
while (c[j] == N - M + j) /* back up throwing elements out */
{
SUM = SUM - S[c[j]];
j = j - 1;
}
next_update: /* goto label */
SUM = SUM - S[c[j]];
c[j] = c[j] + 1;
tsum = S[c[j]];
for i = j+1 to M by 1 /* advance adding elements in */
{
c = c[i-1] + 1;
tsum = tsum + S[c];
}
if (SUM + tsum >= P)
{
j = j - 1; /* backup 1 level */
if (j <= 0) break; /* break out of while loop */
goto next_update;
}
SUM = SUM + tsum;
}
print "the number of subsets is," count;