Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() I've try to translate the code below to VBA. Can someone help to correct the rest of the code? C code is provided. TIA John 'Modification of algorithm given in "Combinatorial Algorithms" 'by Reingold, Nievergelt, and Deo '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; Sub Test() Dim N%, M%, P& Dim S() Dim c() Dim SUM&, tsum&, count&, I, J N = 20 M = 5 P = 36 ReDim S(N + 1) ReDim c(M + 1) SUM = 0 count = 0 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]; ' } c(0) = -1 For I = 1 To M 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; ' } While (J 0 And SUM < 0) count = count + 1 J = M While (c(J) = N - M + J) SUM = SUM - S(c(J)) J = J - 1 'next_update: /* goto label */ 'SUM = SUM - S[c[j]]; 'c[j] = c[j] + 1; 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]]; ' } tsum = S(c(J)) For I = J + 1 To M 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; } If (SUM + tsum = P) Then J = J - 1 If (J <= 0) Then Exit Sub SUM = SUM + tsum 'Print "the number of subsets is,"; count; Debug.Print count End Sub |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
How to search for a code, and have the correct information auto fi | Excel Discussion (Misc queries) | |||
#Value! error on code that should be correct? | Excel Discussion (Misc queries) | |||
Code translation please | Excel Discussion (Misc queries) | |||
Cell Translation Code | Excel Programming | |||
XlPrevious Translation Code | Excel Programming |