You are viewing a single comment's thread. Return to all comments →
i am trying to do the same thing but the output remains 0.000000 i dont know how?
Are your variables of type double instead of int?
Same for me...
yea.. same for me also...
Most programing languages do not like when variable types mix with eachother. An integer divided by an integer produces a floating point in most cases, but the computer only works with variables that all have the same data type; such as a double divided by a double producing a double. The problem seems like you are having is because of inconsistant varible types.
The folowing case does not work because there are two differant variable types:
double = integer / integer
A solution to this problem is to use a STATIC CAST. The following line could be read as "turn my (positiveIntegerCount) into a double before i divide by its length":
myDoubleAnswer = static_cast<double>(positiveIntCount) / n
typecast into float
apply casting while dividing number of positive(negative or zeroes)by number of elements