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
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
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:
class intORfloat
{
public:
intORfloat(){} //default constructor
intORfloat(float f) //constructor: making a float
{
pointer = (void*) newfloat; //allocating space for float
iorf = false; //false means it's float
}
intORfloat(int i) //constructor: making an int
{
pointer = (void*) newint; //allocating space for float
iorf = true; //true means it's int
}
void assign(float f) //assign a float
{
pointer = (void*) newfloat; //allocating space for float
iorf = false; //false means it's float
}
void assign(int i) //assign an int
{
pointer = (void*) newint; //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
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]|
||
intORfloat(float f) //constructor: making a float
{
pointer = (void*) newfloat; //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*) newint; //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*) newfloat; //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*) newint; //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:
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: