Use of List<int[]>

Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
Reply

Join Date: Jul 2009
Posts: 4
Reputation: andreivanea is an unknown quantity at this point 
Solved Threads: 0
andreivanea andreivanea is offline Offline
Newbie Poster

Use of List<int[]>

 
0
  #1
Jul 3rd, 2009
Hello. I am writing a program that uses a List<T> object to retain a list of int[] objects.

  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:

  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):

  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 ...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,417
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 430
adatapost's Avatar
adatapost adatapost is online now Online
Nearly a Posting Maven

Re: Use of List<int[]>

 
0
  #2
Jul 3rd, 2009
What about list.Count? I think it should be theList.Count.
Last edited by adatapost; Jul 3rd, 2009 at 10:32 am.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: andreivanea is an unknown quantity at this point 
Solved Threads: 0
andreivanea andreivanea is offline Offline
Newbie Poster

Re: Use of List<int[]>

 
0
  #3
Jul 3rd, 2009
Originally Posted by adatapost View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,838
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 266
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Use of List<int[]>

 
0
  #4
Jul 3rd, 2009
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!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: andreivanea is an unknown quantity at this point 
Solved Threads: 0
andreivanea andreivanea is offline Offline
Newbie Poster

Re: Use of List<int[]>

 
0
  #5
Jul 3rd, 2009
Originally Posted by ddanbe View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,417
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 430
adatapost's Avatar
adatapost adatapost is online now Online
Nearly a Posting Maven

Re: Use of List<int[]>

 
0
  #6
Jul 3rd, 2009
Your code adding same reference of an array into the list. Create new reference for each iteration.

  1. theList.Add(numbers);
  2. numbers = new int[9];
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,838
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 266
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Use of List<int[]>

 
1
  #7
Jul 3rd, 2009
You're right! I overlooked the == for != operator
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,134
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: 551
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Use of List<int[]>

 
0
  #8
Jul 3rd, 2009
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,838
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 266
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Use of List<int[]>

 
0
  #9
Jul 3rd, 2009
Scott I used this to track errors but I am still busy... Recursive (at least for me) is not really my cup of tea.
  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. }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: andreivanea is an unknown quantity at this point 
Solved Threads: 0
andreivanea andreivanea is offline Offline
Newbie Poster

Re: Use of List<int[]>

 
0
  #10
Jul 3rd, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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