0

Write a program that prompts the user to input three numbers. The program should then output the numbers in ascending order.

6th Nov 2017, 6:22 PM
Talal Odat
Talal Odat - avatar
2 odpowiedzi
+ 15
//================================================ // Input some arbitrary integer and sort'em in // ascending order using selection sort. // // Done for : Talal Odat // By : Babak Sheykhan //================================================ #include <iostream> using std::cout; using std::cin; using std::endl; void sort(int *, int); int main() { int num_of_input = 0; cout << "How many number do you want to enter? "; cin >> num_of_input; int *inputs_container = new int[num_of_input]; for (auto i = 0; i < num_of_input; ++i) { do { // check for valid input which is integer values cout << "Value " << i + 1 << " is "; cin >> inputs_container[i]; if (std::cin.fail()) { std::cin.clear(); std::cin.ignore(200, '\n'); cout << "Enter correct value.\n"; } else break; } while(true); } sort(inputs_container, num_of_input); for (auto x = 0; x < num_of_input; ++x) cout << inputs_container[x] << "\t"; cout << endl; delete []inputs_container; inputs_container = 0; } void sort(int *c, int size) { /*** using selection sort to sorting the list ***/ int min; // holds the smallest element index bool isSwap = false; // Auxiliary flag for (auto i = 0; i < size - 1; ++i) { min = i; // initialize with the first element of the container for (auto j = i + 1; j < size; ++j) // comparing each elements of the container and updating min if (c[j] < c[min]) {min = j; isSwap = true;} // swapping if (isSwap) { c[i] ^= c[min]; c[min] ^= c[i]; c[i] ^= c[min]; isSwap = false; } } } Source code: [https://code.sololearn.com/cyEd13zC1nDj] If you had any question about that feel free and ask.
7th Nov 2017, 7:16 AM
Babak
Babak - avatar
0
Please Show that you have the ability first
6th Nov 2017, 6:27 PM
Joshua
Joshua - avatar