참조에 의한 호출을 사용하여 cyclicSwapping() 함수에 전달하여 3개의 숫자를 순환 순서로 교환할 수 있습니다. 이 함수는 순환 방식으로 숫자를 교환합니다.
참조에 의한 호출을 사용하여 순환 순서로 번호를 교환하는 프로그램은 다음과 같습니다. -
예시
#include<iostream>
using namespace std;
void cyclicSwapping(int *x, int *y, int *z) {
int temp;
temp = *y;
*y = *x;
*x = *z;
*z = temp;
}
int main() {
int x, y, z;
cout << "Enter the values of 3 numbers: "<<endl;
cin >> x >> y >> z;
cout << "Number values before cyclic swapping..." << endl;
cout << "x = "<< x <<endl;
cout << "y = "<< y <<endl;
cout << "z = "<< z <<endl;
cyclicSwapping(&x, &y, &z);
cout << "Number values after cyclic swapping..." << endl;
cout << "x = "<< x <<endl;
cout << "y = "<< y <<endl;
cout << "z = "<< z <<endl;
return 0;
} 출력
위 프로그램의 출력은 다음과 같습니다 -
Enter the values of 3 numbers: 2 5 7 Number values before cyclic swapping... x = 2 y = 5 z = 7 Number values after cyclic swapping... x = 7 y = 2 z = 5
위의 프로그램에서 cyclicSwapping() 함수는 참조에 의한 호출을 사용하여 순환 순서로 세 숫자를 교환합니다. 함수는 변수 temp를 사용하여 이를 수행합니다. 이에 대한 코드 조각은 다음과 같습니다 -
void cyclicSwapping(int *x, int *y, int *z) {
int temp;
temp = *y;
*y = *x;
*x = *z;
*z = temp;
} main() 함수에서 3개의 숫자 값은 사용자가 제공합니다. 그런 다음 이러한 값은 교환하기 전에 표시됩니다. cyclicSwapping() 함수를 호출하여 숫자를 교환한 다음 교환한 후 값을 표시합니다. 이것은 다음과 같습니다 -
cout << "Enter the values of 3 numbers: "<<endl; cin >> x >> y >> z; cout << "Number values before cyclic swapping..." << endl; cout << "x = "<< x <<endl; cout << "y = "<< y <<endl; cout << "z = "<< z <<endl; cyclicSwapping(&x, &y, &z); cout << "Number values after cyclic swapping..." << endl; cout << "x = "<< x <<endl; cout << "y = "<< y <<endl; cout << "z = "<< z <<endl;