-2

I need to rotate table by 90,180,270 degrees and reset it to start at last option I must do it in switch but i have no idea how to do that because table must be in char. I found a lot of question about table in int but no one in char. I had this at this moment and no idea how to rotate it in char

using System;

namespace Table.ConsoleApp
{
    class Table
    {
        static void Main()
        {
            char[,] a = new char[6, 6]
            {
                {'#', '#', '#', '%', '%', '%'},
                {'#', '#', '#', '%', '%', '%'},
                {'#', '#', '#', '%', '%', '%'},
                {'*', '*', '*', '+', '+', '+'},
                {'*', '*', '*', '+', '+', '+'},
                {'*', '*', '*', '+', '+', '+'},
            };
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0;  j  < 6;  j++)
                {
                    Console.WriteLine("a[{0}, {1}] = {2}", i, j, a[i, j]);
                }
            }
            
      
        }
    }
}
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
Mequee
  • 1
  • 1
  • The tool-tip of the `[c#-4.0]` Tag you added says _"[...] Use this tag if your question specifically pertains to C# 4.0 specific features. The C# tag should be used if your question is not specific to C# 4.0 features. "_ – Stefan Wuebbe Nov 16 '22 at 16:19
  • 2
    You asked that yesterday and you still don't show what you have tried. – Ralf Nov 16 '22 at 16:22
  • "I must do it in switch but i have no idea how to do that because table must be in char" -- That's English only in the most technical meaning of the word. – Blindy Nov 16 '22 at 16:22
  • Yes. Im asking about this yesterday but I delete it in 3 minutes because that post look terrible so I dont know why u so angry at me. My english isn't the best I know but I'm trying. Next time I will upload my code with all my attemps to figure out my problems. I using this side first time so I don't know all the rules but next time it will be better. Thanks for all comments and I'm really gratefull for helping me with my problems. – Mequee Nov 16 '22 at 18:15
  • You asked this yesterday (or the day before). You have not deleted it - it's still there. Yeah, it looked terrible when you asked it, but it was edited very soon after you asked it (by me) and it looks OK now (after only very minor editing). Please don't ask the same question twice. I almost answered it there. – Flydog57 Nov 18 '22 at 00:27

2 Answers2

2

try this code:

char[,] a = new char[6, 6]
        {
            {'#', '#', '#', '%', '%', '%'},
            {'#', '#', '#', '%', '%', '%'},
            {'#', '#', '#', '%', '%', '%'},
            {'*', '*', '*', '+', '+', '+'},
            {'*', '*', '*', '+', '+', '+'},
            {'*', '*', '*', '+', '+', '+'},
        };
        Console.WriteLine("Rotate 90");
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                Console.Write(" " + a[5 - j, i]);
            }
            Console.WriteLine();
        }
        Console.WriteLine();
        Console.WriteLine("Rotate 180");

        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                Console.Write(" "+ a[5-i, 5-j]);
            }
            Console.WriteLine();
        }
        Console.WriteLine();
        Console.WriteLine("Rotate 270");
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                Console.Write(" " + a[j, 5-i ]);
            }
            Console.WriteLine();
        }

then the result:

enter image description here

Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
  • We don't know really if he needs to rotate the table or the output of the table. Things we might have found out seeing his attempts. – Ralf Nov 16 '22 at 17:08
  • @Ralf Yes, but I told the method of rotating the two-dimensional array. Now he can use the `Switch Case` and three functions. or to store in another array... But I said something he apparently has a problem with. – Hossein Sabziani Nov 16 '22 at 17:14
  • Thank you very much It's exactly what I need. Again I sorry for my english and next time I will try to explain what I need better and I send my attepms to it too. – Mequee Nov 16 '22 at 18:24
  • @Mequee No problem. If the answer to your question is correct, please tick the correct answer (green tick). – Hossein Sabziani Nov 16 '22 at 21:37
0

This is likely not going to be appropriate for your homework. But, then again, I don't do homework for other people.

Instead of rotating the array several times, I create a type that gets initialized with an array and provides a way to see it rotated at several angles.

I relaxed a few of your restrictions. You asked about a 6x6 array of char. This provides a generic solution for an MxN array of a generic type T instead (i.e. the array can be 5x7 if you want).

Your selection of characters made it hard to test, so I used this instead:

private char[,] _theArray = new char[,]
        {
            {'a', 'b', 'c', 'd', 'e', 'f'},
            {'A', 'B', 'C', 'D', 'E', 'F'},
            {'g', 'h', 'i', 'j', 'k', 'l'},
            {'G', 'H', 'I', 'J', 'K', 'L'},
            {'m', 'n', 'o', 'p', 'q', 'r'},
            {'M', 'N', 'O', 'P', 'Q', 'R'},
        };

But I also tested it with:

private char[,] _theArray = new char[,]
        {
            {'a', 'b', 'c', 'd', 'e', 'f'},
            {'A', 'B', 'C', 'D', 'E', 'F'},
            {'g', 'h', 'i', 'j', 'k', 'l'},
            {'G', 'H', 'I', 'J', 'K', 'L'},
        };

and:

private char[,] _theArray = new char[,]
        {
            {'a', 'b', 'c', 'd',},
            {'A', 'B', 'C', 'D',},
            {'g', 'h', 'i', 'j',},
            {'G', 'H', 'I', 'J',},
            {'m', 'n', 'o', 'p',},
            {'M', 'N', 'O', 'P',},
        };

