943,735 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 3255
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 3rd, 2009
0

Use of List<int[]>

Expand Post »
Hello. I am writing a program that uses a List<T> object to retain a list of int[] objects.

C# Syntax (Toggle Plain Text)
  1. private int[] numbers = new int[9];
  2. private List<int[]> theList = new List<int[]>();

It's basicaly a backtracking algorithm that constructs the numbers object, which contains a permutatio of the numbers 1..9, and after every complete construction adds the solution to the list:

C# Syntax (Toggle Plain Text)
  1. list.Add(numbers);

I checked the construction and it works fine, but if I want to read/print/use the list after all the solutions are added, it only contains the number 9, in every entry of the integer vector.

My question is: why isn't the List object saving the exact state added to each of it's elements? Here is my code (I know it's messy, but it's experimental):

C# Syntax (Toggle Plain Text)
  1. private int[] numbers = new int[9];
  2. private List<int[]> theList = new List<int[]>();
  3.  
  4. public Executor()
  5. {
  6. }
  7.  
  8. private void BackTrack(int p)
  9. {
  10. int i;
  11. for (i = 1; i <= 9; i++)
  12. {
  13. numbers[p] = i;
  14. if (CheckCorrectness(p))
  15. {
  16. if (p == 8) AddSolution();
  17. else BackTrack(p + 1);
  18. }
  19. }
  20. }
  21.  
  22. private bool CheckCorrectness(int p)
  23. {
  24. int i;
  25. for (i = 0; i < p; i++)
  26. if (numbers[i] == numbers[p]) return false;
  27. return true;
  28. }
  29.  
  30. private void AddSolution()
  31. {
  32. theList.Add(numbers);
  33. //uncomment line to that it generates the right solution
  34. //for (int i = 0; i < 9; i++) Console.Out.Write(numbers[i] + " ");
  35. }
  36.  
  37. public void Print()
  38. {
  39. for (int i = 0; i < list.Count; i++)
  40. {
  41. for (int j = 0; j < 9; j++)
  42. {
  43. Console.Out.Write(theList[i][j].ToString() + " ");
  44. }
  45. Console.Out.WriteLine();
  46. }
  47. Console.Out.WriteLine();
  48. }

It should print:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
and so on, but it prints:
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9

It starts execution with BackTrack(0) and the it calls the Print() method.
Last edited by andreivanea; Jul 3rd, 2009 at 10:20 am. Reason: Forgot something ...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andreivanea is offline Offline
4 posts
since Jul 2009
Jul 3rd, 2009
0

Re: Use of List<int[]>

What about list.Count? I think it should be theList.Count.
Last edited by adatapost; Jul 3rd, 2009 at 10:32 am.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 3rd, 2009
0

Re: Use of List<int[]>

Click to Expand / Collapse  Quote originally posted by adatapost ...
What about list.Count? I think it should be theList.Count.
The Count is 362880. It successfully adds all the permutations, but when the algorithm is over, theList is a list of 362880 {9 9 9 9 9 9 9 9 9} elements.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andreivanea is offline Offline
4 posts
since Jul 2009
Jul 3rd, 2009
0

Re: Use of List<int[]>

Lines 16 and 17 of your code never get executed, because the function on line 22 always returns false. Play computer like I did! It's fun! You will see why your int array fills up with all nines!
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jul 3rd, 2009
0

Re: Use of List<int[]>

Click to Expand / Collapse  Quote originally posted by ddanbe ...
Lines 16 and 17 of your code never get executed, because the function on line 22 always returns false. Play computer like I did! It's fun! You will see why your int array fills up with all nines!
You're wrong, the function executes fine. It's easy to see if you uncomment the code at line 34, or simply if you add a breake point at line 35, to see the full array.

The problem doesn't seem that the problem is at the generation of the permutation, but rather at the saving/storing section, envolvind the List<int[]> object.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andreivanea is offline Offline
4 posts
since Jul 2009
Jul 3rd, 2009
0

Re: Use of List<int[]>

Your code adding same reference of an array into the list. Create new reference for each iteration.

C# Syntax (Toggle Plain Text)
  1. theList.Add(numbers);
  2. numbers = new int[9];
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 3rd, 2009
1

Re: Use of List<int[]>

You're right! I overlooked the == for != operator
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jul 3rd, 2009
0

Re: Use of List<int[]>

Andre -- upload your complete class here so we can look at it. What you posted had a slight compilation mistake or you were missing a member of the class when you posted code. That could be a make or break piece of information for solving your issue.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 3rd, 2009
0

Re: Use of List<int[]>

Scott I used this to track errors but I am still busy... Recursive (at least for me) is not really my cup of tea.
c# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApplication1
  5. {
  6. class Program
  7. {
  8. private static int[] numbers = new int[9];
  9. private static List<int[]> theList = new List<int[]>();
  10.  
  11. static void Main(string[] args)
  12. {
  13. BackTrack(0);
  14. Print();
  15. Console.ReadKey();
  16. }
  17.  
  18. private static void BackTrack(int p)
  19. {
  20. int i;
  21. for (i = 1; i <= 9; i++)
  22. {
  23. numbers[p] = i;
  24. if (CheckCorrectness(p))
  25. {
  26. if (p == 8) AddSolution();
  27. else BackTrack(p + 1);
  28. }
  29. }
  30. }
  31.  
  32. private static bool CheckCorrectness(int p)
  33. {
  34. int i;
  35. for (i = 0; i < p; i++)
  36. if (numbers[i] == numbers[p]) return false;
  37. return true;
  38. }
  39.  
  40. private static void AddSolution()
  41. {
  42. theList.Add(numbers);
  43. //uncomment line to that it generates the right solution
  44. //for (int i = 0; i < 9; i++) Console.Out.Write(numbers[i] + " ");
  45. }
  46.  
  47. public static void Print()
  48. {
  49. for (int i = 0; i < theList.Count; i++)
  50. {
  51. for (int j = 0; j < 9; j++)
  52. {
  53. Console.Out.Write(theList[i][j].ToString() + " ");
  54. }
  55. Console.Out.WriteLine();
  56. }
  57. Console.Out.WriteLine();
  58. }
  59.  
  60. }
  61. }
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Jul 3rd, 2009
0

Re: Use of List<int[]>

Solved it! It was the List<T> object. adatapost was partialy wright: I was adding the same instance of that object (but why where there only 9s?), but using new to generate new instances determins the loss of the numbers allready generated in the numbers object (it resets them with 0).

What did the trick was rewriting the AddSolution function, like this:

C# Syntax (Toggle Plain Text)
  1. private void AddSolution()
  2. {
  3. theList.Add(new int[9]);
  4. for (int i = 0; i < 9; i++) theList[theList.Count - 1][i] = numbers[i];
  5. }

Thank you all for your help.
Last edited by andreivanea; Jul 3rd, 2009 at 5:30 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andreivanea is offline Offline
4 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Check Whether Button Clicked or not - Please Help
Next Thread in C# Forum Timeline: C# help pls....!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC