Why do I keep getting the same value of a variable for each iteration? c++ -


i new c++ , keep getting same value variable 'w'..

#include <iostream> #include <string>  #define noexe 10 #define winit 0.9 #define wfinal 0.2  int main() {     (int nexeno = 0; nexeno<noexe; nexeno++)     {         double w;          w = (((noexe - nexeno)/noexe)*((noexe - nexeno)/noexe))*(winit - wfinal) + wfinal;         std::cout << w << "\n";     } } 

and output is

0.9 0.2 0.2 0.2 ....... 

(noexe - nexeno)/noexe done entirely in integer arithmetic. since nexeno < noexe entire previous expression zero.

thus left final term wfinal.

to force floating point arithmetic, @ least 1 operand must of floating point type (in case double). clean up, , rid of nasty macros, define constants follows:

constexpr int    noexe   = 10; // use const instead of constexpr c++03 constexpr double noexe_d = noexe; constexpr double winit   = 0.9; constexpr double wfinal  = 0.2; 

and use noexe_d in calculation instead. i.e. (noexe_d - nexeno)/noexe_d).


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -