Jump to content

I really need help on my C++ programming assignment.


Tampitump

Recommended Posts

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 by Tampitump
Link to comment
Share on other sites

!

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Sensei
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Sensei
Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;


}

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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 by Tampitump
Link to comment
Share on other sites

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++?".

Link to comment
Share on other sites

  • 4 weeks later...

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 by Tampitump
Link to comment
Share on other sites

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 is
https://www.google.com/?#q=graph+x^3

Imagine 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_method
see 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 by Sensei
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Tampitump
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.