-1

While I was testing someting, I have created a vector of type float, and put the values 1/i into that vector;however, while reading the values, the output is printed as integers and not as float.

#include <stdio.h>
#include <vector>
#include <iterator>

int main(int argc, char const *argv[])
{
    std::vector<float> testVec;
    for (int i = 1; i < 5; ++i)
    {
        float v = 1/i;
        testVec.push_back(v);
    }

    std::vector< float >::iterator it = testVec.begin();
    for (; it!=testVec.end(); ++it)
    {
        printf("The value of the iterator: %f, *it);
    }   
    return 0;
}

So, what is the problem with this ? Is it related with the iterator ? I mean I have not much experienced with them.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Our
  • 986
  • 12
  • 22

1 Answers1

1

The reason behind your issue is that 1/i where i is of type integer is integer arithmetic and will give integer as output.
1.0 in place of 1 would help you. :)

Aditi Rawat
  • 784
  • 1
  • 12
  • 15