I am writing a program for statistics, and I need to calculate the quartiles. The problem is that the installation of else going on all the time, and it is not clear why the code does not work:

In my case, n=130 and on i=2 and i=4 should work

tmp == (ceil(tmp)-1) round to larger and subtract one

 void KvartilC(vector<int>&X,float n) { for(int i=1;i<=4;i++) { float tmp = (i*n) / 4; // если число целое то подходит, если нет то //квартиля нет if (tmp == (ceil(tmp)-1)) // проверка на целочисельность { cout <<"Qvartil_"<<i<<" : "<< X[tmp]<< endl; } else { cout << "Ne isnuje Qvartila_"<<i<< endl; } } } 
  • Does n really have to be a float or is it really a whole? - MBo
  • @MBo, integer, I thought that the reason is integer division - Awesome Man
  • one
    Then there is no need to mess with real and rounding, it is enough to check that (i*n)%4==0 or (i*n)&3==0 - MBo

1 answer 1

And just count and see what prevents?

 float n = 130; for(int i = 1; i <= 4; i++) { float tmp = (i*n) / 4; cout << "tmp = " << tmp << " ceil.. = " << (ceil(tmp)-1) << endl; } 

gives

 tmp = 32.5 ceil.. = 32 tmp = 65 ceil.. = 64 tmp = 97.5 ceil.. = 97 tmp = 130 ceil.. = 129 

Any questions? :) Read carefully how ceil performs rounding ...

  • Lol, thanks. I thought that ceil rounds off only fractional numbers. And how else can you check the number on an integer? Maybe there is a function in C ++ - Awesome Man
  • one
    Use the floor :) Still - casting to int drops the fractional part. - Harry