3

The User should choose the Dimension (1, 2). After the user chose the Dimension, an Array with the dimension should be created. After this the array will be filled randomly, sorted and printed. I have created Methods for this that take a reference of an one dimensional or two dimensional array.

My only problem is now that both arrays need to have the same name (arr) because the methods later only take arr as a parameter.

I have tried creating a var variable before choosing the array-dim. But with var I need to specify the type of the variable, so I cannot simply use var arr;

Is there any solution to my problem? I really don't know how to solve this problem.

In the End I want something like that:

        var arr;
        switch (arrDim)
        {
            case 1:
                arr = Fill1DArrayRandom();
                break;
            case 2:
                arr = Fill2DArrayRandom();
                break;
        }


        PrintArray(arr);

        switch (chosenAlgo)
        {
            case 1:
                Algorithms.InsertionSort.Sort(ref arr);
                break;
                ...

The Methods that take either an one or two dimensional array.

 static void PrintArray(int[] arr) ...
    
 static void PrintArray(int[,] arr) ...

 static bool IsSorted(ref int[] arr) ...
 
 static bool IsSorted(ref int[,] arr)
 {
      for (int i = 0; i < arr.Length - 1; i++)
           for (int j = 0; j < arr.Length - 1; j++)
                if (arr[i,j] > arr[i-1,j-1])
                     return false;
      return true;
 }
toni
  • 133
  • 1
  • 13
  • You can use directly the type [Array](https://learn.microsoft.com/en-US/dotnet/api/system.array?view=net-5.0) or with generic `List>`. – vernou Dec 21 '20 at 14:43
  • Other solution is to have a array with one dimension and simulate the multi-dimension. – vernou Dec 21 '20 at 14:46
  • @Vernou If I use the Array type, how would my methods know if its a 1D or 2D array? Because the parameter of the Method must be of type Array then. If thats the case I could not use the methods. – toni Dec 21 '20 at 14:50
  • The property [Array.Rank](https://learn.microsoft.com/fr-fr/dotnet/api/system.array.rank) return the dimension number. – vernou Dec 21 '20 at 17:07

1 Answers1

2

One ugly solution is to create an object, then cast it to int[] array like this:

var arr = new object();

// switch here
arr = new int[5] { 1 ,2,3, 4, 5};
Console.WriteLine((arr as int[])[0]);
arr = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Console.WriteLine((arr as int[,])[0, 0]);

and printing:

    private static void Print(object arr) 
        {
            if (arr.GetType() == typeof(int[,]))
            {
                int[,] myArr = arr as int[,];
                Console.WriteLine(myArr[0, 0]);
                // do stuff here with 2D array
            } 
            else 
            {
                int[] myArr = arr as int[];
                Console.WriteLine(myArr[0]);
                // do stuff here with 1D array
            }
        }

MestreDosMagros
  • 1,000
  • 5
  • 19
  • I still would have to specify every time I call a method if the array is 1D or 2D `PrintArray(arr as int[]);` or `PrintArray(arr as int[,]);` – toni Dec 21 '20 at 14:55
  • I could give the methods type object as parameter, but then I can not access the array like `arr.Length` or `arr[0] = 0` – toni Dec 21 '20 at 15:00
  • 1
    you can reverse cast this to object, then pass to your method and verify with an if clause if arr.GetType() is int[] or int[,] then execute you print casting back again to the right type to get access to array methods – MestreDosMagros Dec 21 '20 at 15:00
  • This would be such an ugly solution, I'd have to rewrite all my methods, is this really the only solution? – toni Dec 21 '20 at 15:03
  • 1
    i'm afraid so, you can't work with two distincts type of objects in the same way, you have to cast them to something else that you can work with. I updated the answer with the printing solution. – MestreDosMagros Dec 21 '20 at 15:09