Jump to content
NEurope
Sign in to follow this  
ZeldaFreak

Anyone know any c#?

Recommended Posts

Hi all I was wondering if anyone heres knows c#.

 

I have been having trouble converting 2 algorithms in pseudo code into c#, I would love some help so just post here and I will thank you.

 

First

 

For every possible start position

Set subtotal to 0

For every possible end position

{

Add end position's profit value to subtotal

Update subsequence if subtotal exceeds current best total

}

 

Second

 

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 zero

}

 

Any help would be truly appreciated.

Share this post


Link to post
Share on other sites

Sorry to jump in here and go off topic, but was going to ask anyone earlier if they've got any experience in coding games with xna studio, or in c#? As was wondering if it's possible to get anything created in it with people from here.

 

Good luck with your thread though, and I hope someone can help you out with your problem.

Share this post


Link to post
Share on other sites
Hi all I was wondering if anyone heres knows c#.

 

I have been having trouble converting 2 algorithms in pseudo code into c#, I would love some help so just post here and I will thank you.

 

First

 

For every possible start position

Set subtotal to 0

For every possible end position

{

Add end position's profit value to subtotal

Update subsequence if subtotal exceeds current best total

}

 

Second

 

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 zero

}

 

Any help would be truly appreciated.

 

 

I can't code in c# (I work mainly in vb.net and in stats packages) but it seems to me as if it is trivial usage of for loops and if/elseif/else clauses. Perfect them and you'll be fine.

Share this post


Link to post
Share on other sites

You really need access to the MSDN Library, which, if this is Uni work, you should be able to get access to somehow, here is a section lifted from MSDN 2005 (a basic if-else statement):

 

if (condition)

statement1

[else

statement2]

 

Arguments

 

condition

Required. A Boolean expression. If condition is null or undefined, condition is treated as false.

 

statement1

Optional. The statement to be executed if condition is true. Can be a compound statement.

 

statement2

Optional. The statement to be executed if condition is false. Can be a compound statement.

 

Remarks

 

It is generally good practice to enclose statement1 and statement2 in braces ({}) for clarity and to avoid inadvertent errors.

 

Hope this helps :)

Share this post


Link to post
Share on other sites

Its just the second algorithm that I'm having trouble with now. Its giving the right ending and right total but totally the wrong first value.

 

this is by current situation:

 

public static void Search3(double[] data, out int bestStart,

out int bestEnd, out double bestTotal, out int loops)

{

bestTotal = 0;

bestStart = 0;

bestEnd = 0;

loops = 0;

/// TODO - put your search code here

int start = 0;

double subTotal = 0;

for (int i = start; i < data.Length; i++)

{

subTotal += data;

loops++;

if (subTotal > bestTotal)

{

bestStart = start;

bestEnd = i;

bestTotal = subTotal;

}

 

if (subTotal < 0)

{

start = start + 1;

subTotal = 0;

}

}

}

 

Just its printing out the wrong bestStart position, however I'm getting the right bestEnd and bestTotal values correct.

 

I've done it I'm the man yahhhhhhooooooooooo, now I only have one piece of work to do which I'm 50% complete.

Share this post


Link to post
Share on other sites

Hi zeldafreak

 

how did you solve the beststart problem, because im going through the same thing.

 

will appreciate your input.

Share this post


Link to post
Share on other sites

Unfortunately Zeldafreak was banned a long time ago (note this thread is from 2007 and he has a strike through his name), but just on the off chance that you're asking a genuine question, I'll leave this open in case anyone can help you.

Share this post


Link to post
Share on other sites

Yeh, if it's a genuine problem, I'd be happy to take a look at the code if you post it. ZeldaFreak already solved his own problem back at the time..

Share this post


Link to post
Share on other sites

I'm not familiar with C#, but I am familiar with Java and a little bit of C/C++, and for what you're doing, there really isn't any difference.

 

Anyway, if I understand what you're trying to do correctly, the problem is that you're not setting start to the next index value, but instead, just increasing it by 1.

 

Try this:

 

if (subTotal < 0)
{
   subTotal = 0;
   start = i+1;		
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

×