0

My problem states: Write a program using a while or do/while loop to generate a conversion table for converting degrees to radians. The degree values start at 0 degrees, increment by 10 and go through 360 degrees.

I'm very, very new at coding in general, and this is for my C++ class

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. Any tips would be greatly appreciated!

Niall
  • 30,036
  • 10
  • 99
  • 142
RebeccaM
  • 3
  • 1
  • 2

3 Answers3

0

You'll probably want to have an array to store the conversions. For example: 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.

Tyler Gaona
  • 479
  • 1
  • 4
  • 14
0

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. Then you have to change it to <=360 for your purpose. 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++

Community
  • 1
  • 1
Javi
  • 3,440
  • 5
  • 29
  • 43
  • 1
    Don't make his homework for him! Help him learn by just guiding him. – t.pimentel Sep 12 '14 at 19:32
  • So, this code doesn't use an array. Is it not a necessary component? – RebeccaM Sep 12 '14 at 19:35
  • It is if you want to store your result. For what you asked, the table is generated but not stored for later use in the program. Do you need it? – Javi Sep 12 '14 at 19:36
  • I shouldn't have to store it for any reason since this won't be put into a larger program. Thank you everyone for your help! – RebeccaM Sep 12 '14 at 19:37
  • Since you are learning C++, I (and probably all of us) recommend you to try to store the solution using std::vector or std::array containers. And try to write a code to use the stored values. For instance, going back to degrees again from stored radians. – Javi Sep 12 '14 at 19:39
0

The structure of a Do-While loop is "do stuff while something is true". 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. You want to start at 0, stop at 100, and increment by 5. So you would want a Do-While that looks like this:

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. 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...).

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. 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.

Also, be mindful of your semicolons.

alpha.wolf
  • 59
  • 3