Guys...I was adding more parts to that equation solver program to make it a calculator of sorts...I need to know how to change degrees into radians..some help please?
This is a discussion on How to convert angle degrees into radians within the C++ Programming forums, part of the General Programming Boards category; Guys...I was adding more parts to that equation solver program to make it a calculator of sorts...I need to know ...
Guys...I was adding more parts to that equation solver program to make it a calculator of sorts...I need to know how to change degrees into radians..some help please?
PI radians == 180 degrees
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
Did you consider google?
This is the third or so on the list of MANY that come back if you enter "degrees to radians" into google.
http://www.teacherschoice.com.au/Mat...les/Angles.htm
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
This then? And yeah I just checked google before Salem replied lol sorry...I'll be careful in future to use the forum as a last resort. And the "r" variable is a char variable I already declared in the Class.(as are degrees,radians and pi..)Code:cout << "Enter angle in (d)degrees or (r)radians? - "; if (r == 'd') { cout << "Enter angle theta (degrees) -"; cin >> degrees; a = (degrees * 180)/pi; } else if (r == 'r') { cout << "Enter angle theta(radians) -"; cin >> radians; a == radians; }
Last edited by SVXX; 10-01-2007 at 03:25 AM.
Your degrees to radians is wrong - try reading the page I posted for you.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Its the same one I used o.O damn I made a mistake
It isn't working...Let me post the problem -:
This is the first part.
The Trigonometrics PartCode:class Program { private: float a,x,b,c,D,d,f; //Teh math vars. int n; //You guessed it....ANOTHER math variable. long double radians, degrees, Sine, Tangent, Cosine, Arctan, Arcsine, Arccos; //Trigonometry. char g[1]; //For the trigo section. public: void Case1(); //All of void Case2(); //these void Case3(); //are void Case4(); //the void Case5(); //mathematical void Case6(); //operations. void Trigonometry();// Trigonometrics. void EXIT(); //The exit function. void RorQ(); //The restart or quit function. int exit; //The exit value. int exitRorQ; //The restart or quit value. char rq[1]; // "r" or "q"?(char size 1) };
And the Case6 partCode:void Program::Trigonometry() { double pi = 3.1416; //Again for trigo. if (g == "M") { setcolor(7); cout << "Enter angle theta (degrees) -"; cin >> degrees; a = (degrees * pi)/180; Sine = sin (a); Cosine = cos (a); Tangent = tan (a); Arctan = atan (a); Arccos = acos (a); Arcsine = asin (a); cout << "\nThe trigonometric ratiosare as follows -:\n"; setcolor(14); cout << "Sin theta = " << Sine << endl; cout << "Cos theta = " << Cosine << endl; cout << "Tan theta = " << Tangent << endl; cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl; cout << "Inverse Cos(Arccos) theta = " << Arccos << endl; cout << "Inverse Tan(Arctan) theta = " << Arctan << endl; } else if (g == "N") { setcolor(7); cout << "Enter angle theta(radians) -"; cin >> radians; a == radians; Sine = sin (a); Cosine = cos (a); Tangent = tan (a); Arctan = atan (a); Arccos = acos (a); Arcsine = asin (a); cout << "\nThe trigonometric ratiosare as follows -:\n"; setcolor(14); cout << "Sin theta = " << Sine << endl; cout << "Cos theta = " << Cosine << endl; cout << "Tan theta = " << Tangent << endl; cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl; cout << "Inverse Cos(Arccos) theta = " << Arccos << endl; cout << "Inverse Tan(Arctan) theta = " << Arctan << endl; } }
The output after cout << "Enter angle"..... isn't coming..it just skips the if else statements and goes straight to the getch(); in main..Code:void Program::Case6() { cout << "Enter angle in (M)degrees or (N)radians? - "; cin >> g; Trigonometry(); setcolor(7); Sleep(3000); }
You could declare the multiplier as a constant:
Then use call it in you program like:Code:const DEG_TO_RAD 0.0174532925;
It saves some extra calculations.Code:radians = degrees * DEG_TO_RAD;
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Code:setcolor(7); cout << "Enter angle theta (degrees) -"; cin >> degrees; cin.ignore();These?Code:setcolor(7); cout << "Enter angle theta(radians) -"; cin >> radians; cin.ignore();
Yes, in all places where you read in numbers.
Edit: Of course, if you do two reads in the same function, you can put just one ignore after the second "cin >> number;", because the newline left in the buffer will be "eaten" by the second "cin >> number;" - but there will be a newline left in the buffer.
You can also use the "flush the input buffer" method(s) described in the FAQ section (top left of this page).
--
Mats
Last edited by matsp; 10-01-2007 at 06:18 AM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
All the other functions are working...EXCEPT Trigonometry..Trigonometry(Case6) wasn't working even before I entered the cin.ignore()s...and the others were working anyway.
where exactly must I implement that code? In the main()?
Code:int main() //Muhuhahahhahaha { Program OMGDOIT; int y; C.exit = 1; int ch; //Clearing input buffers. char buf[BUFSIZ];//Same. while(C.exit == 1) { cout << "\nWelcome to the Equation Solver!\n"; cout << "\nChoose one of the following -:\n"; cout << "\n1. Find a quadratic equation's nature of roots\n"; cout << "\n2. Find a quadratic equation's roots\n"; cout << "\n3. Establish the Vičte relation of the roots and coefficients\n"; cout << "\n4. Find a term of an arithmetic progression(AP)\n"; cout << "\n5. Find the sum of the terms of an AP\n"; cout << "\n6. Find the trigonometric values of an angle\n"; cout << "\n7. Quit,noobs >_>\n"; cin >> y; cin.ignore(); switch(y) { case 1: OMGDOIT.Case1(); getch(); OMGDOIT.RorQ(); break; case 2: OMGDOIT.Case2(); getch(); OMGDOIT.RorQ(); break; case 3: OMGDOIT.Case3(); getch(); OMGDOIT.RorQ(); break; case 4: OMGDOIT.Case4(); getch(); OMGDOIT.RorQ(); break; case 5: OMGDOIT.Case5(); getch(); OMGDOIT.RorQ(); break; case 6: cout.flush(); OMGDOIT.Case6(); getch(); OMGDOIT.RorQ(); break; case 7: C.exit = 0; break; default: cout << "Enter a number in the list\n"; break; } clrscr(); //Clears screen maybe? } return 0; }
Ehm, perhaps the problem is something else:
This compares the address of g with the address of the literal "M". You probably don't want that (as it's always false with the current piece of code).Code:g == "M"
would compare the first letter in g with the letter 'M', which is more likely to be what you actually want to compare.Code:g[0] == 'M';
Also:is duplicated. You only need it once.Code:Sine = sin (a); Cosine = cos (a); Tangent = tan (a); Arctan = atan (a); Arccos = acos (a); Arcsine = asin (a); cout << "\nThe trigonometric ratiosare as follows -:\n"; setcolor(14); cout << "Sin theta = " << Sine << endl; cout << "Cos theta = " << Cosine << endl; cout << "Tan theta = " << Tangent << endl; cout << "Inverse Sin(Arcsine) theta = " << Arcsine << endl; cout << "Inverse Cos(Arccos) theta = " << Arccos << endl; cout << "Inverse Tan(Arctan) theta = " << Arctan << endl;
However, Isn't atan etc supposed to be multiplied by the conversion back to degrees as well?
You still only need one piece of code to display things, and to do most of the caclulations.
Duplicating code is a sure way to "miss" changing something on one side of the else that needs to be changed on both sides. It is a good habit to get into only writing as little as possible inside if/else statements, and keeping any code that is common outside the if/else.
You could even do the whole calculation of angle from radians to degrees or keep it radians by having a variable "conv_factor" that is set to either 1.0 or the conversion factor, and always multiply by this factor when producing the result. Multiplying by 1.0 is of course a "no change".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Hmm I see...let me try this...