forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
11 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,29 @@ | ||
//!@Alan | ||
//!@Yue wang | ||
//! | ||
//! Exercise 6.10: | ||
//! Using pointers, write a function to swap the values of two ints. | ||
//! Test the function by calling it and printing the swapped values. | ||
//! | ||
|
||
|
||
|
||
#include <iostream> | ||
#include <string> | ||
#include <stdexcept> | ||
|
||
using namespace std; | ||
|
||
void swap(int* _n1, int* _n2); | ||
void swap(int* lhs, int* rhs) | ||
{ | ||
int tmp; | ||
tmp = *lhs; | ||
*lhs = *rhs; | ||
*rhs = tmp; | ||
} | ||
|
||
int main() | ||
{ | ||
int n1, n2; | ||
|
||
cout<<"Please Enter:\n"; | ||
while(cin>>n1>>n2) | ||
for (int lft, rht; std::cout << "Please Enter:\n", std::cin >> lft >> rht; ) | ||
{ | ||
swap(&n1, &n2); | ||
cout<<n1<<" "<<n2<<endl; | ||
swap(&lft, &rht); | ||
std::cout << lft << " " << rht << std::endl; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void swap(int* _n1, int* _n2) | ||
{ | ||
int temp; | ||
temp = *_n1; | ||
*_n1 = *_n2; | ||
*_n2 = temp; | ||
} |