Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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 frorm 1. 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 integer and assign it to int_ptr. Use man 3 malloc for details on this system call 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 user's response using scanf, storing the integer in the second element of the integer array allocated in step 1. 5. Now, print out the original values of both the integers input by the user. 6. This next step is where the swapping is done: 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 () SAMPLE OUTPUT (user input shown in bold): $ . /a.out Enter first integer: 571 Enter second integer: 18 Original values: 1st 571 2nd18 Swapped values: 1st- 18 2nd571 $ . /a.out Enter first integer: -4 Enter second integer: 288 Original values: 1st -4 2nd288 Swapped values: 1st 288 2nd - -4
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