Hello all, first time posting here so excuse me if my etiquette is a bit off.
I'm in a C++ coding class at my university and due to me having to manage my other classes, a job, and my priorities to a society I had to miss a few classes.
So I'm a bit behind, I have an assignment I'm working on right now, it's open book and I have no idea how to tackle this one question? Perhaps I'm just a bit dumb, but either way I have no idea how to tackle this question (Will be written out below)
I'm not asking for the easy way out, I just need someone to show me the way as it was, so I know how to do what. I have a few more questions that aren't related to the assignment, but this is the most pressing right now.
The question is simply.
"Write a C++ program that calls built-in functions sin and cos. The program prints a table of sin and cos for every degree from 10 to 30. The argument being passed to sin and cos must be double, and it must be in radians.
Radians are related to degrees by the following equation:
degrees=(Radians*180)/PI
Thanks very much for whatever help yall can offer!
really not much to this, so I will give you one freebie.
void dosin()
{
const double deg2rad = 3.141592654/180.0; // units. deg* rad/deg -> rad
for(int s = 10; s<31; s++)
cout << sin(s*deg2rad) << endl; //letting it promote s to a double via the conversion factor.
}
which is mostly asking if you can make a for loop and write to the screen and whether you can do enough math to fix the inverted conversion factor (they gave rad to deg); the assignment is to iterate degrees, so its kinda backwards).
Thank you very much for this!
Albeit I'm still a little confused?
See I suppose I should have specified a bit more in my original post, I have a bit of trouble figuring out how functions work in general.
Like I kind of get the basics of them in regards to how the function call, prototype, and definition work. Just not how to write them out and incorporate them into the main part of the program.
If you think you don't know enough about functions, then read more! But if you have already read, but still are having doubts, then you have to be more specific by asking questions.
Like: Why is prototype required. That's just an example. Because you see, "I don't know how functions work in general" isn't telling us what exactly you aren't getting so basically we would have to explain to you everything about functions, which you are better off reading from a proper source.
What are you confused with.. the for-loop? You have to be more specific. What are your other questions not relating to this? Hey, maybe we can help with those too.
Little brief note:
How to write a function? Think about what is the return type. In this case it's nothing so we specify void. Then think about the parameters, in jonnin's case there are no parameters, the function would do the same thing whenever called, so he specific no parameters. If you wanted the function to print radians from 1 to an inputted number then you would take an integer argument.
How to call the function? Simple specify name with parenthesis and arguments if they exist.
Mention the function before the void main or after void main, but if you're specifying after void main then declare a prototype at the beginning.
you need to read your textbook or an online reference.
functions work somewhat like variables: the compiler needs to see it (top of program file(s) down) before you can use it. Prototypes expose the function to the compiler and tell it "you are going to see this soon, this is what it is, the rest will come later". You can avoid prototypes in simple programs by putting the functions above main, and in an order that any dependency is resolved by seeing the dependent item first, but that is a pain for anything but the smallest of programs. Get used to prototypes, and use them.
functions are simple enough format:
return_type function_name(parametertype parametername, ... repeat for all params)
and using them is also simple
result = function_name(p1, ...); //parameters are in order defined and comma divided.
not all functions return a result, those have a type of void.
some functions use reference parameters to return a result, that has & on the parameter:
void foo(int &x) //if x changes in the body, the variable sent to the function (which likely is not named x) will be changed and the resulting change sticks.
foo(zz); //zz will be changed by the above function, as the result of its operations. again note that what you call a function with and the parameter names are not the same -- the function works on the names it was written with, and the variables passed into it are renamed to the local name. That way you can do foo(zz) and foo(xyz) and so on with different inputs but the function just works on x as an alias for those inputs.
Thats really 90% of it. There are a few details I skipped like recursion (a function that calls itself, which takes time to wrap your mind around) but this + your book is enough to get started.
Like I kind of get the basics of them in regards to how the function call, prototype, and definition work. Just not how to write them out and incorporate them into the main part of the program.
You'd create a function whenever you need to do something. We might come up with some as we're deciding how to approach a solution, and more as necessary when we're translating our ideas into code.
For instance, we might decide that it makes sense to produce the table row-by-row, and might have an idea to say something like this:
1 2 3 4 5 6 7 8 9 10 11
int main()
{
// print the header of the table
print_header_row();
// for each integer degree in [10, 30]
for (int degree = 10; degree <= 30; ++degree)
{
// print the row of the table corresponding to the given degree
print_table_row(degree);
}
}
Where we've broken down the problem into a few simpler discrete steps, each of which naturally translates to a function.
When we approach the task further, we might organically decide that we need to convert degrees to radians, in which case we could write a function for that too.