Answered step by step
Verified Expert Solution
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
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 3) Verify that x and y were initialized properly by printing out their values and their 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 are on the right. This makes it easier to align the values.) 4)Verify that ptr1 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: *pt r 1 = 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.) CS1325- Introduction to Programming page: 2 5) Using ptrl and ptr2 only, change x to 10 and y to 15. (Note: do not access x and y directly.) 6) Verify that x and y were changed as follows: 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 ().) 8)Verify that x and y were changed as follows: Stage 4: x= XXX, y= XXX ; &x=XXX; &y=XXX (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 &%) Change the variable pointed to by ptrl to 77, and change the variable pointed to by ptr2 to 88 11) 12) Verify that x and y were changed as follows: (Note that y has the 77 now, and x has the 88.) Output Requirements These types of experiments are easier to interpret if the output is neat and wel 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) Stage 1: Stage 2: *ptrl5, *ptr2 10 ptrl0032FD10, ptr2 0032FD04 Stage 3: Stage 4: Stage 5: *ptrl-10, *ptr2-15 ptr1-0032FD04, ptr2-0032FD10 Stage 6: y10 &y = 0032FD04 x10, x - 15, - 15; y10 &x0032FD10, x0032FD10, &- 0032FD04 &y - 0032FD04 x-88 y-77 62. - 0032FD10, & - 0032FD04 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 can 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), ptr 1 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 Final Notes Errors and Warnings As we grade this assignment, we are going to look in particular for errors or warnings of the following types warning C4047: 'function': 'int 'differs in levels of indirection from 'int* or warning C4024 SwapIntPtr': different types for formal and actual parameter 1 These particular warnings were generated in Visual Studio, and so, of course, they may be slightly different with different compilers. Nevertheless, these types of warnings often occur when we have a data type mismatch involving pointers. For example, they can occur when we attempt to send an int into a function that is expecting an "int *". Note that in this case, the levels of indirection are different. Most compilers will correctly identify that problem. When we compile your programs, there should be no warnings or errors of this kind at a Each such warning or error would indicate that a pointer is not being used correctly, and will cost points
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started