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

Click For Summary

Discussion Overview

The discussion centers around how to modify a selection sort function in C++ so that it can work with both double and integer types. Participants explore various approaches to achieve this functionality, including typedefs, function overloading, and templates.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a selection sort function that currently only accepts an array of type double and expresses frustration when trying to pass an integer array to it.
  • Another participant suggests using a typedef to create a common type for the array, allowing for flexibility in the data type used.
  • It is proposed that function overloading could be a solution, where two versions of the sSort function are created, one for doubles and one for integers.
  • A third participant introduces the concept of templates in C++, suggesting that defining the function with a template would allow it to handle any data type.
  • A later reply points out a specific issue with variable types in the sorting function, noting that using a double variable for holding integer values could lead to problems, reinforcing the need for consistent data types.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to address the issue, including typedefs, function overloading, and templates. No consensus is reached on the best approach.

Contextual Notes

Some limitations are noted regarding the need for consistent data types within the sorting function and potential issues with type casting when using mixed data types.

Saladsamurai
Messages
3,009
Reaction score
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
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.
 
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.
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
20
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K