0

I have a recursion assignment where I have to input into console the following data:

  • On the first line a natural number equal with the number of tiles I have to input on the following lines (in my example the first number is 6).
  • On the following lines the domino tiles in the following order:

1 2

1 3

3 4

2 3

3 5

4 5

  • On the last line another number representing the number of tiles that needs to be returned on each separate line (in my example the number is equal with 3).

With this data I have to display all possible combinations of pairs. For each separate line the number the second number from the first pair has to be equal with the first number of the following pair and so on. I had a hint for this assignment where I have to declare an intermediary list that is equal with the function (using recursion from the start) but when I'm trying to run the code it gives me a null exception for the intermediaryList.

In the first instance I read the data as a 2d array but settled with a simple string array where. This is my code so far, could you help me with a suggestion, how to avoid the null exception? (sorry if I missed something from the explanation, this is my second post here so far and thank you in advance). Also I have to mention that I'm allowed to use just "using System;" Not allowed to use Linq or anything else.

enter code here

static string[] ReadDominoTiles(int n)
        {
            string[] input = new string[n];
            for (int i = 0; i < n; i++)
            {
                input[i] = Console.ReadLine();
            }

            return input;
        }

        static string[] DominoSolution(string[] dominoTiles, int numberOfPairs)
        {
            string[] finalList = new string[numberOfPairs];

            if (numberOfPairs == 1)
            {
                return finalList;
            }

            string[] intermediaryList = DominoSolution(dominoTiles, numberOfPairs - 1);
            for (int i = 0; i < intermediaryList.Length; i++)
            {
                for (int j = 0; j < dominoTiles.Length; j++)
                {
                   // This is where I get the nullref exception, every value from intermediaryList is null
                    if (intermediaryList[i] != dominoTiles[j] && intermediaryList[j][1] == dominoTiles[j][0])
                    {
                        finalList[j] += intermediaryList[j] + " " + dominoTiles[j];
                    }
                }
            }

            return finalList;
        }

        static void Main(string[] args)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            string[] dominoTiles = ReadDominoTiles(n);
            int numberOfPairs = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(DominoSolution(dominoTiles, numberOfPairs));
        }
  • 1
    You have two numbers on each line so you have to split the numbers before calling Convert. The Convert method only want to see one number at a time. – jdweng Feb 07 '22 at 15:31
  • 1
    A Console.WriteLine of a string-array will print "System.String[]" - you will need to print each item using a loop – Hans Kesting Feb 07 '22 at 15:34
  • I'm using Convert.ToInt32 just for the number of lines and the number of pairs (first and last input) – Bogdan Muscari Feb 07 '22 at 15:34
  • You say you get a nullref exception (but don't mention where...), do you know about [this](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it)? – Hans Kesting Feb 07 '22 at 15:42
  • In the first loop from the recursive function. I will edit the post with the info, sorry for missing that. – Bogdan Muscari Feb 07 '22 at 15:44
  • As recurive call you use 1 less "numberOfPairs" but you pass the whole list of dominoes - I think you should take one out and combine this with all returned combinations. – Hans Kesting Feb 07 '22 at 16:46

1 Answers1

1

This is where I get the nullref exception, every value from intermediaryList is null

That's correct, you call the function recursively after doing exactly zero work, besides adding a recursion termination condition which returns an array of nulls. So the first time you come out of your recursion you have in intermediaryList a number of null elements, so when you pretend they're strings and try to get intermediaryList[j][1] you'll get a null reference exception.

As to the solution, it's not exactly clear what's with all the arrays you're allocating. Use a List<> with an actual type and do your work properly. Though I hate to break it to you but if I understand your assignment correctly the solution will use backtracking and combinatorics, which is much more code and much better structured than what you have here.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • The OP said that only classes from "System" are allowed - thus no LINQ, no generics – Hans Kesting Feb 07 '22 at 16:44
  • You don't even really need a list, you already know the maximum size result you can return (the total number of input elements). So we're back to what's with all the allocations? One array allocation per function call makes it exponential, that's terrible to a level I can't even describe. A list gives you the better API to work with though (`Add`/`RemoveAt`) – Blindy Feb 07 '22 at 16:51