Exercise · Pointers · lesson 7 of 8
Exercise: swap
Write a function func swap(a, b *int) that swaps the values of the variables a and b point to. Then write a function func sortPair(a, b *int): after it, the smaller of the two values is in *a and the larger one is in *b.
x, y := 1, 2, thenswap(&x, &y)→xis2,yis1x, y := -3, 7, thenswap(&x, &y)→xis7,yis-3x, y := 5, 2, thensortPair(&x, &y)→xis2,yis5
Constraints: the pointers are not nil; a and b may point to the same variable, and then its value does not change; sortPair leaves an ordered pair as it is.
Do not add extra imports unless the task says so explicitly.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.