473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Value return by a method does not change???

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...
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3. class AbstractResistor
  4. {
  5. public:
  6.     virtual double getResistance() = 0;
  7. };
  8.  
  9. class  Thermistor:public AbstractResistor
  10. {
  11. private:
  12.     double a,b,c,Temp;
  13. public:
  14. Thermistor(double a,double b,double c,double Temp)
  15. {
  16.    this->a=a;
  17.    this->b=b;
  18.    this->c=c;
  19.    this->Temp=Temp;
  20. }
  21. ~Thermistor(){}
  22.  
  23. double getResistance()
  24. {
  25. double y=(a-(1/Temp))/c;
  26. double x=sqrt(pow(b/(3*c),3)+pow((y/2),2));
  27. return exp(pow(x-y/2,1/3)-pow(x+y/2,1/3));
  28. }
  29. void setTemp(double Temp)
  30. {
  31.     this->Temp=Temp;
  32. }
  33. };
  34.  
  35.  
  36. int main()
  37. {
  38. Thermistor* t=new Thermistor(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),328.15);
  39. std::cout <<"Resistance of Thermistor= "<<t->getResistance();
  40.  
  41. for(int i=0;i<=100;i++)
  42. {
  43.     t->setTemp(i+273.15);
  44.     std::cout << t->getResistance()<<std::endl ;
  45. }
  46. return 0;
  47. }
  48.  
May 23 '15 #1

✓ answered by computerfox

Took another look.
Make this:
Expand|Select|Wrap|Line Numbers
  1. exp(pow(x-y/2,1/3)-pow(x+y/2,1/3))
  2.  
into this:
Expand|Select|Wrap|Line Numbers
  1. exp(pow(x-y/2.0,1.0/3.0)-pow(x+y/2.0,1.0/3.0))
  2.  
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
computerfox
276 100+
No, based on the equation you give here:
Expand|Select|Wrap|Line Numbers
  1. pow(x-y/2,1/3)-pow(x+y/2,1/3)
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3.  
into this:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
It will remove the need to use std all over the place in your code.
May 23 '15 #2
computerfox
276 100+
Took another look.
Make this:
Expand|Select|Wrap|Line Numbers
  1. exp(pow(x-y/2,1/3)-pow(x+y/2,1/3))
  2.  
into this:
Expand|Select|Wrap|Line Numbers
  1. exp(pow(x-y/2.0,1.0/3.0)-pow(x+y/2.0,1.0/3.0))
  2.  
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!
May 23 '15 #3
computerfox
276 100+
Just for reference, here is what the C version would look like:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. double get_resistance(double a,double b,double c,double Temp){
  4.  double y=(a-(1/Temp))/c;
  5.  double x=sqrt(pow(b/(3*c),3)+pow((y/2),2));
  6.  return exp(pow(x-y/2.0,1.0/3.0)-pow(x+y/2.0,1.0/3.0));
  7.  
  8. int main(){
  9.  double temp=328.15;
  10.  printf("%.3f",get_resistance(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),temp));
  11.  int i=0;
  12.  while(i<=100){
  13.   temp=i+273.15;
  14.   printf("%.3f\n",get_resistance(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),temp));
  15.   i++;
  16.  }
  17.  //system("PAUSE");
  18.  return 0;
  19.  
http://safe.abelgancsos.com/codepost...ect.php?id=390
May 23 '15 #4
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.
May 24 '15 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
May 26 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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" >>>...
14
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...
2
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...
3
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...
4
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...
1
by: analysis | last post by:
what method does the compiler generate? class A{ };
3
by: naveenmrs | last post by:
How many values can a value-returning method use in its return statement?
0
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)....
33
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...
0
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$) { } ...
0
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...
0
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...
0
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
0
BarryA
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...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.