Recursion In C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 1
Reputation: staplesauce is an unknown quantity at this point 
Solved Threads: 0
staplesauce staplesauce is offline Offline
Newbie Poster

Recursion In C#

 
0
  #1
Feb 19th, 2009
The RemoveMin class should remove the first occurrence of the smallest element from the input list. Despite what the inputs are I keep getting the same NullReference error from having 'rem = null'. I don't know if I'm using recursion right or not. I have a feeling the starting reference for the ConsList may not be right. This was a homework assignment but it was due a couple days ago. I just want to understand for future references what I need to do. Anything pointers or examples would be appreciated. Here is the line of code giving me problems.
  1. public static ConsList<int> RemoveMin(ConsList<int> list, out int min)
  2. {
  3. ConsList<int> r;
  4. min = list.Head;
  5. if (list.Tail.Head > min)
  6. {
  7. r = new ConsList<int>(list.Head, RemoveMin(list.Tail, out min));
  8. return r;
  9. }
  10. else
  11. {
  12. r = new ConsList<int>(min, RemoveMin(list.Tail, out min));
  13. return r;
  14. }
  15. }

Here is the class that implements RemoveMin. 'rem' keeps causing a null reference. This class shouldn't need any editing.

  1. private ConsList<int> SelectionSort(ConsList<int> list)
  2. {
  3. if (list == null)
  4. {
  5. return list;
  6. }
  7. else
  8. {
  9. int min;
  10. ConsList<int> rem = RemoveMin(list, out min);
  11. return new ConsList<int>(min, SelectionSort(rem));
  12. }
  13. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,231
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 576
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Recursion In C#

 
0
  #2
Feb 19th, 2009
Care to post the full code unit so I can load it up in a debugger and test without putting it back together?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: danielernesto is an unknown quantity at this point 
Solved Threads: 1
danielernesto danielernesto is offline Offline
Newbie Poster

Re: Recursion In C#

 
0
  #3
Feb 19th, 2009
The problem might be in the creation of the object.
Passing a recursive parameter can be a trouble, becouse every single call to the function will create a different object an you're using a type out value wich has no value.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC