Lustrzane odbicie macierzy A o dowolnym rozmiarze C#

0

Witam. Mam taki problem bo zrobilem lustrzane odbicie macierzy A ale o rozmiarze 3x3. Niestety nie wiem jak zrobic lustrzana mape dla macierzy o dowolnym rozmiarze n.

Tutaj podaje kod jaki mam dla macierzy 3x3.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] macierz = new int[3, 3];
Console.WriteLine("Podaj trzy cyfry pierwszego wiersza macierzy oddzielone przecinkiem:");
string w1 = Console.ReadLine();
Console.WriteLine("Podaj trzy cyfry drugiego wiersza macierzy oddzielone przecinkiem:");
string w2 = Console.ReadLine();
Console.WriteLine("Podaj trzy cyfry trzeciego wiersza macierzy oddzielone przecinkiem:");
string w3 = Console.ReadLine();

        string[] w1split = w1.Split(',');
        string[] w2split = w2.Split(',');
        string[] w3split = w3.Split(',');

        macierz[0, 0] = Convert.ToInt16(w1split[0]);
        macierz[1, 0] = Convert.ToInt16(w1split[1]);
        macierz[2, 0] = Convert.ToInt16(w1split[2]);
        macierz[0, 1] = Convert.ToInt16(w2split[0]);
        macierz[1, 1] = Convert.ToInt16(w2split[1]);
        macierz[2, 1] = Convert.ToInt16(w2split[2]);
        macierz[0, 2] = Convert.ToInt16(w3split[0]);
        macierz[1, 2] = Convert.ToInt16(w3split[1]);
        macierz[2, 2] = Convert.ToInt16(w3split[2]);

        Console.WriteLine("Podana macierz to:");
        Console.WriteLine(macierz[0, 0].ToString() + macierz[1, 0].ToString() + macierz[2, 0].ToString());
        Console.WriteLine(macierz[0, 1].ToString() + macierz[1, 1].ToString() + macierz[2, 1].ToString());
        Console.WriteLine(macierz[0, 2].ToString() + macierz[1, 2].ToString() + macierz[2, 2].ToString());
        Console.WriteLine("Jej odbicie lustrzane to:");
        Console.WriteLine(macierz[0, 2].ToString() + macierz[1, 2].ToString() + macierz[2, 2].ToString());
        Console.WriteLine(macierz[0, 1].ToString() + macierz[1, 1].ToString() + macierz[2, 1].ToString());
        Console.WriteLine(macierz[0, 0].ToString() + macierz[1, 0].ToString() + macierz[2, 0].ToString());
        Console.ReadKey();
    }
}

}

0

Użyj pętli

0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{

        int x, y;
        Console.WriteLine("Odbicie poziome lustrzane macierzy względem srodka.");
        for (; ; )
        {
            Console.WriteLine("Podaj rozmar macierzy:");
            int n = Convert.ToInt16(Console.ReadLine());
            if (n >= 2)
            {
                int[,] macierz = new int[n, n];
                Console.WriteLine("Macierz posiada {0} elementów. Podaj je wszystkie zaczynając od elementu 1,1 a kończac na {1},{1}",n*n,n);
                x = y = 0;
                for (int i = 0; i < n*n; i++)
                {
                    if (x == n) { y++; x = 0; }
                    macierz[x, y] = Convert.ToInt16(Console.ReadLine());
                    x++;
                }

                Console.Clear();
                x = y = 0;
                Console.WriteLine("Podana macierz to:");
                for(int i= 0;i<n*n;i++)
                {
                    if (x == n) { y++; x = 0; Console.WriteLine(); }
                    Console.Write(macierz[x, y]+" ");
                    x++;
                }
                int[,] macierzodbita = new int[n,n];
                x = y = 0;
                if (n % 2 == 0)
                {
                    int srodek = n / 2;
                    
                    for (int i = 0; i < srodek; i++)
                    {
                        for (int m = 0; m < n; m++)
                        {
                            if (x == n) { y++; x = 0; }
                            macierzodbita[x, y] = macierz[x, y];
                            x++;
                        }
                    }
                    x = 0;
                    y = 0;
                    int j = n-1;
                    int k = n-1;
                    for (int i = 0; i < srodek; i++)
                    {
                        for (int m = 0; m < n; m++)
                        {
                            if (x == n) { y++; x = 0; }
                            if (j < 0) { k--; j = 0; }
                            macierzodbita[j, k] = macierzodbita[y, x];
                            x++;
                            j--;
                        }
                    }
                    x = y = 0;
                    Console.WriteLine("Odbita macierz to:");
                    for (int i = 0; i < n * n; i++)
                    {
                        if (x == n) { y++; x = 0; Console.WriteLine(); }
                        Console.Write(macierzodbita[x, y] + " ");
                        x++;
                    }
                }
                else
                {

                }
            }
            else
            {
                Console.WriteLine("Podana macierz jest za mała.");
            }
            Console.ReadKey();
        }
    }
}

}

Tu podaje program ale oblicza mi zle macierz odbita. Moze mi ktos pomoc? Potrzebuje to zeby zaliczyc przedmiot na studiach...

0

A może tak po ludzku zrobisz?

for(int y=0;y<n;++y) for(int x=0;x<n;++x) macierz[x,y]=Convert.ToInt16(Console.ReadLine());

W pozostałych miejscach również.

1 użytkowników online, w tym zalogowanych: 0, gości: 1