Tampitump Posted September 21, 2016 Posted September 21, 2016 (edited) I have a programming assignment in my C++ class that is due tomorrow and I'm pretty much lost. This assignment is actually a little bit ahead of where we are in our studies currently, and is a pretty difficult undertaking (at least for me): Here are the requirements: Develop an algorithm that will take an angle, measured in degrees, between -90° and +90° ( -90 < x < 90 ) and calculate the sin, cos and tan of the angle, and implement the algorithm in C++. Your program should validate the input for the angle, making sure it falls within the given range and notifying the user if it does not. Your angle and sin, cos, tan and all numbers should be doubles. The output should be formatted this way: Following the input, there should be a line of 40 asterisks then a table of outputs, then another line of 40 asterisks, with blank lines between the asterisks and outputs: **************************************** ^ ^Angle sin cos tan ±xx.xx^^^±x.xxxx^^^±x.xxxx^^^±x.xxxx ^ **************************************** The headings "Angle, sin," etc. are right justified over their columns. The angle is typed out with two decimal places. The sine, cosine & tangent have 4 decimal places and are right justified under their column headings. There are 3 spaces between the fields for each number. (If the outputs are too big or too small to fit in this format, don't worry about that.) For example, if the angle was 65.5°, the output would be: **************************************** Angle sin cos tan 65.50 0.9100 0.4147 2.1943 **************************************** Note that C++ expects angles to be given in radians. Your algorithm will need to convert that inputted angle from degrees to radians for the calculations. For full credit you need to use a "constant variable" for the value of pi &/or the value of the conversion factor between degrees and radians. I really need help on this. So if anyone here has good knowledge of C++ and good math skills, please help. THANKS!!!!! Edited September 21, 2016 by Tampitump
ydoaPs Posted September 21, 2016 Posted September 21, 2016 ! Moderator Note We don't give out answers, but we do give help. What do you have so far? Where are you stuck? Is there anything specific that you feel like you don't understand? Perhaps you could start by making a mockup with pseudocode.
Tampitump Posted September 21, 2016 Author Posted September 21, 2016 I've got nothing so far. I'm completely lost and floundering. I'll just have to figure it out. Thanks.
Sensei Posted September 21, 2016 Posted September 21, 2016 I've got nothing so far. I'm completely lost and floundering. I'll just have to figure it out. Thanks. How come? 1) how are you reading argument supplied by user to command-line application? 2) how to convert string to float/double? Which function you have to use to do that? 3) how to check if float is in proper range? Which function you have to use to do that? 4) how to print float/double with fixed precision after decimal dot.. ? Which function you have to use to do that?
Tampitump Posted September 21, 2016 Author Posted September 21, 2016 (edited) Right now I'm trying to make sense out of the requirements. C++ only recognizes angles in radians, but converting an angle in degrees is supposed to be part of our algorithm. This part confuses me. Edited September 21, 2016 by Tampitump
Sensei Posted September 21, 2016 Posted September 21, 2016 (edited) C++ only recognizes angles in radians, but we converting an angle in degrees is supposed to be part of our algorithm. This part confuses me. Angle in degrees is f.e. -180.0 .... 180.0 Angle in radians is f.e. -M_PI .... M_PI To convert from degrees to radians, multiply by M_PI and divide by 180.0. Or reverse, in 2nd way. 3.14159265 / 180.0 is constant. M_PI is defined in math.h Use #define _USE_MATH_DEFINES prior include line. Edited September 21, 2016 by Sensei
Tampitump Posted September 21, 2016 Author Posted September 21, 2016 I'm a novice in every conceivable sense of the word. I'm still trying to understand the differences in "int, float, double", and what they do, etc. I'll have to go bacl and read the e-text that goes along with my coarse, but I'm afraid I'll run out of time. My reasons for getting started so late is the unreal amount of calculus work and other tough classes I'm taking. This semester is hitting me like 12 tons of bricks.
Sensei Posted September 21, 2016 Posted September 21, 2016 (edited) I'm a novice in every conceivable sense of the word. I'm still trying to understand the differences in "int, float, double", and what they do, etc. That's mathematics. What is difference between Integer https://en.wikipedia.org/wiki/Integer and Real number? https://en.wikipedia.org/wiki/Real_number In computers they have built-in limits of precision. Float has smaller precision than double. I'll have to go bacl and read the e-text that goes along with my coarse, but I'm afraid I'll run out of time. My reasons for getting started so late is the unreal amount of calculus work and other tough classes I'm taking. This semester is hitting me like 12 tons of bricks. If material is too hard for you, you should use your time in the holidays/vacations to acustomize with the books from new semester. There were 2 months. Edited September 21, 2016 by Sensei
Tampitump Posted September 21, 2016 Author Posted September 21, 2016 Angie in degrees is f.e. -180.0 .... 180.0 Angle in radians is f.e. -M_PI .... M_PI To convert from degrees to radians, multiply by M_PI and divide by 180.0. Or reverse, in 2nd way. 3.14159265 / 180.0 is constant. No, I understand how to convert between the two, but it seems like the assignment is asking us to get the program to do it.
Sensei Posted September 21, 2016 Posted September 21, 2016 No, I understand how to convert between the two, but it seems like the assignment is asking us to get the program to do it. User enters value "angle in degrees", but trigonometric functions are expecting argument "angle in radians". You have to convert them.
Tampitump Posted September 21, 2016 Author Posted September 21, 2016 (edited) Nevermind. I'll figure it out. Thanks. Edited September 21, 2016 by Tampitump
Tampitump Posted September 23, 2016 Author Posted September 23, 2016 Actually, I do have a further question. How can I get it to validate the input of an angle, making sure it falls within -90 degrees and 90 degrees and notifying the user when the angle does not fall within these margins? Thanks.
Sensei Posted September 23, 2016 Posted September 23, 2016 Actually, I do have a further question. How can I get it to validate the input of an angle, making sure it falls within -90 degrees and 90 degrees and notifying the user when the angle does not fall within these margins? Thanks. Well, that's the most essential part of programming http://www.cprogramming.com/tutorial/c/lesson2.html
Tampitump Posted September 23, 2016 Author Posted September 23, 2016 Here's what I've got so far. Maybe you can tell me where I'm way off point. Thanks for your help sensei. #define USE MATH DEFINES #include <iostream>; using namespace std; int main() { double angle; const double PI=3.14159; cout << "Finding sine, cosine, and tangent of angles. \n" // Have user enter an angle between -90 degrees and 90 degrees (measured in degrees, not radians) cout << "enter agnle:"; cin >> angle; return 0; }
Sensei Posted September 23, 2016 Posted September 23, 2016 Did you read entire article from link that I gave in previous post? Where do you have if() statement? ps. What for using #define USE MATH DEFINES if you're not linking proper header file which is using it, and you're defining PI by yourself ?
Tampitump Posted September 23, 2016 Author Posted September 23, 2016 (edited) ps. What for using #define USE MATH DEFINES if you're not linking proper header file which is using it, and you're defining PI by yourself ? I'm sorry, I have no idea what you're talking about. Where do you have if() statement? I don't have one in there yet. All this program is supposed to do is take an angle between -90 and 90 (in degrees), convert it to radians, and provide sin, cos, and tan for that angle. It also has to have a feature that notifies the user when angle is invalid, so it has to validate the input. I really just want to get done with this thing and go to bed. Its been a long week, I'm tired, and its due in 2 hours. Edited September 23, 2016 by Tampitump
ydoaPs Posted September 23, 2016 Posted September 23, 2016 Actually, I do have a further question. How can I get it to validate the input of an angle, making sure it falls within -90 degrees and 90 degrees and notifying the user when the angle does not fall within these margins? Thanks. The most important part of programming is being able to break your problem down into parts. Get general steps. Once you've got big steps, go to each step and break it down. Repeat until you know exactly what code you need to write. So, for this one, you have: Step 1: Make sure the angle is between -90 and 90. Step 2: Tell the user if the angle is invalid. Now, you don't need to convert the angle yet, since you're comparing against degrees and the input is degrees. So, if you have the input stored as a variable and the converted angle stored as a variable, use the input. Having an angle in degrees, what operation do you need to perform to check to see if the angle is within 90 degrees of 0? Here's what I've got so far. Maybe you can tell me where I'm way off point. Thanks for your help sensei. #define USE MATH DEFINES #include <iostream>; using namespace std; int main() { double angle; const double PI=3.14159; cout << "Finding sine, cosine, and tangent of angles. \n" // Have user enter an angle between -90 degrees and 90 degrees (measured in degrees, not radians) cout << "enter agnle:"; cin >> angle; return 0; } Those parts I talked about before? You want to do them outside of the main function. Make each big part its own function and give it a good name. Then your main function should be mostly calling your other functions. All this program is supposed to do is take an angle between -90 and 90 (in degrees), convert it to radians, and provide sin, cos, and tan for that angle. It also has to have a feature that notifies the user when angle is invalid, so it has to validate the input. I really just want to get done with this thing and go to bed. Its been a long week, I'm tired, and its due in 2 hours. So, break this list into steps, and tackle them one at a time. Step 1: Take a value from the user. Step 2: Make sure it is within 90 degrees of 0, and notify the user if it isn't (or if it isn't in degrees or a number at all). Step 3: Create a new variable holding the angle in radians. Step 4: Output the angle, sin, cos, and tan for the input angle. Break each step down more if possible, and tell us what you have. Don't worry about C++ syntax yet. Just think about the logical steps. What big picture things do we need to do? ps. What for using #define USE MATH DEFINES if you're not linking proper header file which is using it, and you're defining PI by yourself ? Because you instructed to do so and they didn't understand why. We need to help the OP with the actual algorithm first and then we can talk about code. The problem here seems to be rooted in "How do I program?" instead of "How do I code in C++?". 1
Tampitump Posted October 20, 2016 Author Posted October 20, 2016 (edited) Hey guys, I've attached an image of my current C++ programming assignment to this post. I've written some of the code and I'd like someone to look at it and tell me whether it is okay, and what needs to be done to it to make it match the requirements. Thanks, and here's my code: #include <iostream> #include <math.h> #include <cmath> #include <string> #include <iomanip> using namespace std; int main() { int x; bool finished = false; bool correct_no = false; // bool values aka tru or false to determine data validation cout << "Enter a Value For A\n" << endl; cin >> x; //makes user of the program enter a angle in degrees from -90 to 90 and stores that value in int while (!correct_no) // this is a while statement used to help validate the data { if (x<=0) // ensure the upper bound of the data is correct { correct_no = true; // execute if true } else // else statement to help in data validation { cout << "Try again pick a Value where B>A\n" << endl; cin >> x; //stores input in memory to verify it if not correct re exacutes the statement till the correct informantion is entered in the corrct range } } while (!finished) { if (x<=0)// data has to be in the specified range as set in this line { finished = true;// if true executes this line of code } else//else statement { cout << "Try again pick a Value where B>A\n" << endl; // cout to tell user to try again cin >> x; // else statement to ask the useser again to enter the correct information if what was entered was no in the correct range } } //validates the input to ensure it is between the specicified range cout << endl << "All done! Nice!! Your Data Is Valid\n" << endl; //cout to let user know the correct infomation was entered and the program can now execute the rest of program finished = false; correct_no = false; // bool values aka tru or false to determine data validation cout << "Enter a Value For B\n" << endl; cin >> x; //makes user of the program enter a angle in degrees from -90 to 90 and stores that value in int while (!correct_no) // this is a while statement used to help validate the data { if (x<=0) // ensure the upper bound of the data is correct { correct_no = true; // execute if true } else // else statement to help in data validation { cout << "Try again pick a Value where B>A\n" << endl; cin >> x; //stores input in memory to verify it if not correct re exacutes the statement till the correct informantion is entered in the corrct range } } while (!finished) { if (x<=0)// data has to be in the specified range as set in this line { correct_no = true;// if true executes this line of code } else//else statement { cout << "Try again pick a Value where B>A\n" << endl; // cout to tell user to try again cin >> x; // else statement to ask the useser again to enter the correct information if what was entered was no in the correct range } //validates the input to ensure it is between the specicified range cout << endl << "All done! Nice!! Your Data Is Valid\n" << endl; //cout to let user know the correct infomation was entered and the program can now execute the rest of program { long int constant, power; char var; cout << "Enter a constant of variable to be integrated (if there is no constant enter 1): " << endl; cin >> constant; cout << "Now enter the variable to be integrated: " << endl; cin >> var; cout << "Finally, enter the power which everything is raised to: " << endl; cin >> power; cout << "\nYou have entered: \n" << constant << "*" << var << "^" << power << "\n" << endl; power= power + 1; //cout << endl << endl << constant << endl; constant=power/constant; //cout << endl << endl << constant << endl; cout << "The integral is: " << constant << "" << var << "^" << power << endl << endl; system("pause"); return 0;} } } Sorry, I guess the link didn't attach. Here's the assignment: Develop a program that will approximate the integral f(x) = x^3 evaluated from a to b. Use the "rectangular approximation" for the integral. Use a step size of dx = 0.01. (you do not need to check if the value of b is an even number of steps up from a). Your program should: Request the values of a and b from the user. Use some type of decision structure (if, switch, etc.) to validate that b > a. If b < a, return error message. Use some kind of loop structure (while, for, etc.) to calculate the apporoximation. Separate calculate the exact value of the integral. Calculate the difference between the exact value and the approximated value. The output of the program should consist of: The approximation. The exact value. The difference between the exact value and approximation. The program should point out explicitly if the approximation is less than or equal to the exact value. (this will require the use of a second decision structure). The output should clearly indicate the results. Please help! This is due tonight, Edited October 20, 2016 by Tampitump
Sensei Posted October 20, 2016 Posted October 20, 2016 (edited) It's not right,you don't have nor A variable, nor B variable,you don't have loop from A to B in 0.01 steps.You refuse to accept negative inputs (your assignment has no mention about it).Graph of f(x)=x^3 in Google ishttps://www.google.com/?#q=graph+x^3Imagine user is asked to enter x axis. Any float/double.It's becoming A variable.Then user is asked to enter x axis again.It's becoming B variable.But you have to warn user if B<A, that such value is illegal.And repeat asking for B.Once B>A,you have to perform loop.Summing area of object enclosed by A...B, and f(A) and f(B) at these points. Do it in steps like asked in assignment.They want you to use "Rectangle method" https://en.wikipedia.org/wiki/Rectangle_methodsee anim-gif, as it's progressing with more divisions.even though the more precise would be "Trapezoidal rule"https://en.wikipedia.org/wiki/Trapezoidal_rule also see anim-gif.. Edited October 20, 2016 by Sensei
ydoaPs Posted October 21, 2016 Posted October 21, 2016 I know you're having trouble with calculus too, so I'll help you a bit more than I probably should. It wants you do do an approximation via a Riemann sum. It's like taking an integral without taking the limit. You make your rectangles as the value of the function being one side and the step value being the other. For the exact answer, you need to take the limit of that process. Luckily for you, there's a rule you can use as a shortcut for a polynomial. If you think of the rule for polynomial differentiation, you can get the rule for integration by working it backwards. Once you have the indefinite integral, you just need to evaluate it from a to b. What do you need to store? Should you hardcode the step value or ask for it? Are there any places in the function where the value is negative? Does that mean you need to do anything differently? If so, do you need to only do the different thing where the function is negative? Once you know that kind of stuff, you can break it down into big picture pieces. What in big chunks do you need to do to solve this problem?
Tampitump Posted October 21, 2016 Author Posted October 21, 2016 (edited) I'm having trouble with my Visual Studio 2012 compiler. Every time I try to create a new project for this, it gives me this message: System.Collections.Generic.Randomized.StringEqualityComparer is no a GenericTypeDefinition. MakeGenericType may only be called on a type for which Type.IsGenericTypeDefinition is true. Edited October 21, 2016 by Tampitump
Tampitump Posted October 21, 2016 Author Posted October 21, 2016 Can someone please advise me on why I'm seeing these messages? I've got 35 minutes to turn this project in so I need to resolve them.
ydoaPs Posted October 21, 2016 Posted October 21, 2016 Have you tried opening it in 2015 instead of 2012?
Tampitump Posted October 21, 2016 Author Posted October 21, 2016 Have you tried opening it in 2015 instead of 2012? Instructor doesn't use 2015. Wants us to keep it in 2012. Too late now anyway.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now