write a c program to swap elements in cyclic order using call by reference
Answers
Answered by
0
To understand this example, you should have the knowledge of following C programming topics:
C Programming Pointers
C Call by Reference: Using pointers
Three variables entered by the user are stored in variables a, b and crespectively.
Then, these variables are passed to the function cyclicSwap(). Instead of passing the actual variables, addresses of these variables are passed.
When these variables are swapped in cyclic order in the cyclicSwap()function, variables a, b and c in the main function are also automatically swapped.
Example: Program to Swap Elements Using Call by Reference
#include<stdio.h> void cyclicSwap(int *a,int *b,int *c); int main() { int a, b, c; printf("Enter a, b and c respectively: "); scanf("%d %d %d",&a,&b,&c); 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; } void cyclicSwap(int *a,int *b,int *c) { int temp; // swapping in cyclic order temp = *b; *b = *a; *a = *c; *c = temp
C Programming Pointers
C Call by Reference: Using pointers
Three variables entered by the user are stored in variables a, b and crespectively.
Then, these variables are passed to the function cyclicSwap(). Instead of passing the actual variables, addresses of these variables are passed.
When these variables are swapped in cyclic order in the cyclicSwap()function, variables a, b and c in the main function are also automatically swapped.
Example: Program to Swap Elements Using Call by Reference
#include<stdio.h> void cyclicSwap(int *a,int *b,int *c); int main() { int a, b, c; printf("Enter a, b and c respectively: "); scanf("%d %d %d",&a,&b,&c); 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; } void cyclicSwap(int *a,int *b,int *c) { int temp; // swapping in cyclic order temp = *b; *b = *a; *a = *c; *c = temp
Similar questions