+ 4

I have a question

We have two variables. a and b. Can you change the values ​​of a b and c like example without using another variable? Ex: a = 10 b = 5 c = 7 >> Run a = 7 b = 10 c = 5 a -> b b -> c c -> a

6th May 2018, 7:03 PM
Ayastar
Ayastar - avatar
3 odpowiedzi
+ 1
ofc you can. thing of swapping only two of them first and then one of them with the third. If you cant get your head around, I am more than ready to help ;)
6th May 2018, 7:15 PM
Paul
+ 1
#include <stdio.h> void cyclicSwap(int* a, int* b, int* c) { // Before overwriting b, store its  // value in temp. int temp = *a; // Now do required swapping starting   // with b. *a = *b; *b = *c; *c = temp; } int main() { int a = 10, b = 5, c = 7; printf("Value before swapping:\n"); printf("a = %d \nb = %d \nc = %d\n", a, b, c); cyclicSwap(&a, &b, &c);  printf("Value after swapping:\n");  printf("a = %d \nb = %d \nc = %d", a, b, c); return 0; }
3rd Jun 2018, 11:32 AM
Anish Vishwakarma
Anish Vishwakarma - avatar