SHOW:
|
|
- or go back to the newest paste.
| 1 | #include<iostream> | |
| 2 | ||
| 3 | ||
| 4 | - | using namespace std; |
| 4 | + | |
| 5 | std::cout << "Enter 3 numbers:" << std::endl; | |
| 6 | int array[3]; | |
| 7 | const int n = 3; | |
| 8 | - | int hold; |
| 8 | + | for(int i = 0; i < n; i++) {
|
| 9 | std::cin >> array[i]; | |
| 10 | - | int array[3]; |
| 10 | + | } |
| 11 | /* basic bubble sort for educational purposes | |
| 12 | - | cout<<"Enter 3 numbers: "<<endl; |
| 12 | + | * Replace with std::sort for production code |
| 13 | */ | |
| 14 | - | for(int i=0; i<3; i++) |
| 14 | + | for(int i = n; i > 0; i--) {
|
| 15 | for(int j = 0; j < i - 1; j++) {
| |
| 16 | - | {
|
| 16 | + | if(array[j] > array[j+1]) {
|
| 17 | /* Replace with std::swap for production code */ | |
| 18 | - | cin>>array[i]; |
| 18 | + | int hold = array[j]; |
| 19 | array[j] = array[j+1]; | |
| 20 | - | } |
| 20 | + | array[j+1] = hold; |
| 21 | } | |
| 22 | - | for(int j=0; j<3; j++) |
| 22 | + | } |
| 23 | } | |
| 24 | - | {
|
| 24 | + | std::cout << "Ascending Array is:\n"; |
| 25 | for(int i = 0; i < 3; i++) {
| |
| 26 | std::cout << array[i] << "\n"; | |
| 27 | - | } |
| 27 | + | } |
| 28 | std::cout << "Descending Array is:\n"; | |
| 29 | - | for(int i=0; i<4; i++) |
| 29 | + | for(int i = n - 1; i >= 0; i--) {
|
| 30 | std::cout << array[i] << "\n"; | |
| 31 | - | {
|
| 31 | + | } |
| 32 | /* just for good measure */ | |
| 33 | - | for(int j=0; j<4; j++) |
| 33 | + | std::cout.flush(); |
| 34 | return 0; | |
| 35 | - | {
|
| 35 | + |