Arrays Matrix 90 Grad drehen im C

bimdot

Grünschnabel
Ich hab es bis jetzt geschafft 4 matrixen zu machen aber ich will das sie in eine reihe sind und danach sollen sie sich 4 mal sich drehen, wie im foto:
9lhn9z.jpg

Und so sieht mein Code aus:

C++:
#include <stdio.h>

int main()
{
    char matrixeins[4][4]={
        {'-','-','-','-'},
        {'-','o','-','-'},
        {'o','o','o','-'},
        {'-','-','-','-'},
    };

    char matrixzwei[4][4]={
        {'-','-','-','-'},
        {'-','o','o','-'},
        {'o','o','-','-'},
        {'-','-','-','-'},
    };

    char matrixdrei[4][4]={
        {'-','-','-','-'},
        {'-','o','-','-'},
        {'-','o','-','-'},
        {'-','o','o','-'},
    };

    char matrixvier[4][4]={
        {'-','-','o','-'},
        {'-','-','o','-'},
        {'-','-','o','-'},
        {'-','-','o','-'},
    };

    int x,y;

    for(x=0; x<=3; x++){
        for(y=0; y<=3; y++){
            printf("%c ", matrixeins[x][y]);
        }
        printf("\n");
    }

    for(x=0; x<=3; x++){
        for(y=0; y<=3; y++){
            printf("%c ", matrixzwei[x][y]);
        }
        printf("\n");
    }

    for(x=0; x<=3; x++){
        for(y=0; y<=3; y++){
            printf("%c ", matrixdrei[x][y]);
        }
        printf("\n");
    }

    for(x=0; x<=3; x++){
        for(y=0; y<=3; y++){
            printf("%c ", matrixvier[x][y]);
        }
        printf("\n");
    }

    return 0;
}
 
Hi und Willkommen bei tutorials.de

warum nicht alle 4 Matrizen auch in ein Array zusammenfassen?
C++:
#include <stdio.h>

int main()
{
	char matrix[4][4][4] =
	{
		{
			{'-','-','-','-'},
			{'-','o','-','-'},
			{'o','o','o','-'},
			{'-','-','-','-'}
		},
		{
			{'-','-','-','-'},
			{'-','o','o','-'},
			{'o','o','-','-'},
			{'-','-','-','-'}
		},
		{
			{'-','-','-','-'},
			{'-','o','-','-'},
			{'-','o','-','-'},
			{'-','o','o','-'}
		},
		{
			{'-','-','o','-'},
			{'-','-','o','-'},
			{'-','-','o','-'},
			{'-','-','o','-'}
		},
	};

	int n, x, y;
	for(n = 0; n <= 3; n++)
	{
		for(x = 0; x <= 3; x++)
		{
			for(y = 0; y <= 3; y++)
			{
				printf("%c ", matrix[n][x][y]);
			}
			printf("\n");
		}
	}
	return 0;
}

Eine Matrix mit zwei Schleifen in eine andere kopieren kannst du vermutlich,
und zum Drehen muss man nur noch die Reihenfolge, wie die Indexe durchlaufen werden,
etwas ändern. In eine Funktion gepackt:
C++:
void rotateright(const char matrixold[4][4], char matrixnew[4][4])
{
	int x, y;
	for(y = 0; y <= 3; y++)
	{
		for(x = 0; x <= 3; x++)
		{
			matrixnew[x][3 - y] = matrixold[y][x];
		}
	}
}
 
Die matrizen sollen in eine reihe sein, so hab es geschafft es in eine reihe zu machen aber jetzt um 90 grad zu drehen:

C++:
#include <stdio.h>

int main()
{
    char matrixeins[4][4]={
        {'-','-','-','-'},
        {'-','o','-','-'},
        {'o','o','o','-'},
        {'-','-','-','-'},
    };

    char matrixzwei[4][4]={
        {'-','-','-','-'},
        {'-','o','o','-'},
        {'o','o','-','-'},
        {'-','-','-','-'},
    };

    char matrixdrei[4][4]={
        {'-','-','-','-'},
        {'-','o','-','-'},
        {'-','o','-','-'},
        {'-','o','o','-'},
    };

    char matrixvier[4][4]={
        {'-','-','o','-'},
        {'-','-','o','-'},
        {'-','-','o','-'},
        {'-','-','o','-'},
    };

    int x,y;

for(x=0; x<=3; x++){

        for(y=0; y<=3; y++){
            printf("%c ", matrixeins[x][y]);
        }
        printf(" ");

        for(y=0; y<=3; y++){
            printf("%c ", matrixzwei[x][y]);
        }
        printf(" ");

        for(y=0; y<=3; y++){
            printf("%c ", matrixdrei[x][y]);
        }
        printf(" ");

        for(y=0; y<=3; y++){
            printf("%c ", matrixvier[x][y]);
        }
        printf("\n");
}

    return 0;
}
 
Da du auf die bestehende Antwort überhaupt nicht eingehst, was passt nicht?
Code zu schwer verständlich?
 
Zurück