Hi this is my simple c++ programme, it has Thermister class which calculate resistance when varying Temp,but each time when calling to setTemp() eventhough it change value of Temp,value given by getResistance() mthd does not change.It always gives 1 when calling through a for loop.here is the code any help please... - #include <iostream>
-
#include <cmath>
-
class AbstractResistor
-
{
-
public:
-
virtual double getResistance() = 0;
-
};
-
-
class Thermistor:public AbstractResistor
-
{
-
private:
-
double a,b,c,Temp;
-
public:
-
Thermistor(double a,double b,double c,double Temp)
-
{
-
this->a=a;
-
this->b=b;
-
this->c=c;
-
this->Temp=Temp;
-
}
-
~Thermistor(){}
-
-
double getResistance()
-
{
-
double y=(a-(1/Temp))/c;
-
double x=sqrt(pow(b/(3*c),3)+pow((y/2),2));
-
return exp(pow(x-y/2,1/3)-pow(x+y/2,1/3));
-
}
-
void setTemp(double Temp)
-
{
-
this->Temp=Temp;
-
}
-
};
-
-
-
int main()
-
{
-
Thermistor* t=new Thermistor(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),328.15);
-
std::cout <<"Resistance of Thermistor= "<<t->getResistance();
-
-
for(int i=0;i<=100;i++)
-
{
-
t->setTemp(i+273.15);
-
std::cout << t->getResistance()<<std::endl ;
-
}
-
return 0;
-
}
-
Took another look.
Make this: -
exp(pow(x-y/2,1/3)-pow(x+y/2,1/3))
-
into this: -
exp(pow(x-y/2.0,1.0/3.0)-pow(x+y/2.0,1.0/3.0))
-
Otherwise, the result of the pow method will result in an integer instead of a double due to the divisions returning an integer value.
Hope that helps!
5 1486
No, based on the equation you give here: -
pow(x-y/2,1/3)-pow(x+y/2,1/3)
-
It will always cancel out and give 1.
Are you sure that, that's the correct formula?
Also, I'm not sure why you're not doing this, but it's standard practice in C++ to make this: -
#include <iostream>
-
#include <cmath>
-
into this: -
#include <iostream>
-
#include <cmath>
-
using namespace std;
-
It will remove the need to use std all over the place in your code.
Took another look.
Make this: -
exp(pow(x-y/2,1/3)-pow(x+y/2,1/3))
-
into this: -
exp(pow(x-y/2.0,1.0/3.0)-pow(x+y/2.0,1.0/3.0))
-
Otherwise, the result of the pow method will result in an integer instead of a double due to the divisions returning an integer value.
Hope that helps!
Just for reference, here is what the C version would look like: -
#include <stdio.h>
-
#include <math.h>
-
double get_resistance(double a,double b,double c,double Temp){
-
double y=(a-(1/Temp))/c;
-
double x=sqrt(pow(b/(3*c),3)+pow((y/2),2));
-
return exp(pow(x-y/2.0,1.0/3.0)-pow(x+y/2.0,1.0/3.0));
-
}
-
-
int main(){
-
double temp=328.15;
-
printf("%.3f",get_resistance(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),temp));
-
int i=0;
-
while(i<=100){
-
temp=i+273.15;
-
printf("%.3f\n",get_resistance(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),temp));
-
i++;
-
}
-
//system("PAUSE");
-
return 0;
-
}
-
http://safe.abelgancsos.com/codepost...ect.php?id=390
I do not recommend writing out using namespace std; unless you are absolutely certain that no conflicts will arise. I prefer to omit that line and instead do std::symbol_name, which is what the person asking did. The reason is that importing all of the contents in an stl header can potentially conflict or overshadow something you have written. Maybe I am paranoid about things that may have a low probability of happening, but I would rather see people develop good habits. The scope identifier is there so you can use what you need and ignore the rest. Other than that, use decimals (double) with pow. I suspect that pow is raising a number to 0 because 1/3 is an integer division. Integer divisions in which the numerator is smaller than the denominator yields 0. A number raised to 0 yields 1.
Amen to that
One time I had to merge I Microsoft code with mine and we both used "group". Took six months to go through all that code and install a namespace.
Using std:: everywhere guarantees that library code does not conflict with yours. Of course, you should install your own namespace so you don't conflict with someone else.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Robert Brewer |
last post by:
>>> class A(types.DictType):
.... def __getitem__(self, key):
.... return types.DictType.__getitem__(self, key)
....
>>> a = A()
>>> a = 3
>>> a.__getitem__ = lambda x: "w00t"
>>>...
|
by: inquirydog |
last post by:
Hi-
One frusterating thing for me with xsl is that I don't know
how to make xslt throw some sort of exception when a value-of path
does not exist. For instance, suppose I have the following...
|
by: Earl Teigrob |
last post by:
I am having trouble creating an Attribute on a new node. The
NodeObject.SetAttribute(x,y) method does not exist for the "RecordElement"
node. What am I missing???
Earl
XmlDocument d = new...
|
by: Toco |
last post by:
Hello. I have method (called GetKey) in a class that returns a string.
Also, I have in another class, a method which makes a call to the GetKey
method. What I wish to do is to do an evaluation of...
|
by: Wayne Shu |
last post by:
Hi everyone:
I am reading Bjarne Stroustrup's The C++ Programming
Language(Special Edition).
In section 7.3, bs wrote "Like the semantics of argument passing,
the semantics of function value...
|
by: analysis |
last post by:
what method does the compiler generate?
class A{
};
|
by: naveenmrs |
last post by:
How many values can a value-returning method use in its return statement?
|
by: ramveers |
last post by:
I ma creating a toolbar for IE. I am setting a parent of toolbar dynamically by using SetParent method of window API.
My problem is that SetParent method does not set parent and return zero(IntPtr)....
|
by: zamaam0728 |
last post by:
I am at the end of my first semester of C++, and I'm not sure what I should do to make this program meet the requested specifications. The assignment is as follows:
Write a function called...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |