Untitled

mail@pastecode.io avatar
unknown
c_cpp
21 days ago
874 B
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

int sumArray( int n, int m, int** tab )
{
    int result = 0;

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

    return result;
}

int main()
{
    int n = 3;
    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 value = 1;
    for( int i = 0; i < n; ++i )
    {
        for( int j = 0; j < m; ++j )
        {
            tab[ i ][ j ] = value;
        }
    }

    int result = sumArray( n, m, tab );
    printf( "Suma elementow tablicy: %d\n", result );

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

    free( tab );

    return 0;
}
Leave a Comment