0

I am writing some code in C#. I have an array with values in it. As you can see in my code I have in my int[,] figuurI at row 1, column 1 number 1. In row 2, column 1 I have a 1 again. The number 1 will fill a box. The number 0 leaves the box empty. In this way I can create figures.

I have an function which make the figures rotate 90 degrees to the right. I should add that this happens when an arrow key is clicked.

Is it true that the values in the function are in an array? I want to store the values from the output of the function in an array. In that way I can run the function again if the user clicked again on the arrow key. Now it will rotate again 90 degrees to the right. Compared to the first array, the figure have now rotated 180 degrees and compared to the second array 90 degrees. How can I make this work?

using System;
class GFG {
     
    static void rotateRight(int[,] arr, int N)
    {

        for (int j = 0; j < N; j++)
        {
            for (int i = N - 1; i >= 0; i--)
            {
                Console.Write(arr[i, j] + " ");
            
            }
            Console.WriteLine();
        }
        Console.WriteLine("\n");
      }
     
  static void Main() {
                  
    int[,] figuurI = {{1, 0, 0, 0},
                      {1, 0, 0, 0},
                      {1, 0, 0, 0},
                      {1, 0, 0, 0} };
    rotateRight(figuurI, 4);

  }
}
8
  • Are you asking us how to rotate a 2D array, or are you asking us how to make the array be remembered between successive invokes of the method rather than resetting each time? Commented Apr 29, 2022 at 17:52
  • How to make the array remebered so I can rotate the remebered one again. Commented Apr 29, 2022 at 17:54
  • Declare it as a class level variable, not a local(to the method) level variable (unless youre never going to make your app more complex than it is, and you're just going to call rotate multiple times in Main). If you want to remember previous states (like an undo), consider a Stack<int[,]>? Commented Apr 29, 2022 at 17:55
  • Can you help me a little bit? How can I make it a class level variable? I'm sorry, i'm quite new to C#. Commented Apr 29, 2022 at 17:58
  • In C#, methods are named using PascalCase and acronyms longer than 2 chars are also PascalCased -> Http, not HTTP Commented Apr 29, 2022 at 17:58

1 Answer 1

1

I suppose you want something like this - make a new array and copy the rotation into it:

class Gfg {
     
    static int[,] RotateRight(int[,] arr)
    {
        int n = arr.GetLength(0);
        int[,] r = new int[n,n];

        for (int j = 0; j < n; j++)
            for (int i = n - 1; i >= 0; i--)
                r[j, n-i-1] = arr[i, j];
        
        return r;
    }
     
  static void Main() {
                  
    int[,] figuurI = {{1, 0, 0, 0},
                      {1, 0, 0, 0},
                      {1, 0, 0, 0},
                      {1, 0, 0, 0} };
      
    var figuur2 = RotateRight(figuurI);
      
  }
}

figuur2 is now:

{
  {1, 1, 1, 1},
  {0, 0, 0, 0},
  {0, 0, 0, 0},
  {0, 0, 0, 0} 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Last question ;) Is a static int global and a static void local?
Not when they're on a method declaration. static is sort of an odd little universe where there is only one of everything. It's actually a real pain in the ass because it defeats a lot of the concepts you'll want to learn when using an object oriented language. static void is used on a method to indicate that the method belongs to the static universe and returns no value. static int is again, belongs to static universe, returns an int. if used to declare a class level variable (a variable declared under the word class not inside a method) means it's available globally,and there's only 1
Strive to avoid using static if you can. You need it on your Main, but then your main can do var x = new Something(); x.DoThing(); to get out of the static universe and into "multiple different objects of the same type". I see many newbies making everything static "just to get something working" but then it comes round to bite them in the end.. if truly everything was static it would be more like a C program than a C#, and you lose the nuances of what makes an OO language useful and obvious able to model the real world if you run in a mode where there is only one of everything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.