template function with median

Z

zfareed

I created a program to determine the medians of arrays, be it of type
int or float. I have used a template function and the problem I'm
having is returning the float median for even arrays. Can anyone help?
include <iostream>

using namespace std;

template <class T>
T getMedian(T* array, int size)
{
int middle = 0;


cout << endl;
cout << "Processing " << size << " element array" << endl;
cout << "Array elements: ";
for(int i=0;i<size;i++)
cout << array << " , ";
cout << endl;

if(size % 2 == 0)
{
middle = ((array[size/2] + array[(size/2)+1])/2); // this
is where the problem lies
return middle;
}
else
{
middle = (size)/ 2;
return array[middle];

}



}

int main()
{
int A1[] = {1,9,3,4,6};
int A2[] = {4,1,9,3,6,2};
float A3[] = {1.1,4.1,3.1,5.2,6.3};
float A4[] = {4.1,1.2,9.3,3.4,6.1,2.7};

int medianA1;
float medianA3;
int medianA2;
float medianA4;

sort(A1,A1+5);
sort(A2,A2+6);
sort(A3,A3+5);
sort(A4,A4+6);

medianA1 = getMedian (A1, (sizeof A1 / sizeof A1[0]));
cout << "Median from A1 is " << medianA1 << endl;
medianA2 = getMedian (A2, (sizeof A2 / sizeof A2[0]));
cout << "Median from A2 is " << medianA2 << endl;
medianA3 = getMedian (A3, (sizeof A3 / sizeof A3[0]));
cout << "Median from A3 is " << medianA3 << endl;
medianA4 = getMedian (A4, (sizeof A4 / sizeof A4[0]));
cout << "Median from A4 is " << medianA4 << endl;

system("Pause");
return 0;
}

// the compilation error: In function `T getMedian(T*, int) [with T =
float]':
instantiated from here
[Warning] converting to `int' from
`float'
 
J

Jim Langston

I created a program to determine the medians of arrays, be it of type
int or float. I have used a template function and the problem I'm
having is returning the float median for even arrays. Can anyone help?
include <iostream>

using namespace std;

template <class T>
T getMedian(T* array, int size)
{
int middle = 0;


cout << endl;
cout << "Processing " << size << " element array" << endl;
cout << "Array elements: ";
for(int i=0;i<size;i++)
cout << array << " , ";
cout << endl;

if(size % 2 == 0)
{
middle = ((array[size/2] + array[(size/2)+1])/2);


Operator precidence. / has a higher precidence that + or -. So this
becomes:

middle = (array[size/2] + (array[size/2 + 1] / 2 );
Not what you wanted. So you have to use parenthesis to get the precidence
you want. Try changing it to:

middle = ( array[size/2] + array[size / 2 + 1] ) / 2;
return middle;
}
else
{
middle = (size)/ 2;
return array[middle];

}



}

int main()
{
int A1[] = {1,9,3,4,6};
int A2[] = {4,1,9,3,6,2};
float A3[] = {1.1,4.1,3.1,5.2,6.3};
float A4[] = {4.1,1.2,9.3,3.4,6.1,2.7};

int medianA1;
float medianA3;
int medianA2;
float medianA4;

sort(A1,A1+5);
sort(A2,A2+6);
sort(A3,A3+5);
sort(A4,A4+6);

medianA1 = getMedian (A1, (sizeof A1 / sizeof A1[0]));
cout << "Median from A1 is " << medianA1 << endl;
medianA2 = getMedian (A2, (sizeof A2 / sizeof A2[0]));
cout << "Median from A2 is " << medianA2 << endl;
medianA3 = getMedian (A3, (sizeof A3 / sizeof A3[0]));
cout << "Median from A3 is " << medianA3 << endl;
medianA4 = getMedian (A4, (sizeof A4 / sizeof A4[0]));
cout << "Median from A4 is " << medianA4 << endl;

system("Pause");
return 0;
}

// the compilation error: In function `T getMedian(T*, int) [with T =
float]':
instantiated from here
[Warning] converting to `int' from
`float'
 
R

red floyd

const T* array, int size;

