I have defined a variable dX = (b - a)/N, where a = 0, b = 3, and N = 15; however, when I print dX, it always gives me a value of zero which makes no sense. I'm not sure what in my code is causing this to occur. I've tried using different allocations for the variables, but nothing seems to work. I even tried putting it inside and outside int main(), but that didn't do anything either. Does anyone have a suggestion?
a, b and N are integers so the result of (b - a)/N will also be an integer. You could change one of the variables to a double or use a cast on one of the sides of /. double dX = static_cast<double>(b - a) / N;
Thank you for that. I did not know that C++ won't accept two different data types. In the long run, is it generally better, storage wise, to use a cast or use double for all my values?
Use the type that makes more sense. N should be an integer because that is what it is, and you can't use it to specify the size of the array if it isn't an integer. a and b are probably better as doubles.
C++ accepts two types, but it'll cast for you and won't care if you lose data or not. Computers have no idea how to add 3 to 4.5678. It has to make them of the same type.
C++ accepts two types, but it'll cast for you and won't care if you lose data or not. Computers have no idea how to add 3 to 4.5678. It has to make them of the same type.
I think I'm so used to working with MATLAB that I've started to take for granted the basic operations of a computer. :-)
Haha no worries :D Just things you should keep in mind. On a semi side note, most computers anymore have a separate processor entirely for floating point arithmetic, which you should see is another reason why a computer can't add integers and floating points