Question: 9%20(1).pdf PROGRAM DESCRIPTION: In this recitation assignment, will write a complete C program that accepts as input any two integers from the user and swaps
9%20(1).pdf PROGRAM DESCRIPTION: In this recitation assignment, will write a complete C program that accepts as input any two integers from the user and swaps both integers using bitwise operators only without a third variable. Early on in your programming career, you were told that you need to use a third variable in order to swap two variables, but in this assignment, you will accomplish this task without the extra variable. We will use the bitwise XOR operator to swap the integers as the bitwise XOR operator evaluates each bit of the result to 1 if the corresponding bits of the operands are different and 0 otherwise. Programming with C and Unix optional reference textbook by Adam Hoover to be helpful. your TA or fellow students. Although not required, you may find Chapter 2 on Bits, Bytes, and Data Types in the System For this recitation assignment, complete the following tasks. You may receive guidance from Declare an integer pointer and then request memory using malloc () for an array of 2 integers. This means that you should request memory for a size of 2 times the size of an I integer and assign it to int_ptr. Use man 3 malloc for details on this system call. 1. 2. If the malloc () system call failed to return memory, print out a meaningful error message and terminate the program. 3. Prompt the user to enter the first integer using printf and then read in the user's response using scanf, storing the integer in the first element of the integer array allocated in step 1 4. Prompt the user to enter the second integer using printf and then read in the users response using scanf, storing the integer in the second element of the integer array allocated in step 1. Now, print out the original values of both the integers input by the user. This next step is where the swapping is done: 5. 6. a. Use the bitwise XOR operator on both the array elements and assign the result to the first element. b. Use the bitwise XOR operator on both the array elements and assign the result to the second element. c. Use the bitwise XOR operator on both the array elements and assign the result to the first element. 7. Now, print out the swapped values of both the integers input by the user 8. Finally, release the allocated memory using free O
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