But you should look into std::vector.
{
int middle = 0; T middle = T(0);


cout << endl;
cout << "Processing " << size << " element array" << endl;
cout << "Array elements: ";
for(int i=0;i<size;i++)
cout << array << " , ";
cout << endl;

if(size % 2 == 0)
{
middle = ((array[size/2] + array[(size/2)+1])/2);


Operator precidence. / has a higher precidence that + or -. So this
becomes:

middle = (array[size/2] + (array[size/2 + 1] / 2 );
Not what you wanted. So you have to use parenthesis to get the precidence

you want. Try changing it to:

No, he parenthesized it corrctly.

return middle;
}
else
{
middle = (size)/ 2;
return array[middle];

}



}

int main()
{
int A1[] = {1,9,3,4,6};
int A2[] = {4,1,9,3,6,2};
float A3[] = {1.1,4.1,3.1,5.2,6.3};
float A4[] = {4.1,1.2,9.3,3.4,6.1,2.7};

int medianA1;
float medianA3;
int medianA2;
float medianA4;

sort(A1,A1+5);
sort(A2,A2+6);
sort(A3,A3+5);
sort(A4,A4+6);

medianA1 = getMedian (A1, (sizeof A1 / sizeof A1[0]));
cout << "Median from A1 is " << medianA1 << endl;
medianA2 = getMedian (A2, (sizeof A2 / sizeof A2[0]));
cout << "Median from A2 is " << medianA2 << endl;
medianA3 = getMedian (A3, (sizeof A3 / sizeof A3[0]));
cout << "Median from A3 is " << medianA3 << endl;
medianA4 = getMedian (A4, (sizeof A4 / sizeof A4[0]));
cout << "Median from A4 is " << medianA4 << endl;

system("Pause");
return 0;
}

// the compilation error: In function `T getMedian(T*, int) [with T =
float]':
instantiated from here
[Warning] converting to `int' from
`float'

Look at your declaration of "middle". It's an int. It should be of
type T. Otherwise, when you calculate the "middle" value of a float
array, you get the warning.
 
M

Marcus Kwok

red floyd said:
I created a program to determine the medians of arrays, be it of type
int or float. I have used a template function and the problem I'm
having is returning the float median for even arrays. Can anyone help?
include <iostream>

using namespace std;

template <class T>
T getMedian(T* array, int size)
{
int middle = 0;


cout << endl;
cout << "Processing " << size << " element array" << endl;
cout << "Array elements: ";
for(int i=0;i<size;i++)
cout << array << " , ";
cout << endl;

if(size % 2 == 0)
{
middle = ((array[size/2] + array[(size/2)+1])/2);
return middle;
}
else
{
middle = (size)/ 2;
return array[middle];
}
}

// the compilation error: In function `T getMedian(T*, int) [with T =
float]':
instantiated from here
[Warning] converting to `int' from
`float'


Look at your declaration of "middle". It's an int. It should be of
type T. Otherwise, when you calculate the "middle" value of a float
array, you get the warning.


Actually, he is using "middle" in two different ways. If the size is
even, he is using it directly as the return value. If the size is odd,
he is using it as the index into the array. I think if he makes its
usage consistent (either by using it as an array index in both cases, or
as the return value in both cases) then it should help.
 
Z

zfareed

red floyd said:
I created a program to determine the medians of arrays, be it of type
int or float. I have used a template function and the problem I'm
having is returning the float median for even arrays. Can anyone help?
include <iostream>
using namespace std;
template <class T>
T getMedian(T* array, int size)
{
int middle = 0;
cout << endl;
cout << "Processing " << size << " element array" << endl;
cout << "Array elements: ";
for(int i=0;i<size;i++)
cout << array << " , ";
cout << endl;
if(size % 2 == 0)
{
middle = ((array[size/2] + array[(size/2)+1])/2);
return middle;
}
else
{
middle = (size)/ 2;
return array[middle];
}
}
// the compilation error: In function `T getMedian(T*, int) [with T =
float]':
instantiated from here
[Warning] converting to `int' from
`float'

Look at your declaration of "middle". It's an int. It should be of
type T. Otherwise, when you calculate the "middle" value of a float
array, you get the warning.

Actually, he is using "middle" in two different ways. If the size is
even, he is using it directly as the return value. If the size is odd,
he is using it as the index into the array. I think if he makes its
usage consistent (either by using it as an array index in both cases, or
as the return value in both cases) then it should help.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply- Hide quoted text -

- Show quoted text -


Thanks. I got it to work.
 
O

Old Wolf

I created a program to determine the medians of array

if(size % 2 == 0)
{
middle = ((array[size/2] + array[(size/2)+1])/2);
// this is where the problem lies
return middle;
}

Firstly, it should be -1 rather than +1. For example if your
array has 2 elements you want to average array[0] and array[1].
Currently your code would do array[1] and array[2].

Secondly, you should check that size > 0 before doing this.

Thirdly, 'middle' is an int, so whatever the median actually is
will be truncated down to an int. You could have just written:
return (array[size / 2] + array[size / 2 - 1]) / 2.;

Finally, your function returns int if the array is an array of
ints, but in that case the median might not be an integer, so it
will get truncated. You might want to consider always returning
a float (or preferably a double).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top