Code translation

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;

D

Dave Peterson

If this helps, I'd be surprised:

Option Explicit
Sub testme()

Dim N As Long
Dim M As Long
Dim P As Long
Dim S() As Long
Dim c() As Long
Dim SUM As Long
Dim lCount As Long
Dim tSum As Long
Dim i As Long
Dim j As Long

'initialize some variables??
N = 10
M = 20
P = 30

ReDim S(0 To N)
ReDim c(0 To M)

SUM = 0
lCount = 0
j = 1

c(0) = -1

For i = 1 To M Step 1
c(i) = i
SUM = SUM + S(i) 'where was S() initialized???
Next i

Do While (j > 0 And SUM < P)
lCount = lCount + 1
j = M

While (c(0) = N - M + j)
SUM = SUM - S(c(j))
j = j - 1
Wend

next_update:
SUM = SUM - S(c(j))
c(j) = c(j) + 1

tSum = S(c(j))
For i = j + 1 To M Step 1
c(i) = c(i - 1) + 1
tSum = tSum + S(c(i))
Next i

If (SUM + tSum >= P) Then
j = j - 1 '
If (j <= 0) Then
Exit Do
End If

GoTo next_update:

SUM = SUM + tSum
End If

Loop

MsgBox "the number of subsets is," & lCount

End Sub

But I didn't know how S() was initialized.

And this portion:
For i = 1 To M Step 1
c(i) = i
SUM = SUM + S(i) 'where was S() initialized???
Next i

makes it look like S() can go to 1 to M. It looked like S() was dimmed between
0 to N. That could cause an error if N < M.

(it compiled--but I didn't run it.)

Just For Fun... said:
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;

J

Just For Fun...

Dave said:
If this helps, I'd be surprised:

Option Explicit
Sub testme()

Dim N As Long
Dim M As Long
Dim P As Long
Dim S() As Long
Dim c() As Long
Dim SUM As Long
Dim lCount As Long
Dim tSum As Long
Dim i As Long
Dim j As Long

'initialize some variables??
N = 10
M = 20
P = 30

ReDim S(0 To N)
ReDim c(0 To M)

SUM = 0
lCount = 0
j = 1

c(0) = -1

For i = 1 To M Step 1
c(i) = i
SUM = SUM + S(i) 'where was S() initialized???
Next i

Do While (j > 0 And SUM < P)
lCount = lCount + 1
j = M

While (c(0) = N - M + j)
SUM = SUM - S(c(j))
j = j - 1
Wend

next_update:
SUM = SUM - S(c(j))
c(j) = c(j) + 1

tSum = S(c(j))
For i = j + 1 To M Step 1
c(i) = c(i - 1) + 1
tSum = tSum + S(c(i))
Next i

If (SUM + tSum >= P) Then
j = j - 1 '
If (j <= 0) Then
Exit Do
End If

GoTo next_update:

SUM = SUM + tSum
End If

Loop

MsgBox "the number of subsets is," & lCount

End Sub

But I didn't know how S() was initialized.

And this portion:
For i = 1 To M Step 1
c(i) = i
SUM = SUM + S(i) 'where was S() initialized???
Next i

makes it look like S() can go to 1 to M. It looked like S() was dimmed between
0 to N. That could cause an error if N < M.

(it compiled--but I didn't run it.)

Just For Fun... said:
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;