Desiring to store the 6 vertices of a hexagon inscribed in t

Desiring to store the 6 vertices of a hexagon inscribed in the unit circle programmer wrote the following code fragment: double hex[6][2]; const double d theta = 2*M_PI/6; int i = 0; for (double theta = 0.0; theta

Solution

Correcting the code

1. I believe Variable \'i\' has to be iterated from 0 to less than 6
2. and make theta increment by dtheta, Initially theta being 0

double hex[6][2];

const double dtheta = 2*M_PI/6; // Same as above

double theta = 0.0; //Declaring theta as 0.0

for(int i=0;i<6;++i) //Making i run from 0 to less than 6, so that no segmentation fault occurs

{

hex[i][0] = cos(theta);

hex[i][1] = sin(theta);

theta = theta + dtheta;

}

Thanks, I hope it clarifies

 Desiring to store the 6 vertices of a hexagon inscribed in the unit circle programmer wrote the following code fragment: double hex[6][2]; const double d theta

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site