I need help writing a C++ program to generate a random angle (and it\'s suppleme
ID: 670000 • Letter: I
Question
I need help writing a C++ program to generate a random angle (and it's supplementary angle) and calculate the corresponding chord lengths. Use sin function in <cmath>; use rand function in <cstdlib>; set output resolution using cout.precision function. Generate a random angle in whole degree between 1 and 180 (and corresponding supplementary angle). Use the rand function and seed it using time (0) -- srand(time(0));. Compute the chord length subtended by this angle inside a circle of radius 2.5. Chord length given by function c=2 r sin(theta/2). Constant float variable storing radius as 2.5 (and shouldn't appear elsewhere). Use constant pi as defined in <cmath> as M_PI (do not define yourself). Set the output precision for chord length at 4 using cout.precision(4);
Output --> Angle [deg]: 122 Angle[deg]: 71
Chord length: 4.373 Chord length: 2.904
Thank you very much! I have been struggling with this for hours.
Steps involved-
Define and initialize variables. Seed the random number generator. Determine the random angle. Change angle value from degrees to radians. Calculate the chord length. Output the random angle value. Set the precision. Output the calculated chord length.
Explanation / Answer
#include <iostream>
#include<cstdlib>
#include<cmath>
#include <iomanip>
using namespace std;
int main()
{
const float radius=2.5;
srand(time(0));
int angle = rand() % (181 - 1) + 1;
int cSup=180-angle;
double angle1=angle*(M_PI/180);
double cSup1=cSup*(M_PI/180);
double len1=2*radius*(sin(angle1/2));
double len2=2*radius*(sin(cSup1/2));
cout<<"Angle[deg]: " <<angle<<endl;
cout<<"Chord length: "<<setprecision(4)<<len1<<endl;
cout<<"Angle[deg]: " <<cSup<<endl;
cout<<"Chord length: "<<setprecision(4)<<len2<<endl;
return 0;
}