#include <iostream>
using namespace std;
int main()
{
char name[30];
char date1[30];
char date2[30];
char date3[30];
double vault1, vault2, vault3;
cout << "Enter your name: ";
cin.getline(name, 30);
cout << "Enter the date of your highest vault: ";
cin.getline(date1, 30);
cout << "Enter your highest vault in meters: ";
cin >> vault1;
if (vault1 < 2 || vault1 > 5)
cout << "You must enter a number between 2 and 5 meters" << endl;
cin.ignore();
cout << "Enter the date of your 2nd highest vault: ";
cin.getline(date2, 30);
cout << "Enter your 2nd highest vault in meters: ";
cin >> vault2;
if (vault2 < 2 || vault2 > 5)
cout << "You must enter a number between 2 and 5 meters" << endl;
cin.ignore();
cout << "Enter the date of your 3rd highest vault: ";
cin.getline(date3, 30);
cout << "Enter your 3rd highest vault in meters: ";
cin >> vault3;
if (vault3 < 2 || vault3 > 5)
cout << "You must enter a number between 2 and 5 meters" << endl;
if (vault1 > vault2 && vault1 > vault3)
{
if (vault2 > vault3)
{
cout << "Highest vaults in order: " << endl
<< date1 << ": " << vault1 << endl
<< date2 << ": " << vault2 << endl
<< date3 << ": " << vault3 << endl;
}
else
{
cout << "Highest vaults in order: " << endl
<< date1 << ": " << vault1 << endl
<< date3 << ": " << vault3 << endl
<< date2 << ": " << vault2 << endl;
}
}
if (vault2 > vault1 && vault2 > vault3)
{
if (vault1 > vault3)
{
cout << "Highest vaults in order: " << endl
<< date2 << ": " << vault2 << endl
<< date1 << ": " << vault1 << endl
<< date3 << ": " << vault3 << endl;
}
else
{
cout << "Highest vaults in order: " << endl
<< date2 << ": " << vault2 << endl
<< date3 << ": " << vault3 << endl
<< date1 << ": " << vault1 << endl;
}
}
if (vault3 > vault1 && vault3 > vault2)
{
if (vault1 > vault2)
{
cout << "Highest vaults in order: " << endl
<< date3 << ": " << vault3 << endl
<< date1 << ": " << vault1 << endl
<< date2 << ": " << vault2 << endl;
}
else
{
cout << "Highest vaults in order: " << endl
<< date3 << ": " << vault3 << endl
<< date2 << ": " << vault2 << endl
<< date1 << ": " << vault1 << endl;
}
}
return 0;
}
2 Replies - 2712 Views - Last Post: 10 May 2011 - 01:32 PM
#1
Sorting 3 numbers in order of magnitude - most efficient/simple way
Posted 10 May 2011 - 11:20 AM
I'm only in chapter 4 of starting out with c++ by tony gaddis so I'm not looking for some crazy function I wouldn't have heard of yet. I'm just wondering if there is a better/cleaner logical solution than the way i have done it here:
Replies To: Sorting 3 numbers in order of magnitude - most efficient/simple way
#2
Re: Sorting 3 numbers in order of magnitude - most efficient/simple way
Posted 10 May 2011 - 11:48 AM
I'd put the numbers in an array and then sort the array using something like quick sort or insertion sort. Or I'd put them in a vector and tell the vector to sort itself.
#3
Re: Sorting 3 numbers in order of magnitude - most efficient/simple way
Posted 10 May 2011 - 01:32 PM
If you've never implemented a sorting algorithm before I would suggest Selection Sort, it's one of the easiest sorting algorithms to understand and code.
If you're sorting relatively small sets of data, < 100, then a basic algorithm like Selection Sort will prove perfectly adequate. When you are more confident in your understanding and programming abilities, I would suggest learning about Quick Sort.
Edit: I would always recommend trying to implement the algorithm yourself, from its description as much as possible, (I think that it's better for your understanding.) However if you do have any trouble coding Selection Sort I would recommend the tutorial here as it explains it quite clearly, with examples of C++ source code. Although you will want to encapsulate your sorting algorithm in a void function and pass the array as an argument by reference.
If you're sorting relatively small sets of data, < 100, then a basic algorithm like Selection Sort will prove perfectly adequate. When you are more confident in your understanding and programming abilities, I would suggest learning about Quick Sort.
Edit: I would always recommend trying to implement the algorithm yourself, from its description as much as possible, (I think that it's better for your understanding.) However if you do have any trouble coding Selection Sort I would recommend the tutorial here as it explains it quite clearly, with examples of C++ source code. Although you will want to encapsulate your sorting algorithm in a void function and pass the array as an argument by reference.
This post has been edited by RetardedGenius: 10 May 2011 - 01:40 PM
Page 1 of 1

New Topic/Question
Reply



MultiQuote





|