How do i write this formula in c++?

331 * squareroot 1 + t/273

 
 v = 331 * (sqrt((double)1 + t/273)) 
Umm...what is your question? Seems like you already answered that
What is the type of t?
t is a number i set !
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main ()
{
int t, sum, n;
double v;
t = 0;
sum = 20;
n = 4 ;
cout << left << fixed << setprecision(2);
cout << setw(10) << " t " << setw(10) << " v " << endl;
cout << setw(10) << sum << endl;
v = 331 * (sqrt((double)1 + sum/273)) ;
while (n <= 40)
{
sum = 20 + t;
t = t + 2;
n = n + 4 ;
v = 331 * (sqrt((double)1 + sum/273)) ;
cout << setw(10) << sum << setw(10) << v << endl;
}

system("pause");
return 0;
}
int t, sum, n; t is an integer. So is sum. So is 273

Hence either t/273 or sum/273 will carry out an integer divide. You need a floating-point divide, so put sum/273.0 where 273.0 is of type double and the division will be done properly, giving a result of type double.

See the tutorial for how to define different types of constant.
http://www.cplusplus.com/doc/tutorial/constants/
Last edited on
Topic archived. No new replies allowed.