
BOOK: Stephens' C# Programming with Visual Studio 2010 24Hour Trainer
 This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24Hour Trainer by Rod Stephens; ISBN: 9780470596906 
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com



March 21st, 2013, 02:30 PM

Registered User


Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts


Multidimension array, find the max result from add values from bo
I have a multidimension array, i need to find the max result from add values from bottom to top.

March 21st, 2013, 02:34 PM


Wrox Author


Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts


Do you mean the total of all entries in the array? Either loop through it and add the values up, or look at LINQ. It can probably do this for you.

March 21st, 2013, 02:42 PM

Registered User


Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts


Not from the entire array, in some way should try many paths and select the one with the max value.

March 21st, 2013, 02:50 PM


Wrox Author


Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts


I think you're going to have to give a little more detail. For example, search the columns to find the one with the largest total? Find a path via adjacent entries from the upper left corner to the lower right corner to find the largest total? Follow some other kids of paths?

March 21st, 2013, 02:54 PM

Registered User


Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts


Ok, please see the following:
11 35 5
5 6 12
1 2 6
4 6
4
4+4+2+6+35 = ??

March 21st, 2013, 03:08 PM


Wrox Author


Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts


Are you trying to find the best path from the bottom to the top where each entry in the row above must have X coordinate differing by at most 1 from the row below?
If so, you can write a recursive routine to do this. It would add the current entry's value to a total and then recursively call itself to add up the paths above it for the various allowed X coordinate values.

March 21st, 2013, 03:14 PM

Registered User


Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts


Thank you for the answers, but I am quite new in C#. Actually I am studying your Visual C# 2010 24H.
Can you please tell me or give me some hint how to write a recursive routine to find the best path from the bottom to the top

March 21st, 2013, 03:30 PM


Wrox Author


Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts


Gotcha.
Here's the basic idea.
Code:
// Find the best total from entry [x, y] to the top.
BestTotal(int[,] array, int x, int y)
{
// Start with the value from this entry.
int total = array[x, y];
// If we're at the top, then we're done.
if (y == 0) return total;
// Try up and to the left.
int best_total = 0;
int test_total = 0;
if (x > 0)
{
test_total = BestTotal(array, x  1, y  1);
if (test_total > best_total) best_total = test_total;
}
// Try straight up.
if (x <= {maximum x coordinate in row y  1})
{
test_total = BestTotal(array, x, y  1);
if (test_total > best_total) best_total = test_total;
}
// Try up and to the right.
if (x + 1 <= {maximum x coordinate in row y  1})
{
test_total = BestTotal(array, x + 1, y  1);
if (test_total > best_total) best_total = test_total;
}
// Add the best value to our value and return it.
return total + best_value;
}
I'm not writing this in Visual Studio so I may have some of the details wrong.
You'll also need to figure out how many items row y  1 has.
And if you want to calculate the path and not just the total, you'll need to use another array to store the best values used at each step.
I hope that helps. Hopefully this can get you started.

The Following User Says Thank You to Rod Stephens For This Useful Post:


March 21st, 2013, 03:41 PM

Registered User


Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts


Thank you very much Rod. However, it is not an array, I made an example to make it simplerr... it is actually a pyramid. I will study and try your code, and I will let you know.
Kind regards,
Isidoro

March 21st, 2013, 03:50 PM


Wrox Author


Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts


I think the basic idea will help. The method recursively calls itself once for each possible path up the pyramid and then uses the best total it finds from those paths. Hopefully you can adapt it for your data structure.



