### median not showing up as a decimal

my median is never a decimal for some reason how can i fix it show it shows up as decimal at appropriate times.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````#include using namespace std; const int N=10; void input(double &s, int arynum[]){ s=0; for (int i = 0; i>arynum[i]; s= s+arynum[i]; } } void swap(int &a, int &b){ int temp; temp=a; a=b; b=temp; } void sort(int arynum[]){ for (int a=0; aarynum[b]){ swap (arynum[a], arynum[b]); } } } } double calc (double sum, int arynum[]){ double mean=sum/N; } void output(double sum, int arynum[]){ cout<<"Your mean is: "<< sum/N <

numbes is an array of int. Any int divided by an int gives an int.

What is 3/2? One. What is 6/8? Zero.

Don't divide an int by an int if you want a decimal answer.
If you divide by 2.0 rather than just 2, the compiler will use double arithmatic for the division. Or use a double cast on one of the operands.

Andy

PS Why are you using this?

`int size = sizeof(numbers)/sizeof(int);`

It will just return the N you used to create the array. So you might as well use N rather than size (as you are everywhere else).
Last edited on
what do you mean by double cast on operands? sorry i am new to c++
casting in C and C++ is to take an object of one type and turn it into another type. For your own classes you'd have to write the functionality yourself, but for simple primitives like int, it's part of the language.

To cast an int x as a double, you can use the C style:
`(double) x`
or the (safer) C++ style
`static_cast<double> (x)`

like this
 ``12345678910`` ``````#include using std::cout; int main(){ int x = 7; double y0 = 8/x; double y1 = 8/static_cast (x); cout << "y0 = " << y0 << std::endl << "y1 = " << y1 << std::endl; }``````
Last edited on
Or even a function-style cast, e.g. `double(x)`
Thanks got it working now.
Topic archived. No new replies allowed.