Untitled

mail@pastecode.io avatar
unknown
c_cpp
20 days ago
1.2 kB
3
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

double oddAvg( int** tab, int n, int m )
{
    int sum = 0;
    int count = 0;

    for( int i = 1; i < n; i += 2 )
    {
        for( int j = 1; j < m; j += 2 )
        {
            sum += tab[ i ][ j ];
            ++count;
        }
    }

    if( count == 0 )
    {
        return 0;
    }

    return ( double )sum / count;
}

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

    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;
            ++counter;
        }
    }

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

    double result = oddAvg( tab, n, m );
    printf( "Srednia elementow na nieparzystych indeksach: %f\n", result );

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

    return 0;
}
Leave a Comment