So, it works with 6x6, 4x6 and 6x4 arrays.

Instead of specifying the rotation with an integer (1-4), it uses an enum:

public enum RotationAngle
{
    Rotate0,
    Rotate90,
    Rotate180,
    Rotate270,
}

It relies on a generic class named RotatableArray<T>:

public class RotatableArray<T>
{
    public int Width { get; }
    public int Height { get; }

    private T[,] _theArray;

    public RotatableArray(T[,] array)
    {
        _theArray = array;
        Width = array.GetLength(0);
        Height = array.GetLength(1);
    }

    public T this[int x, int y]
    {
        get
        {
            CheckParametersPlain(x, y);
            return _theArray[x, y];
        }
    }

    public T this[RotationAngle angle, int x, int y]
    {
        get
        {
            switch (angle)
            {
                case RotationAngle.Rotate0:
                    return this[x, y];
                case RotationAngle.Rotate90:
                    CheckParametersRotated(x, y);
                    return _theArray[Width - y - 1, x];
                case RotationAngle.Rotate180:
                    CheckParametersPlain(x, y);
                    return _theArray[Width - x - 1, Height - y - 1];
                case RotationAngle.Rotate270:
                    CheckParametersRotated(x, y);
                    return _theArray[y, Height - x - 1];
                default:
                    throw new ArgumentOutOfRangeException($"Parameter {nameof(angle)} must be one of {string.Join(", ", Enum.GetNames<RotationAngle>())}");
            }
        }
    }

    public int AdjustedWidth(RotationAngle angle)
    {
        if (angle == RotationAngle.Rotate0 || angle == RotationAngle.Rotate180)
        {
            return Width;
        }
        else
        {
            return Height;
        }
    }

    public int AdjustedHeight(RotationAngle angle)
    {
        if (angle == RotationAngle.Rotate0 || angle == RotationAngle.Rotate180)
        {
            return Height;
        }
        else
        {
            return Width;
        }
    }

    private void CheckParametersPlain(int x, int y)
    {
        if (x >= Width)
        {
            throw new ArgumentOutOfRangeException(nameof(x), $"Parameter must be less than the array Width ({Width})");
        }
        if (y >= Height)
        {
            throw new ArgumentOutOfRangeException(nameof(y), $"Parameter must be less than the array Height ({Height})");
        }
    }

    private void CheckParametersRotated(int x, int y)
    {
        if (y >= Width)
        {
            throw new ArgumentOutOfRangeException(nameof(y), $"After rotation, parameter must be less than the array Width ({Width})");
        }
        if (x >= Height)
        {
            throw new ArgumentOutOfRangeException(nameof(x), $"After rotation, parameter must be less than the array Height ({Height})");
        }
    }
}

An instance of a RotatableArray<T> gets initialized (/constructed) with a two-dimension array of T. That array is never mutated.

var rotArray = new RotatableArray<char>(_theArray);

Instead, the class relies on a three-parameter Indexed Property (also known as an Indexer https://learn.microsoft.com/en-Us/dotnet/csharp/programming-guide/indexers/) to provide a rotatable facade over the top of the non-mutable array it was initialized with.

Instead of rotating the array, you index over it (in two dimensions) passing in a RotationAngle value to describe how you want to be presented.

I tested this in a Windows Forms app (putting the output into a multi-line text box). The key code looks like:

private void ShowRotated (RotatableArray<char> array, RotationAngle angle)
{
    textBox1.Clear();
    var row = new List<char>();
    for (int i = 0; i < array.AdjustedWidth(angle); i++)
    {
        row.Clear();
        for (int j = 0; j < array.AdjustedHeight(angle); j++)
        {
            row.Add(array[angle, i, j]);
        }
        textBox1.Text += (string.Join(" | ", row) + Environment.NewLine);
    }
}

Note that it's basically the same as the code you provided. The difference is that rather than accessing:

array[i, j]

I'm accessing:

array[angle, i, j]

where angle is one of Rotate0, Rotate90, Rotate180, or Rotate270.

Using this array as an example:

private char[,] _theArray = new char[,]
        {
            {'a', 'b', 'c', 'd', 'e', 'f'},
            {'A', 'B', 'C', 'D', 'E', 'F'},
            {'g', 'h', 'i', 'j', 'k', 'l'},
            {'G', 'H', 'I', 'J', 'K', 'L'},
        };

These are the results I get:

Just showing the original array:

a | b | c | d | e | f
A | B | C | D | E | F
g | h | i | j | k | l
G | H | I | J | K | L

Showing it at Rotate0 results in the same thing:

a | b | c | d | e | f
A | B | C | D | E | F
g | h | i | j | k | l
G | H | I | J | K | L

At 90, you get:

G | g | A | a
H | h | B | b
I | i | C | c
J | j | D | d
K | k | E | e
L | l | F | f

and at 180:

L | K | J | I | H | G
l | k | j | i | h | g
F | E | D | C | B | A
f | e | d | c | b | a

and finally at 270:

f | F | l | L
e | E | k | K
d | D | j | J
c | C | i | I
b | B | h | H
a | A | g | G

This could easily be extended to provide for transposition, as well and inverting things both vertically and horizontally.

There should be enough code in this for you to do your assignment.

Flydog57
  • 6,851
  • 2
  • 17
  • 18