LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Can you help with the correct translation of this code?


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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search for a code, and have the correct information auto fi Jared Excel Discussion (Misc queries) 4 November 9th 09 05:04 AM
#Value! error on code that should be correct? RHein Excel Discussion (Misc queries) 2 January 3rd 08 03:19 AM
Code translation please N.F[_2_] Excel Discussion (Misc queries) 0 July 6th 07 12:42 AM
Cell Translation Code [email protected] Excel Programming 3 April 11th 04 04:24 PM
XlPrevious Translation Code Steve Barwood Excel Programming 2 January 15th 04 03:31 PM


All times are GMT +1. The time now is 03:47 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"