C++:How to Make sort function work with Double and Integer type

In summary: They allow you to have one function that can work with different data types without having to change the function.
  • #1
Saladsamurai
3,020
7
So I have a simple selection sort function written. It takes an array of type double as the array to be sorted. If I declare the array to be of type int in my main program and then pass it to my sSort function, it gets mad. There must be a way that programmers deal with this kind fo stuff.

Here is the code if needed:

Code:
/* SORTING ALGORITHMS: Etter Chapter6_5 p281         */ 
/*----------------------------------------------------
 This is a driver program to test the sorting 
 functions below.
 ----------------------------------------------------*/
#include <iostream>
#include <iomanip>
using namespace std;

// funtion prototype
void sSort(double sortableArray[], int numElements);

// driver program
int main () {
    // declare objects
	double x[11] = {1.1,1.0,1.5,1.4,1.3,1.8,1.9,1.2,1.7,1.6,2.0};
	int n(11);
	
	// set format flags for standard output
	cout.setf(ios::fixed); //decimal notation
	cout.precision(2); // out to 2 places
	
	// display initial state of x[]
	cout << "********************" << endl;
	cout << "Unsorted: \n";
	for (int i=0;i<=n-1;++i)
	{
		cout << x[i] << endl;
	}
	
	// call sorting algorithm Select Sort
	sSort(x,n); 
	
	// display final state of x[]
	cout << "********************" << endl;
	cout << "Sorted: \n";
	for (int i=0;i<=n-1;++i)
	{
		cout << x[i] << endl;
	}
	
	
	
	
    return 0;
}

/*----------------------------------------------------
 SELECTION SORT: this function sorts an array with n
 elements into ascending order
 ----------------------------------------------------*/
void sSort(double x[], int n)
{
	// declare local objects
	int m;
	double hold;
	// implement selection sort algorithm
	for (int k=0;k<=n-2;++k)
	{
		// find position of smallest value in array
		// beginning at k
		m=k;
		for (int j=k+1;j<=n-1;++j)
		{
			if (x[j]<x[m])
				m = j;
		}
		// exchange smallest value with value at k
		hold = x[m];
		x[m] = x[k];
		x[k] = hold;
	}
	// void return
	return;
}
/*---------------------------------------------------*/
 
Technology news on Phys.org
  • #2
You could create a typedef for the arraytype:

#typedef ARRAYTYPE double

or

#typedef ARRAYTYPE int

Then use arraytype instead of int or double:

void sSort(ARRAYTYPE x[], int n){...}

Since this is C++, you could also duplicate sSort() to create one that sorts integers:

void sSort(int x[], int n){...}

You can leave both versions of sSort() in your program. as C++ will treat them as separate functions since their input parameters are different.
 
  • #3
In C++ so called templates have been introduced.
If you define your function like this:

Code:
template<class X>
void sSort(X sortableArray[], int numElements)
{
   // Replace all occurrences of double by X
}

that should do the trick.
 
  • #4
Hey Saladsamurai.

The problem is in this line:

Code:
double hold;

When your array is an integer, this variable doesn't change (needs to be the same data type, but instead its a double not an int). Different format, no casting and you're bound to get something screwed up.

As mentioned above, this is why templates are used.
 
  • #5
I would like to point out that in order to make the sort function work with both double and integer types, you will need to make some modifications to your code. Currently, your sSort function is only able to sort arrays of type double, as it uses the < operator to compare elements. However, with integers, you cannot use the < operator as it will only compare the integer values and not take into account their decimal places.

To make this function work with both double and integer types, you can implement a template function. This will allow you to pass in different data types to the function and have it work accordingly. For example, you can use the < operator for double arrays and the < operator for integer arrays.

Another approach would be to use the std::sort function from the <algorithm> library. This function takes in a range of elements and a comparison function, which you can define to handle both double and integer types. This would be a more efficient and flexible solution.

In conclusion, there are multiple ways to make the sort function work with double and integer types. It is important to carefully consider your options and choose the best approach for your specific situation.
 

1. How can I make the sort function work with both double and integer types in C++?

The sort function in C++ can work with both double and integer types by using function templates. Function templates allow you to create a single function that can operate on different data types. You can use the template keyword followed by the data type name, and then use that data type within the function.

2. Can I use the sort function to sort a vector of mixed data types?

Yes, you can use the sort function to sort a vector of mixed data types by defining a custom comparison function. This function should take in two elements of the vector and return a boolean indicating whether the first element should come before the second in the sorted vector.

3. How do I ensure that the sort function correctly sorts double and integer values together?

To ensure that the sort function correctly sorts double and integer values together, make sure that your comparison function takes into account both data types. You can use the std::is_same function to check the type of the elements being compared, and then perform the appropriate comparison.

4. Can I use the sort function to sort a custom class that contains both double and integer values?

Yes, you can use the sort function to sort a custom class that contains both double and integer values by overloading the less-than operator for your class. This operator should compare the double and integer values within the class, and return a boolean indicating which object should come first in the sorted list.

5. Are there any performance implications when using the sort function with mixed data types?

There may be some performance implications when using the sort function with mixed data types, as the function needs to convert the data types to a common type for comparison. However, this should not significantly impact performance unless you are working with very large data sets.

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
Replies
10
Views
958
Back
Top