Untitled

mail@pastecode.io avatar
unknown
c_cpp
22 days ago
1.4 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

void revRows( int** tab, int n, int m )
{
    for( int i = 0; i < n; ++i )
    {
        int left = 0;
        int right = m - 1;
        while( left < right )
        {
            int temp = tab[ i ][ left ];
            tab[ i ][ left ] = tab[ i ][ right ];
            tab[ i ][ right ] = temp;
            ++left;
            --right;
        }
    }
}

int main()
{
    int n = 4;
    int m = 5;

    int** tab = ( int** )malloc( n * sizeof( int* ) );
    for( int i = 0; i < n; ++i )
    {
        tab[ i ] = ( int* )malloc( m * sizeof( int ) );
    }

    int counter = 1;
    for( int i = 0; i < n; i++ )
    {
        for( int j = 0; j < m; j++ )
        {
            tab[i][j] = counter++;
        }
    }

    printf( "Tablica przed odwróceniem:\n" );
    for( int i = 0; i < n; i++ )
    {
        for( int j = 0; j < m; j++ )
        {
            printf( "%2d ", tab[ i ][ j ] );
        }
        printf( "\n" );
    }

    revRows( tab, n, m );

    printf( "\nTablica po odwróceniu:\n" );
    for( int i = 0; i < n; i++ )
    {
        for( int j = 0; j < m; j++ )
        {
            printf( "%2d ", tab[ i ][ j ] );
        }
        printf( "\n" );
    }

    for( int i = 0; i < n; i++ )
    {
        free( tab[ i ] );
    }
    free( tab );

    return 0;
}
Leave a Comment