3
Reply

Help with pseudo code / C#

roger

roger

Apr 10 2008 2:16 PM
7.3k
I need help coding the following pseudo code into C#.

The 3 algorithms are search methods for a program which can look through a set of data to find the subsequence of values which add
up to the highest total value.

So for example with the data set.

10 -6 12 -17 9 8 -2

the algorithm(s) show that the "best" value was 17 from array index 4 to array index 5 if you go through it. (9 + 8)

10 - 6 = 4
10 - 6 + 12 = 16
10 - 6 + 12 - 17 = -1
10 - 6 + 12 - 17 + 9 = 8
10 - 6 + 12 - 17 + 9 + 8 = 16
10 - 6 + 12 - 17 + 9 + 8 - 2 = 14
-6 + 12 = 6
-6 +12 -17 = -11
-6 +12 -17 + 9 = -2
etc



For every possible start position // every array index
For every possible end position // rest of array from current start
{
Set subtotal to 0
For every value in subseq // between current start and end
Add profit value to subtotal
Update subseq info when subtotal exceeds current best total
}




For every possible start position...
Set subtotal to 0
For every possible end position...
{
Add end position’s profit value to subtotal
Update subseq if subtotal exceeds current best total
}





Set start position to 0, subtotal to 0
For every profit value... // index from 0 to end of array as end position
{
Add value to subtotal
Keep subseq info (start, end, total) if total exceeds current best
If total is less than 0,
set start position to next index and set total to 0
}

I need help creating C# versions of these methods which compile and run.

Thanks

Answers (3)