193k views
5 votes
In C!!

12.12 LAB: Swapping variables
Write a program whose input is two integers and whose output is the two integers swapped.

Ex: If the input is:

3 8
then the output is:

8 3
Your program must define and call a function. SwapValues returns the two values in swapped order.
void SwapValues(int* userVal1, int* userVal2)

In C!! 12.12 LAB: Swapping variables Write a program whose input is two integers and-example-1
User Yuvaraj M
by
6.3k points

1 Answer

4 votes

Answer:

#include <stdio.h>

void SwapValues(int* userVal1, int* userVal2)

{

int temp = *userVal1;

*userVal1 = *userVal2;

*userVal2 = temp;

}

int main(void)

{

int a = 3;

int b = 8;

printf("Before swap: a=%d, b=%d\\", a, b);

SwapValues(&a, &b);

printf("After swap: a=%d, b=%d\\", a, b);

return 0;

}

Step-by-step explanation:

For swapping variables efficiently, you need a helper variable in C or C++.

In C!! 12.12 LAB: Swapping variables Write a program whose input is two integers and-example-1
User Kevin Sylvestre
by
6.5k points