May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Become a Premium Member and unlock a new, free course in leading technologies each month.

Solved

Posted on 2004-09-30

Can someone translate the code below to VB/ 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] = i;

SUM = SUM + S[i];

}

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[i] = c[i-1] + 1;

tsum = tsum + S[c[i]];

}

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] = i;

SUM = SUM + S[i];

}

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[i] = c[i-1] + 1;

tsum = tsum + S[c[i]];

}

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;

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

- Help others & share knowledge
- Earn cash & points
- Learn & ask questions

3 Comments

Dim N as integer

Dim M as Integer

Dim P as Integer /* the given values of N, M, and P */

Dim SUM as Ingteger /* current sum */

Dim tsum as Integer /* temporary for a partial sum of newly added elements */

Dim count as Integer /* number of subsets whose SUM is < P

Dim i as Integer

Dim j as Integer

Dim S() as Integer /* Given set S -- not using array element 0 */

Dim c() as Integer

ReDim S(N+1)

ReDim c(M+1) /* indicates which indexes are in the current subset */

j = 1

count = 0

Sum = 0

c(1) = -1

for i= 1 to M /* loop sets initial subset and its SUM */

c(i) = i

SUM = SUM + S(i)

next

do while j > 0 AND SUM < P

count = count + 1

j = M

do while c(j) = (N - M + j) /* back up throwing elements out */

SUM = SUM - S(c(j]))

j = j - 1

loop

next_update: /* goto label */

SUM = SUM - S(c(j))

c(j) = c(j) + 1

tsum = S[c[j]];

for i = j+1 to M /* advance adding elements in */

c(i) = c(i-1) + 1

tsum = tsum + S(c(i))

next

if SUM + tsum >= P then

j = j - 1 /* backup 1 level */

if j <= 0 then

Exit Do

end if /* break out of while loop */

goto next_update

end if

SUM = SUM + tsum

loop

MsgBox "the number of subsets is," & count

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

simplest php form | 3 | 88 | |

Embarcadero C++ builder XE10.1 Berlin TRegistry declaration | 1 | 57 | |

Is online banking safe? | 11 | 115 | |

Cannot locate cell | 15 | 37 |

Simple Linear Regression

Course of the Month9 days, 12 hours left to enroll

Join the community of 500,000 technology professionals and ask your questions.