In: Computer Science
(IN C)
What are the contents of the array after the for-loop in the following code?
int array[ SIZE ][ SIZE ] = { { 4, 5, 6, 7, 8 },
{ 1, 2, 3, 4, 5 },
{ 3, 6, 7, 8, 9 },
{ 2, 3, 4, 5, 6 },
{ 5, 6, 7, 8, 9 } };
int i;
int *ptr = array[ 0 ];
for( i = 0; i < SIZE * SIZE; i++ ) {
if( i % SIZE < 2 ) {
*( ptr + i ) = 0;
}
else if( i % SIZE == 2 ) {
*( ptr + i * SIZE ) = 100;
}
}
if( i % SIZE < 2 ) {
*( ptr + i ) = 0;
}
The above condition make the memory address 0,1,5,6,10,11,15,16,20,21 is set to 0
else if( i % SIZE == 2 ) {
*( ptr + i * SIZE ) = 100;
}
The above condition is satisfied when i=2,7,12,17,22
the index which are changed is 10,35,60,85,100
but the index 10 is overwritten by first if condition to 0
So the final output is