Question

PART FOUR:(20 points) Predict the output that would be shown in the terminal window when the following program fragments are
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Output:

5

20

In this particular program, we have created an array 'A' of type integer. Then we created an integer pointer 'ptr1' which points to the 1st element of array 'A' . Then we created 2nd integer pointer 'ptr2' and assign memory location of 6th block from the 1st array element(This is valid declaration in C). As the int size is taken as 4, so when we execute the first print statement,then each element will occupy 4 bytes of memory and and there are 5 blocks of memory between the 2 pointers memory location as 'ptr1' is pointing to 1st element and 'ptr2' is pointing to 6th. So 'ptr2-ptr1' .gives the 5 as there are 5 elements between them. But in the next print statement, we type cast the pointer variables to char,so they started pointing to individual addresses in momory. So the difference between '(char*)ptr2 - (char*)ptr1' gives 5 * 4 (size of int ) memory locations i.e. 20.

2. Output:

1

4

9

16

25

36

49

64

81

100

Total is: 385

In this program, we have created 3 unsigned integer variables(means they can only take positive values), x initialized to 1, total initialized to 0 and y only declared not initialized. When while loop executes, it checks if value of x is less than or equal to 10, if yes it goes inside the loop, First it stores square of x in y and prints y, Then value of y gets added to total which was initially 0, then post increment of x will increment x by 1. This while loop will continue until x is not greater than 10. Each time it prints square of current value of x which gets stored in y, adds that y value to total and increment x. When we come out of loop, value of total will be printed which is equal to sum of squares of x values from 1 to 10.

Add a comment
Know the answer?
Add Answer to:
PART FOUR:(20 points) Predict the output that would be shown in the terminal window when the...
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
ADVERTISEMENT