Działania na tablicach... C

0

Witam, mam pewien problem. Mój program odczytuje z nonatnika pewną tabele (umiem), zapisuje do nowego pliku (mam), robi jej odbicie lustrzane (mam) ale nie mam pomysłu na to jak ją obrócić o 90stopni.
To co stworzyłem dla tabeli np.:
0 0 0 1 1 1
0 0 0 2 2 2
3 3 3 4 4 4
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 0 0 0
1 1 1 1 1 1
0 0 0 1 1 1
5 5 5 4 3 2

#include <stdio.h>
#include <stdlib.h>
#define WYS 9
#define SZER 6

int main(int argc, char *argv[])
{ 
     srand (time (0));
     int i,j;
	 int tab[WYS][SZER];
     FILE *fp;
     FILE *fpt;
     FILE *fptr;
     FILE *f;
     FILE *file;
     while ((fp=fopen("test.txt", "r"))==NULL) 
     {
           printf ("Nie mozna otworzyc pliku test.txt do zapisu!!!\n");
     }
     while ((fpt=fopen("test2.txt", "w"))==NULL) 
     {
           printf ("Nie mozna otworzyc pliku wykres.csv do zapisu!!!\n");
     }
     while ((f=fopen("test3.txt", "w"))==NULL) 
     {
           printf ("Nie mozna otworzyc pliku test.txt do zapisu!!!\n");
     }
     while ((fptr=fopen("histogram.csv", "w"))==NULL) 
     {
          printf ("Nie mozna otworzyc pliku histogram.csv do zapisu!!!\n");
     }
     while ((file=fopen("obrot.txt", "w"))==NULL) 
     {
          printf ("Nie mozna otworzyc pliku obrot.txt do zapisu!!!\n");
     }
     for (j=0; j<=WYS-1; ++j)
     {
         for (i=0; i<=SZER-1; ++i)
         {
             fscanf (fp, "%d", &tab[j][i]);                                // Pobiera wartości z pliku
					fprintf(fpt, "%d ", tab[j][i]);                        // Zapisuje te wartości do nowego pliku
                            fprintf(f, "%d ", tab[j][i]+(rand ()%5));      // Zapisuje wartości losowe w nowym pliku
                                    fprintf (fptr, "%d\n", tab[j][i]);     // Zapisuje wartości w pliku csv     
                                                                           
         }
         fprintf (fpt, "\n");
         fprintf (f, "\n");
     }
     for (j=0; j<=WYS-1; ++j)
     {
         for (i=SZER-1; i>=0; i--)
         {
             fscanf (fp, "%d", &tab[j][i]);
                    fprintf(file, "%d ", tab[j][i]); 
         }
         fprintf (file, "\n");
     }
     fclose (fp);
     fclose (fpt);
     fclose (f);
     fclose (fptr);
     fclose (file);
     system("PAUSE");
     return 0;
}

Będę wdzięczny za pomoc

0

Niech kolumna stanie się wierszem - czyli innymi słowy zamieniasz i z j

0

w ten sposób?
.
.
.

 for (j=0; j<=SZER-1; ++j)
     {
         for (i=WYS-1; i>=0; i--)
         {
             fscanf (fp, "%d", &tab[j][i]);
                    fprintf(file, "%d ", tab[j][i]); 
         }
         fprintf (file, "\n");
     }

.
.
.
i otrzymuje :
0 0 0 1 1 1 0 0 0
3 3 3 2 2 2 0 0 0
0 0 0 4 4 4 3 3 3
0 0 0 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 0 0 0

0
#include <stdio.h>
#include <stdlib.h>

void rotate_90( void * p_destination_matrix,  void * p_source_matrix, int source_row_count, int source_column_count )
{
    // rzutowanie, abym mogl operowac tablicami 2d dowolnych wymiarow
    int ( * destination_matrix)[ source_row_count ] = (int ( * )[ source_row_count ])( p_destination_matrix );
    int ( * source_matrix)[ source_column_count ] = (int ( * )[ source_column_count ])( p_source_matrix );

    int desitination_row_index = 0;
    int desitination_column_index = 0;

    int source_row_index = 0;
    for ( source_row_index = 0; source_row_index < source_row_count; ++source_row_index ) {

        int source_column_index = 0;
        for ( source_column_index = 0; source_column_index < source_column_count; ++source_column_index ) {
            // te dwa przypisania mozna sie pozbyc kosztem czytelnosci kodu
            desitination_row_index = source_column_index;
            desitination_column_index = source_row_index;

            destination_matrix[ desitination_row_index ][ desitination_column_index ] =
                    source_matrix[ source_row_index ][ source_column_index];
        }
    }
}

void print_matrix( void * p_matrix, int row_count, int column_count )
{
    int ( * matrix )[column_count] = static_cast<int ( * )[ column_count ]>( p_matrix );
    
    int row_index = 0;
    for ( row_index = 0; row_index < row_count; ++row_index ) {

        int column_index = 0;
        for ( column_index = 0; column_index < column_count; ++column_index ) {
            printf( "%d ", matrix[ row_index ][ column_index ] );
        }

        printf( "\n" );
    }
}

int main()
{
    int matrix3x5[ 3 ][ 5 ] = {
        { 1,  2,  3,  4,  5 },
        { 6,  7,  8,  9,  10 },
        { 11, 12, 13, 14, 15 }
    };

    int matrix5x3[ 5 ][ 3 ];

    printf( "before:\n" );
    print_matrix( matrix3x5, 3, 5 );

    rotate_90( matrix5x3, matrix3x5, 3, 5 );

    printf( "after:\n" );
    print_matrix( matrix5x3, 5, 3 );

    system( "PAUSE" );
    return 0;
}

Zaznaczam, że funkcja rotate_90 obraca tablicę o 90 stopni w prawo, a nie lewo.

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