program prompts and reads in the number of sample points, the circle radius and the output file name.

1. Prompt and read in the number of sample points. Read and store this number as an integer.

2. Prompt and read in the circle radius. Read and store this number as a double precision floating point number.

3. Prompt and read in the output file name. Read and store the file name as a C++ string. Include the C++ command “#include <string>” to use C++ strings.

5. Open the file for output. Make sure to pass a C style string, not the C++ string, to the open routine. Include the C++ command “#include <fstream>” to use C++ files streams. Be sure to use the type “ofstream”, not “ifstream”, for your output file stream.

6. Check if the file was successfully opened for output. If not, print an error message and exit.

7. Print a message to the terminal informing the user that the program is writing to the given file.

8. The formula for the x and y coordinates of the i’th sample is:
x = R ∗ cos(angle)
y = R ∗ sin(angle)
where angle is 2 ∗ π ∗ i/N and R is the circle radius and N is the number of points.

9. Print each coordinate with two digits after the decimal point. Print the coordinates so that the decimal points in each column line up. You may assume that the circle radius is between 0.1 and 999.99.

Close the output file stream.

i get stuck on what to do with number 8, what is the i




#include<iostream>
#include<cmath>
#include<iomanip>
#include<cstdlib>
#include<string>
#include<fstream>
using namespace std;

int main()
{
ofstream fout;
string file_name;
int r(0);
int n(0);

int x(0);
int y(0);

cout<<"Enter number of sample points: ";
cin>>n;
cout<<"Enter circle radius: ";
cin>>r;
cout<<"Enter file name: ";
cin>>file_name;

fout.open(file_name.c_str(),ios::out);

if(!fout.is_open())
{
cerr<<"Unable to open file "<<file_name<<endl;
exit(10);
}

cout<<"Writing to file "<<file_name<<endl;

x=r*cos(2*M_PI*i/n)
y=r*sin(2*M_PI*i/N)

fout<<
Last edited on
You probably need to loop for "n" sample points and perform your calculations in that loop. The variable "i" would be the loop counter.
Topic archived. No new replies allowed.