https://www.codechef.com/problems/AVG

I am getting the wrong answer

#include
using namespace std;

int main() {
int freq,n,k,v,num;
float del_no,is_int;
long int sum;
cin>>freq;
for(int i=0;i<freq;i++)
{
sum=0;
cin>>n>>k>>v;
for(int i=0;i<n;i++)
{
cin>>num;
sum=sum+num;
}
del_no = v*(n+k)-sum;
del_no = del_no/(float)k;

  is_int = del_no-(int)del_no ;
  if( is_int == 0 && del_no > 0)
  cout<<del_no<<endl;
  else
  cout<<"-1"<<endl;
}
return 0;

}

1 Like

This is because of data types you have taken.
The size of float data type memory is greater then int data type…

#del_no = del_no/(float)k;
In above statement you typecast k to float which is possible as memory size that float data type uses is bigger then int data type.

but in below statement
#is_int = del_no-(int)del_no;

you have convert float to int again so, there is a chance of data loss on type conversion because here memory size of float is greater hence on type casting block size is shrinking and some data can be loss.

yeah, I see it now. thanks, @nikhilkumawat0 . but how do I take the integer part out from a floating point number??

Check solution of others you will get the answer.
There is different logic where you don’t need to use floating data type and don’t
even need to extract integer from floating point data type.