Solved

Code Translation

Posted on 2004-09-30
3
191 Views
Last Modified: 2010-05-19
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;
0
Comment
Question by:tli99
3 Comments
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 12198314
Try this (I have not tested this but it should be mostly there)

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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroMAx challenge 20 77
copyEndy  challenge 15 58
firstChar challenge 13 86
Java Loop 6 25
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

746 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now