What are the lines that you're getting errors at? You need a pointer to pointer to hold the address of the first element of the matrix. Assuming you use a, this variable should be declared as:
Allocate the rows with the following statement:
- a = ( int ** )malloc( m * sizeof( int * ) );
Then, use a for statement to allocate memory for the columns of each row:
- for ( i = 0 ; i < m ; ++i )
-
{
-
*( a + i ) = ( int * )malloc( n * sizeof( int ) );
-
}
Now, loop through the whole matrix and assign 1 or 0 to each element:
- for ( i = 0 ; i < m ; ++i )
-
{
-
for ( j = 0 ; j < n ; ++j )
-
{
-
if ( i != j )
-
{
-
a[ i ][ j ] = 0;
-
}
-
else
-
{
-
a[ i ][ j ] = 1;
-
}
-
}
-
}
Then, print the matrix to check that everything is correct:
- printf( "The matrix is:\n\n" );
-
-
for ( i = 0 ; i < m ; ++i )
-
{
-
for ( j = 0 ; j < n ; ++j )
-
{
-
printf( "%3d", a[ i ][ j ] );
-
}
-
-
putchar( '\n' );
-
}