C++ Lab: 2A

Thu Aug 14 2025
Updated: Fri Aug 15 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction

In this lab will practice prompting the user for values, reading values as doubles from the user/keyboard into variables, using an arithmetic expression to convert the value from degrees to radians, and calculating the sine and cosine using <cmath> functions sin(), cos(), and tan(). Be sure to read this lab thoroughly, especially the Hand-in Procedure.

Lab

Write a complete program that:

  1. Creates a global constant to hold pi (π = 3.141592).

  2. Prompts the user to enter an angle in degrees then reads in an angle in degrees from the keyboard as a double.

  3. Converts the angle from (2) to radians.

  4. Computes the sine of the angle from (3) using <cmath>.

  5. Computes the cosine of the angle from (3) using <cmath>.

  6. Computes the tangent of the angle from (3) using <cmath>.

  7. Writes the answers from (3) (4) (5) and (6) out to the display.

The formula to convert degrees to radians is:

radians=degreesπ180radians=\frac{degrees*π}{180}

The prototypes for sin() and cos() are:

  1. double sin(double x);

  2. double cos(double x);

  3. double tan(double x);

Example Output

An example of an interaction with your program is shown below, your output should match these examples exactly.

Alexs-iMac:lab2a alex$ g++ -Wall -pedantic -Werror main.cpp Alexs-iMac:lab2a alex$ ./a.out Enter an angle value, in degrees **10 10 degrees = 0.174533 radians sin(10) = 0.173648 cos(10) = 0.984808 tan(10) = 0.176327

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the default ones C++’s cin and getline() automatically apply after reading an input from the user. CodeGrade supplies input in a different manner (Linux redirection) to programs so in CodeGrade's auto tests there will not be line breaks after the inputs.

Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.
  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.