What about list.Count? I think it should be theList.Count.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
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!
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
Your code adding same reference of an array into the list. Create new reference for each iteration.
theList.Add(numbers);
numbers = new int[9];
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
You're right! I overlooked the == for != operator:$
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
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.
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Scott I used this to track errors but I am still busy... Recursive (at least for me) is not really my cup of tea.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
private static int[] numbers = new int[9];
private static List<int[]> theList = new List<int[]>();
static void Main(string[] args)
{
BackTrack(0);
Print();
Console.ReadKey();
}
private static void BackTrack(int p)
{
int i;
for (i = 1; i <= 9; i++)
{
numbers[p] = i;
if (CheckCorrectness(p))
{
if (p == 8) AddSolution();
else BackTrack(p + 1);
}
}
}
private static bool CheckCorrectness(int p)
{
int i;
for (i = 0; i < p; i++)
if (numbers[i] == numbers[p]) return false;
return true;
}
private static void AddSolution()
{
theList.Add(numbers);
//uncomment line to that it generates the right solution
//for (int i = 0; i < 9; i++) Console.Out.Write(numbers[i] + " ");
}
public static void Print()
{
for (int i = 0; i < theList.Count; i++)
{
for (int j = 0; j < 9; j++)
{
Console.Out.Write(theList[i][j].ToString() + " ");
}
Console.Out.WriteLine();
}
Console.Out.WriteLine();
}
}
}
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661