2 Replies - 2712 Views - Last Post: 10 May 2011 - 01:32 PM Rate Topic: -----

#1 c++guy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 03-November 09

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:

#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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting 3 numbers in order of magnitude - most efficient/simple way

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 RetardedGenius   User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 127
  • View blog
  • Posts: 555
  • Joined: 30-October 10

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.

This post has been edited by RetardedGenius: 10 May 2011 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1