Question

Why would I be getting an int conversion to float wrong in c++? At a point in a program I am explicitly converting an integer with value 10 or 14 to float, and I get 0. Why would that be ? I tried static_cast, gives me same result. Since both int and float are 4 bytes, I am thinking int to float conversion is not a demotion or promotion by size ? Is it related to rounding errors etc ? Can someone please explain.

Here is the code( There is an alteration here which works fine, but I still don't know why) ---

#include <iostream>
using namespace std;

int main()
{

int n;
int sum;

cout << "Please enter the number:";
cin >> n;

int *elements = new int[n];

cout << "Please enter the elements:";
for(int i=0; i<n; i++)
{
   cin >> elements[i];
}
cout << endl;

///// this approach, gives the sum right, but float(sum) fails and always gives zero.

for(int i=0, sum=0; i < n; i++)
{
   sum = sum + elements[i];
}

////// This works, when sum is initialised outside of for loop. float(sum) does the right conversion as well.
//sum = 0;
//for(int i=0; i < n; i++)
//{
//   sum = sum + elements[i];
//}

cout << " float n is " << float(n) << " float sum is "<< float(sum) << endl;
// float(sum) is zero, when sum is initialised from within for loop.

delete[] elements;

return 0;
}

command to compile -> g++ 3.2.cpp -o 3.2

Inputs: n = 4; elements = 1 2 3 4

It feels like something minor that I am overlooking.

Thanks for the help.

Was it helpful?

Solution

You've got two different sum variables. The sum you're initializing in the for loop is actually scoped to the for loop, just like the i counter. When you do for (int i = 0, sum = 0; ... you're creating two new variables, i and sum, for the loop. Thus, the sum variable you're updating in the for-loop is not the sum variable you think it is (it's shadowing the sum variable you want).

This is why it's important to post your real, actual code from the start. Little details like this matter a lot.

OTHER TIPS

The problem in your program is with the scope of your sum variable(s). Note that in its present form your program has two instances of the sum variable.

One of the sum variables is inside your for loop.

for(int i=0, sum=0; i < n; i++) { sum = sum + elements[i]; }

Inside the for loop it has a local scope, meaning that the sum variable exists only for the duration of the for loop. Outside the for loop it does not exist!

Whereas, in this line of your code cout << " float n is " << float(n) << " float sum is "<< float(sum) << endl; the sum variable that you are trying to output is the one declared at the beginning of your program as int sum; which is also the second instance of the sum variable and it has not been initialized or assigned a value. This is what your problem is. How can the compiler cast your int sum into a float sum and output it onto the screen when it has no value assigned to it?

The program works fine as you say for yourself when sum is initialized outside the for loop. This is because in this case the sum variable used is the one declared at the top (after int n). This version of the sum variable has a global scope for the duration of the whole code body, and when you output it in the end the compiler recognizes the sum as having been initialized to zero and incremented inside the for loop.

So the output is the sum of all the numbers inside the array elements[i].

Remember that the lifetime or scope of a local variable is just within a procedure or a code block whereas the scope of a global variable is throughout the program, meaning it is recognized wherever it is used or referenced.

Hope this helps!

If you are doing division, integer divison truncates. What you can do instead is do the division using floats first, then display using ints. For example, static_cast<float>(10/14) will have no effect because the result of 10/14 is 0, and you're converting 0 to float, which is still 0. If you are attempting to store the result in an int, it will be zero because of truncation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top