Hello, all.

I am trying to calculate entropy, and I cannot get the calculations correct, or match the results with the assigned if-statements. I get a number that differentiates a bit from the answer that I want. So far, this is my code:

``````#include <iostream>
using namespace std;
float Entropy(float z, float z1, float e1, float z0, float e0){
float e;
e= e0 + (e1-e0)*((z-z0)/(z1-z0));
return e;
}
int main() {
float z,e,z1,e1,z0,e0;
cout << "Enter temp between 150 - 500: ";
cin >> z;
if(z==150){
e=7.2810;
}
else if(z==200){
e=7.5081;
}
else if(z==250){
e=7.7710;
}
else if(z==300){
e=7.8941;
}
else if(z==400){
e=8.2236;
}
else if(z==500){
e=8.5153;
}
else if(z>150&&z<200){
e=entropy(z,200,7.5081,150,7.2810);
}
else if(z>200&&z<250){
e=entropy(z,250,7.7710,200,7.5081);
}
else if(z>250&&z<300){
e=entropy(z,300,7.8941,250,7.7710);
}
else if(z>300&&z<400){
e=entropy(z,400,8.2236,300,7.8941);
}
else if(z>400&&z<500){
e=entropy(z,500,8.5153,400,8.2236);
}
else{
cout << "Temperature out of Range" << endl;
}
cout << "The Entropy is: \t" << Entropy << e << " Kj/(Kg.K)" << endl;
return 0;
}``````

Say I input 250, I would expect 7.7710 as output, but in return the value would add an extra 10. How do I make this more precise? I don't want there to be a 10 added to every output. So instead of 17.7710, I want 7.7710. I feel like this is an easy fix, but something is causing this to add 10 and I am not sure what.

Is your C++ compiler not case sensitive?
You define a function `Entropy` in your code you call a function `entropy`.

On line 9, the variables z1,e1,z0,e0 are not used in your main function.
Doesn't your C++ compiler give you a warning about that?
Did you turn warnings off?
Translated your code in C#, which wasn't that hard as only the IO-syntax is a bit different.
It worked.
Perhaps something wrong with the formatting of `cout`?

Remove the extra `Entropy` from line #48 to: `cout << "The Entropy is: \t" << e << " Kj/(Kg.K)" << endl;`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.