Ask your own question, for FREE!
Mathematics 22 Online
OpenStudy (bahrom7893):

Alchemista or anyone who knows C++ , i have a question (related to math too)

OpenStudy (bahrom7893):

Anyway I'm just making an easy converter, but i was wondering about this right.. when converting from Far to Celsius, why is it that when i do this: cout << value << " Farenheit is " << value*5/9-32*5/9 << " Celsius." << endl; the program works, but..

OpenStudy (bahrom7893):

when I did: cout << value << " Farenheit is " << (value-32)*(5/9) << " Celsius." << endl; it didn't ?

OpenStudy (bahrom7893):

if this is any help when i type in 90 farenheit it is showing me 0 celsius but when i use the first method i do get 33 celsius

OpenStudy (anonymous):

You need to be aware of the various data types in C++

OpenStudy (anonymous):

When you perform division on two integers it will perform integer division. The remainder will simply be chopped off instead of being included in the quotient.

OpenStudy (anonymous):

(value - 32.0) * (5.0/9.0)

OpenStudy (bahrom7893):

ohh okay i see.. so i have to use .0 after decimals?

OpenStudy (bahrom7893):

i mean after integers..

OpenStudy (anonymous):

when .0 is added to a literal the compiler will interpret it as a floating point type

OpenStudy (bahrom7893):

oh okay thanks =)!

OpenStudy (bahrom7893):

this is what my stuff looks like so far..

OpenStudy (bahrom7893):

#include <iostream> using namespace std; int main () { char choice; float value; cout << "Select choice:" << endl << "1) Kilometers -> Meters" << endl << "2) Meters -> Kilometers" << endl << "3) Feet -> Meters" << endl << "4) Meters -> Feet" << endl << "5) Farenheit -> Celsius" << endl << "6) Exit" << endl; for ( ;; ) { cout << "Select: " << flush; cin >> choice; switch ( choice ) { case '1': cout << "Kilometers=" << flush; cin >> value; cout << value << " kilometers is " << value*1000.0 << " meters." << endl; break; case '2': cout << "Meters=" << flush; cin >> value; cout << value << " meters is " << value/1000.0 << " kilometers." << endl; break; case '3': cout<< "Feet=" << flush; cin >> value; cout << value << " feet is " << value*0.3048 << " meters." << endl; break; case '4': cout<< "Meters=" << flush; cin >> value; cout << value << " meters is " << value/0.3048 << " feet." << endl; break; case '5': cout<< "Farenheit=" << flush; cin >> value; cout << value << " Farenheit is " << (value-32.0)*(5.0/9.0) << " Celsius." << endl; break; case '6': return 0; default: cout << "Unknown choice!" << endl; } } }

OpenStudy (bahrom7893):

that's if u're interested.. i mean nothing serious.. Ohhh can u differentiate too?

OpenStudy (anonymous):

What do you mean? Like numerically? Or symbolically?

OpenStudy (bahrom7893):

both but id say numerically would be good enough..

OpenStudy (anonymous):

C++ is a general purpose programming language and its very low level (close to the machine). The idea of a mathematical function doesn't exist so you can't really symbolically differentiate.

OpenStudy (bahrom7893):

i see.. i read that before in a thread on some programming site..

OpenStudy (anonymous):

You could write all the code to do this, including all the various rules (chain rule, product rule, power rule, etc) and then develop a way to encode symbolic mathematical expressions but this gets very complicated.

OpenStudy (bahrom7893):

okay so it is possible i might do it eventually as a project or something.. now how about numerically?

OpenStudy (anonymous):

You can also just numerically differentiate, and that's very simple. Recall the difference quotient: \[\frac{f(x + h) - f(x)}{h}\] To take the numerical approximation of the derivative at x, just use a very small h.

OpenStudy (bahrom7893):

yes..

OpenStudy (anonymous):

Why ".." is there something you don't understand?

OpenStudy (bahrom7893):

no im just thinking of putting that into code.. can you give me an example?

OpenStudy (bahrom7893):

ohh wait a second.. hmm if i did h=0, then it would give me an error wouldnt it?

OpenStudy (anonymous):

err

OpenStudy (anonymous):

thats wrong one second

OpenStudy (anonymous):

float h = 0.001; int x = 5; int approx = ((x + h)*(x + h) - x*x)/h;

OpenStudy (bahrom7893):

hmm but what if i wanted a derivative of some function.. suppose: x^2+x+1 would that be possible? because u are saying that x is an integer already..

OpenStudy (bahrom7893):

like f(x) = x^2+x+1 f'(5)-?

OpenStudy (anonymous):

its simpler just to define a function, but im assuming you haven't gotten that far yet.

OpenStudy (bahrom7893):

okay anyway i gtg will continue tomorrow thanks for the help, bye

OpenStudy (anonymous):

typedef float FUNC_REALTOREAL ( float input ); float myPolynomial ( float input ) { return input * input + input + 1; } float derivative ( FUNC_REALTOREAL *realToRealFunc, float x, float h ) { return ( realToRealFunc ( x + h ) - realToRealFunc ( x ) ) / h; } int main () { int result = derivative ( myPolynomial, 5, 0.001 ); }

OpenStudy (anonymous):

Some of the above might not be accessible until you learn a bit more C/C++.

OpenStudy (bahrom7893):

ALCHEMISTA I NEED YOUR HELP!!! go to a new thread please!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!