Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program 1: Warmup Exercises with Pointers Sometimes when we're working with a new subject, like pointers, it helps to do a series of smaller problems

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Program 1: Warmup Exercises with Pointers Sometimes when we're working with a new subject, like pointers, it helps to do a series of smaller problems before we tackle a bigger one. That is the purpose of this first program For this problem, write a single program that does the following 12 tasks. Please put clear comments in your code to indicate when the various tasks are being done, 1) Create two int variables x and y and initialize them to 5 and 10 respectively. 2) Create two int pointers, ptrl and ptr2, and initialize them to point to x and y respectively. addresses (i.e., using the address operator on x and y) as follows: Stage 1: x- XXX, y - XXX; &x -XXX; &y - XXX (Note that we've designed the output so that the values are on the left and the addresses 3)Verify that x and y were initialized properly by printing out their values and their are on the right. This makes it easier to align the values.) 4)Verify that ptrl and ptr2 were initialized properly by printing the variables they are pointing to on the left and their addresses on the right as follows. In this step, print out all 4 items using the pointers only: Stage 2: *ptr1 = XXX, *prt2 = XXX, ptr1 = XXX, ptr2 = XXX (Note: even though we are using pointers here, these output values should be the same as in Stage 1.) 5) Using ptrl and ptr2 only, change x to 10 and y to 15. (Note: do not access x and y directly.) Verify that x and y were changed as follows: Stage 3: x -XXX, y - XXX; &x -XXX; &y -Xx2x 6) 7 Write a swapInt function that changes the values of x and y. Call it on x and y (It is important that you use this exact name for this function. Do not use the name swap ).) Verify that x and y were changed as follows: Stage 4: x -XXX, y - XXX; &x - XXX; &y -Xxix 8) (Here the values for x and y should be the reverse of State 3, but the addresses will remain the same. 9 Write a swapIntPtr ) function that changes the values of ptr1 and ptr2. Call it on ptrl and ptr2. (Again, it is important to use this exact name for this function. Note that there is no function overloading in C (therefore, this function must be named differently than the function referenced above).) 10) Verify that ptrl and ptr2 were changed properly as follows Stage 5: *ptr1 = XXX ; *prt2 = XXX ; ptr1 = XXX, ptr2 = XXX (Now the address contained in ptrl should be the same address as &y, and the address contained in ptr2 should be the same address as &x.) 11) Change the variable pointed to by ptrl to 77, and change the variable pointed to by ptr2 to 88. 12) Verify that x and y were changed as follows: Stage 6: x-XXX, y= XXX ; &x=XXX; (Note that y has the 77 now, and x has the 88.) &y-XXX Output Requirements These types of experiments are easier to interpret if the output is neat and well organized. Here is an example of the output from a correct run of this program. Yours should look just like this (but with different addresses, of course) 0032F010, &y0032FD04 2FD10, ptr2 -0032FD04 &y0032FD04 &y0032FD04 ptr2=0032FD10 Stage 1: y-10; EX age 2: *ptrl-5, p Stage 3: Stage 4: Stage 5: *ptrl-10, *ptr215; ptrl-0032FDO4, Stage 6: 0 ptrl-003 x10 x = 15 y-15; y-10; &x = 0032FD10 62. - 0032FD10 x = 88 Y77 62. - 0032FD10 Of course, the addresses will be different for each run of the program. Nevertheless, the stages are clearly marked and all of the integers and all of the addresses line up. As a result, we carn now see exactly what is happening. Stage 3 occurs after the values for x and y were changed using ptrl and ptr2. Stage 4 occurs after the swapInt ) function is called on x and y. And Stage 5 occurs after the swapIntPtr () function is called on ptrl and ptr2. As we can see in the last line (i.e., Stage 5), ptrl now points to y and ptr2 now points to x. Please make your output neat and aligned as demonstrated above. All of the integer values are on the left, all of the addresses are on the right. Each value should be clearly identified and everything should line up neatly

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions