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

private int[] numbers = new int[9];
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:

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

private int[] numbers = new int[9];
        private List<int[]> theList = new List<int[]>();

        public Executor()
        {
        }

        private 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 bool CheckCorrectness(int p)
        {
            int i;
            for (i = 0; i < p; i++)
                if (numbers[i] == numbers[p]) return false;
            return true;
        }

        private 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 void Print()
        {
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Console.Out.Write(theList[i][j].ToString() + " ");
                }
                Console.Out.WriteLine();
            }
            Console.Out.WriteLine();
        }

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.

Recommended Answers

All 10 Replies

What about list.Count? I think it should be theList.Count.

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.

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!

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.

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

theList.Add(numbers);
    numbers = new int[9];

You're right! I overlooked the == for != operator:$

commented: It's called greatness!! +5

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 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();
        }

    }
}

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:

private void AddSolution()
 {
            theList.Add(new int[9]);
            for (int i = 0; i < 9; i++) theList[theList.Count - 1][i] = numbers[i];
}

Thank you all for your help.

1. @ the OP. Initialize the "int i" in the for loop
2. Why not declare the numbers 1-9 on your own?

Example:

int[] intArray = { 1,2,3,4,5,6,7,8,9 };
commented: Right! +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.