Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour 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 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 21st, 2013, 02:30 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multi-dimension array, find the max result from add values from bo

I have a multi-dimension array, i need to find the max result from add values from bottom to top.
  #2 (permalink)  
Old March 21st, 2013, 02:34 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old March 21st, 2013, 02:42 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not from the entire array, in some way should try many paths and select the one with the max value.
  #4 (permalink)  
Old March 21st, 2013, 02:50 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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?
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #5 (permalink)  
Old March 21st, 2013, 02:54 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, please see the following:

11 35 5
5 6 12
1 2 6
4 6
4

4+4+2+6+35 = ??
  #6 (permalink)  
Old March 21st, 2013, 03:08 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #7 (permalink)  
Old March 21st, 2013, 03:14 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #8 (permalink)  
Old March 21st, 2013, 03:30 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
jminatel (March 26th, 2013)
  #9 (permalink)  
Old March 21st, 2013, 03:41 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #10 (permalink)  
Old March 21st, 2013, 03:50 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find item in one result in another result mphare XSLT 3 October 28th, 2011 10:51 AM
One dimension array. Wenggo C# 5 August 18th, 2006 02:39 AM
How do I add up values in an array? Lucy Classic ASP Basics 6 May 31st, 2005 05:26 AM
Find average for values stored in an array ja8261 VB.NET 2002/2003 Basics 2 October 29th, 2004 10:05 AM
Urgent please help to find Max ID and add 1 to it nhatrang71 Access 5 June 28th, 2004 10:24 PM



All times are GMT -4. The time now is 06:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.