0

I am a complete beginner in programming and I was given the following assignment:

Write a C++ program that computes a pair of estimates of π, using a sequence of inscribed and circumscribed regular polygons. Halt after no more than 30 steps, or when the difference between the perimeters of the circumscribed and inscribed polygons is less than a tolerance of ε=10⁻¹⁵. Your output should have three columns, for the number of sides, the perimeter of an inscribed polygon, and perimeter of the circumscribed polygon. For the last two columns, display 14 digits after the decimal point.

well, I decided to use the law of cos to find the lengths of the sides of the polygon but when I was testing out my program I realized the line: a = cos(360 / ngon); keeps giving me a zero as the output which makes everything else also zero and I am not sure what is wrong please help. P.S. Sorry if the program looks really sloppy, I am really bad at this.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <cmath>


using namespace std;
int main()
{
 char zzz;
 int ngon = 3, a, ak;
 double insngon = 0.0;
 double cirngon = 0.0;
 cout << "Number of Sides" << "\t\t\t" << "Perimeter of insribed region" << "\t\t\t" << "Perimeneter of circumscribed polygon" << "\t\t" << "\n";
 while (ngon <= 30)
 {
  a = cos(360 / ngon);
  ak = pow(.5, 2) + pow(.5, 2) - 2 * .5*.5*a;
  insngon = (ak*ngon);
  cirngon = (ak / (sqrt(1 - pow(ak, 2))));
  cout << fixed << setprecision(14) << ngon << "                               " << insngon << "                                " << cirngon << endl;
  ngon++;
  if (cirngon - insngon <= pow(10.0, -15));
  cin >> zzz;
  return 0;

 }
 cout << "\nEnter any character and space to end ";
 cin >> zzz;
 return 0;
}
Jack
  • 1
  • 1
  • `a` iand `ngon` are integers. Given that, what is expected when you do this: `a = cos(360 / ngon);`? – PaulMcKenzie Mar 25 '18 at 02:36
  • That is an / the answer @PaulMcKenzie – Captain Giraffe Mar 25 '18 at 02:37
  • 6
    The `cos` and other trig functions use radians as the arguments, not degrees. 2 * pi radians = 360 degrees. Also what @PaulMcKenzie said. :) – Ben Zotto Mar 25 '18 at 02:37
  • @Jack `pow(.5, 2)` This is a constant -- there is no need to call the `pow` function to compute something that doesn't change. Same thing here: `pow(10.0, -15)`. Just define the constant and use it. – PaulMcKenzie Mar 25 '18 at 02:41

2 Answers2

2

One issue is that you declared integers, yet you are using them in the call to cos here:

int ngon = 3, a, ak;
//...
a = cos(360 / ngon);

Since a is an integer, the return value of cos (which is of type double) will be truncated. Also, since ngon is an integer, the 360 / ngon will also truncate.

The fix is to make a a double, and divide 360.0 by ngon to prevent the truncation:

int ngon = 3, ak;
double a;
//...
a = cos(360.0 / ngon);

The other issue, as pointed out in the comments is that the trigonometric functions in C++ use radians as the argument, not degrees. You need to change the argument to the equivalent value in radians.

Another issue is that you're using pow to compute values that are constant. There is no need to introduce an unnecessary function call to compute constant values. Just define the constants and use them.

For example:

const double HALF_SQUARED = 0.25
const double EPSILON_VALUE = 10.0e-15;

and then use HALF_SQUARED and EPSILON_VALUE instead of the calls to pow.

Also, pow is itself a floating point function, thus can produce results that are not exact as is discussed by this question . Thus pow(ak, 2) should be replaced with simply ak * ak.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0

Use float a; (or double a) instead of int a.

Here the return type of a is int And calculating

a = cos(360/ngon)

Is equivalent to a= cos(120) that is the result of cos(120) is 0.8141 and being a integer type "a" will only store the integer part it. Therefore 'a' will be 0 and discarding floating value. Also use double ak; instead of int ak;. Because here pow function has been used which have return type 'double'

0xC0d3
  • 77
  • 1
  • 10