[英]Intro to C++: Generating a conversion table for converting degrees to radians with a while or do/while loop
My problem states: Write a program using a while or do/while loop to generate a conversion table for converting degrees to radians. 我的问题是:使用while或do / while循环编写程序以生成用于将度转换为弧度的转换表。 The degree values start at 0 degrees, increment by 10 and go through 360 degrees. 度数值从0度开始,以10度递增,并达到360度。
I'm very, very new at coding in general, and this is for my C++ class 我在编码方面非常非常新,这是针对我的C ++类的
I've come up with this so far: 到目前为止,我已经提出了:
#include <iostream>
using namespace std;
int main()
{
double degrees, radians, degree1
do
{
radians = 3.14159265/180
degree1 = (0 * radians)
degrees = (degree1 + 10) * radians;
}
while (degrees > 360);
return 0;
}
However, I don't have a great grasp on do-while loops, and I'm having troubles getting the code to run. 但是,我对do-while循环没有很好的了解,并且在运行代码方面遇到麻烦。 Any tips would be greatly appreciated! 任何提示将非常感谢!
You'll probably want to have an array to store the conversions. 您可能需要一个数组来存储转换。 For example: degrees_to_radians[180] = 3.14
例如: degrees_to_radians[180] = 3.14
In the do while loop, have the array index be the current value in degrees, and set it equal to that value of degrees converted to radians. 在do while循环中,使数组索引为当前值(以度为单位),并将其设置为等于转换为弧度的度的值。
The do-while guarantees that at least 1 loop is going to be done, and it is going to loop until the while condition is satified. do-while保证至少要执行1个循环,并且它将循环直到满足while条件为止。 Then you have to change it to <=360
for your purpose. 然后,您必须根据自己的需要将其更改为<=360
。 The following code does what you want: 以下代码可以满足您的要求:
#include <iostream>
using namespace std;
double deg2rad(double deg)
{
return deg*3.14159265/180.f;
}
int main()
{
double degrees = 0;
do
{
cout << degrees << "\t" << deg2rad(degrees) << std::endl;
degrees += 10;
}
while (degrees <= 360);
return 0;
}
Suggestion: try to use boost constants. 建议:尝试使用升压常量。 Either C/C++ constants or Boost constants: How to use the PI constant in C++ C / C ++常数或Boost常数: 如何在C ++中使用PI常数
The structure of a Do-While loop is "do stuff while something is true". Do-While循环的结构是“在某些事情成立时做某事”。 Start with something simple. 从简单的事情开始。 Since this is a class assignment, I don't want to just give you the answer, but I'll give a similar example. 由于这是一个课堂作业,因此我不想仅给您答案,但是我将举一个类似的示例。
Say you want to count to 100 by fives. 假设您想将数字乘以100。 You want to start at 0, stop at 100, and increment by 5. So you would want a Do-While that looks like this: 您想从0开始,从100停止,再增加5。因此,您需要一个Do-While,如下所示:
int count = 0;
do {
count = count + 5; // increment by 5
cout << count << endl; // print out the value of count
} while (count <= 100);
Remember, the loop continues while the statement is true. 请记住,当语句为true时,循环将继续。 So in this case, you will keep doing the "stuff" while count
is less-than or equal-to 100. The sample code you provided only runs through the loop once because the condition degrees > 360
is false after it does the calculations inside the loop (after which degrees is 31.415...). 因此,在这种情况下,当count
小于或等于100时,您将继续执行“填充”。您提供的示例代码仅在循环中运行一次,因为条件degrees > 360
在进行内部计算后为false循环(之后的度数为31.415 ...)。
I'm guessing you just learned For-Loops and are now supposed to do something you'd normally use a For-Loop to do using a While/Do-While instead. 我猜您刚刚学习了循环,现在应该做一些您通常使用循环来代替While / Do-While来完成的事情。 If you were to do the same example as a For-Loop, it'd look like this: 如果要执行与循环相同的示例,则看起来像这样:
for (int count = 0; count <= 100; count += 5) {
cout << count << endl; // print out the value of count
}
Now, instead of printing out the value, do your radians/degrees conversions and store them in an array as @Tyler said. 现在,不要打印出该值,而是进行弧度/度转换并将其存储在数组中,如@Tyler所说。
Also, be mindful of your semicolons. 另外,请注意分号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.