how to sort?

Pages: 12
Hi all;
I should sort an array of mixed float and integer numbers by merge method, and using the pointers to sort that mix array. i have no idea how to use pointers to sort those different type of data. i would be grateful to hear your ideas.
Thanks in advance
Last edited on
How would you create such an array?

Wouldn't it be easier to convert all of the values to float and sort that array?
i should read array from an input file, it is a kind of homework.
He asked how you intend to store both integers and floats in a single array.

I suggest to just represent both integers and floating point numbers as double and then sort array.
yes this is my problem how to put in an array, we are not allowed to use double, we should use pointers!
Ah, ok then.
I think you should create a new class called intORfloat. To each object give a bool, if it's true then it's int, if it's false then it's float. Use "new int" or "new float" to allocate space for your variable and store it in a pointer. Make a funtion to get the value (return (int)*pointer; or return (float)*pointer; depending on your bool). To switch places of 2 variables simply switch places of 2 of those objects.

I hope I explained it well enough. Good luck and if you encounter problems while trying to implement this just post your code here and tell us what's the problem.
thank you zoran404;
your idea sounds grate, but I am beginner specially working with classes, here is what i get by your help, but I am sure ,I am wrong:
class intORfloat
{
public:
int integer;
float fp;
int whichtype(//?)
{ // determining which type input is
}
firstly, what type should be define as input of "whichtype" function?
secondly memory should be allocate in class?
finally, my teacher said we can solve this problem by just using pointers , no need to define a class! he exampled this

does it sound??
Last edited on
That code should have been:
1
2
3
unsigned int i=0x3f800000;
float f=*(flaot*)(&i);
cout<<f;

And this will not actually give you the ability to sort your arrays (well you could store the values like this, but without knowing if they are int or float you could not sort them), because 0x3f800000 is not actually an integer, it is a float, you only use pointers to insert that value into an unsigned integer. This is something I highly recommend that you don't do.

And fortunately you didn't implement that class as I described. Here, this is the function I wanted you to make, along with sample of how to use it and comments explaining it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class intORfloat
{
public:
	intORfloat(){} //default constructor
	intORfloat(float f) //constructor: making a float
	{
		pointer = (void*) new float; //allocating space for float
		iorf = false; //false means it's float
	}
	intORfloat(int i) //constructor: making an int
	{
		pointer = (void*) new int; //allocating space for float
		iorf = true; //true means it's int
	}
	void assign(float f) //assign a float
	{
		pointer = (void*) new float; //allocating space for float
		iorf = false; //false means it's float
	}
	void assign(int i) //assign an int
	{
		pointer = (void*) new int; //allocating space for float
		iorf = true; //true means it's int
	}
	double get() //getting the value as double. use this when comparing 2 elements
	{ //it has to be double because it's the most precise
		if(iorf) //if it's true return integer
			return (double)*(int*)pointer;
                        //(int*) convert to integer pointer
                        //*(int*) convert to integer
                        //(double)*(int*) convert to double
		else //otherwise return float 
			return (double)*(float*)pointer;
                        //(float*) convert to float pointer
                        //*(float*) convert to float
                        //(double)*(float*) convert to double
	}
	~intORfloat(){ delete pointer;} //destructor; deletes the variable
private:
	void * pointer;
	bool iorf;
};

int main()
{
    int i = 3;
    float f = 1.56;
	intORfloat yourInt(i), yourFloat(f); //create an integer and a float

    if (yourInt.get() > yourFloat.get()) //3 > 1.56
        cout << "true";
    else cout << "false";
	
	cin.get();
	return 0;
}


Now you can make an array and sort it like you normaly would:
1
2
3
4
intORfloat array[100]; //create an array
float f=44.44;
array[30].assign(f); //assign a value; int or float
array[50].get() > array[51].get() //compare 2 variables 


Note: if you are creating an array you'll have to assign a value to each object separatelly
zoran404 , I don't know how to thank you, you actually saved me.:-)
Thank you
very much
Hi again;
I write my code as you said, but I have problem in printing sorted Array in output file, which function should I choose?
Thank you very much

Last edited on
Just use the get function:
myfile << data[i].get() << endl;
The stream should automatically get rid of extra zeros, so integers will be printed without decimals after the dot.
I face this error;
msort_mix/main.cpp|107|error: passing ‘const intORfloat’ as ‘this’ argument of ‘double intORfloat::get()’ discards qualifiers [-fpermissive]|
||
I don't think you can delete a void*.
This is a bit of an overkill for such a simple problem. Oh, good, old schools.
You can greatly simplify this by using a union.
I can't get rid of this error! can anyone help me? I need this code to run correctly...
change double intOrfloat::get() to double intOrFloat::get() const
Last edited on
Thank you error removed but sorting doesn't happen !output is zero!
I just took a look at the code and realized I forgot to assign the actual values to the variables, I just created them xD

Here, fixed it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	intORfloat(float f) //constructor: making a float
	{
		pointer = (void*) new float; //allocating space for float
		*(float*)pointer = f; //THIS PART WAS MISSING
		iorf = false; //false means it's float
	}
	intORfloat(int i) //constructor: making an int
	{
		pointer = (void*) new int; //allocating space for float
		*(int*)pointer = i; //THIS PART WAS MISSING
		iorf = true; //true means it's int
	}
	void assign(float f) //assign a float
	{
		pointer = (void*) new float; //allocating space for float
		*(float*)pointer = f; //THIS PART WAS MISSING
		iorf = false; //false means it's float
	}
	void assign(int i) //assign an int
	{
		pointer = (void*) new int; //allocating space for float
		*(int*)pointer = i; //THIS PART WAS MISSING
		iorf = true; //true means it's int
	}


Also I'd like to make a small edit to the destructor:
1
2
3
4
5
6
	~intORfloat(){ //destructor; deletes the variable
		if(iorf)
			delete (int*)pointer;
		else
			delete (float*)pointer;
	}
The problem with that is that assigning post-construction will leak memory, since the original memory does not get freed.
Again, this is just overcomplicating a simple problem.

Anyways, you can allocate and assign to a value as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
private:
	void free() {
		if(iorf)
			delete (int*)pointer;
		else
			delete (float*)pointer;
		pointer=0;
	}
public:
	~intORfloat() {
		free();
	}
	// use initialization lists
	intORfloat(float f) : pointer(new float(f)), iorf(0) {}
	intORfloat(int i) : pointer(new int(i)), iorf(1) {}

	void assign(float f) //assign a float
	{
		if(!iorf) {
			// Already a float, no need to free/realloc
			*((float*)pointer) = f;
			return;
		}
		free();
		// void* conversion not required
		pointer = new float(f); //allocating space for float
		iorf = false; //false means it's float
	}
	void assign(int i) //assign an int
	{
		if(iorf) {
			// Already a int, no need to free/realloc
			*((int*)pointer) = i;
			return;
		}
		free();
		pointer = new int(i); //allocating space for float
		iorf = true; //true means it's int
	}


More problems arise when copying the class.

Probably an union is more convenient here, as already mentioned:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class IntOrFloat {
	union IntFloat {
		int m_int;
		float m_float;
	} m_data;
	bool m_isFloat;
public:
	inline IntOrFloat(int v=0) : m_isFloat(0) {m_data.m_int=v;}
	inline IntOrFloat(float v) : m_isFloat(1) {m_data.m_float=v;}
	inline bool IsFloat() const {return m_isFloat;}
	inline float GetFloat() const {return m_isFloat ? m_data.m_float : m_data.m_int;}
	inline int GetInt() const {return m_isFloat ? m_data.m_float : m_data.m_int;}
	inline void Set(float v) {m_isFloat=1; m_data.m_float=v;}
	inline void Set(int v) {m_isFloat=0; m_data.m_int=v;}
};

void PrintVar(IntOrFloat var)
{
	if(var.IsFloat())
		std::cout << "Float var, value: " << var.GetFloat() << std::endl;
	else
		std::cout << "Int var, value: " << var.GetInt() << std::endl;
	
}

int main()
{
	IntOrFloat int_var(42);
	IntOrFloat float_var(5.32f);
	IntOrFloat change_var(37);
	change_var.Set(58.2f);
	
	PrintVar(int_var); // Prints "Int var, value: 42"
	PrintVar(float_var); // Prints "Float var, value: 5.32"
	PrintVar(change_var); // Prints "Float var, value: 58.2"
	
	return 0;
}
Zoran , I get error writing those missing part, " pointer and iorf no declared in this scope!
Pages: 12