Question

Explain the following code at each line. show a struct and with arrows indicate the operations...

Explain the following code at each line. show a struct and with arrows indicate the operations that are being performed. give as much detailed explanation as possible for the following code in C .

//print 1st matrix
printf("\n1st matrix:\n");
i=0;
while(i<3){
j=0;
while(j<3){
printf("%4d", *(*(a+i)+j));
j++;
}
i++;
printf("\n");
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//print 1st matrix
printf("\n1st matrix:\n");
i=0;
while(i<3){
j=0;
while(j<3){
printf("%4d", *(*(a+i)+j));
j++;
}
i++;
printf("\n");
}

The main part in the above code is identifier 'a'. It on a whole represents a 2-Dimensional array but the value in the variable 'a' has address of the 0th indexed 1-D array(Also called the base address). So, on adding 1 to a (a+1), we will be pointing to 1st indexed 1-D array.

By using unary operator astreik, *, the current pointing will be done to 0th element address of nth 1-D array. For example, *(a+1) points to 0th element in the 1st indexed 1-D array of the matrix. So, again using the same operator on the above statement will give the value of 0th element in the 1st 1-D array of the matrix. For example, *(*(a+1)+2) will give the value of 2nd element of the 1st indexed 1-D array in the matrix.

Let the given matrix is having values as below:

5 7 3

9 3 2

4 8 6

Now coming to the program, i will iterate from 0 to 2 and similarly j also but inside i loop iteration. So, the expression *(*(a+i)+j)) inside the printf statement will be accessed 9 times(3*3) as follows,

i = 0

*(*(a+0)+0)), gives value of 0th index of 0th 1-D array(1st row of matrix) , i.e., prints 5

*(*(a+0)+1)), gives value of 1st index of 0th 1-D array(1st row of matrix) , i.e., prints 7

*(*(a+0)+2)), gives value of 2nd index of 0th 1-D array(1st row of matrix) , i.e., prints 3

i = 1

*(*(a+1)+0)), gives value of 0th index of 1st 1-D array(2nd row of matrix) , i.e., prints 9

*(*(a+1)+1)), gives value of 1st index of 1st 1-D array(2nd row of matrix) , i.e., prints 3

*(*(a+1)+2)), gives value of 2nd index of 1st 1-D array(2nd row of matrix) , i.e., prints 2

i = 2

*(*(a+2)+0)), gives value of 0th index of 2nd 1-D array(3rd row of matrix) , i.e., prints 4

*(*(a+2)+1)), gives value of 1st index of 2nd 1-D array(3rd row of matrix) , i.e., prints 8

*(*(a+2)+2)), gives value of 2nd index of 2nd 1-D array(3rd row of matrix) , i.e., prints 6

Note: All the printed value in the output will length of 4 because we have %4d in printf(). For example, to print value '6', It prints as '' 6". It has length 4.

Hoping that the explanation helps.

Add a comment
Know the answer?
Add Answer to:
Explain the following code at each line. show a struct and with arrows indicate the operations...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT