file output for circle points and radius

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Coco Agozzino
    New Member
    • Nov 2011
    • 2

    #1

    file output for circle points and radius

    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<iostre am>
    #include<cmath>
    #include<iomani p>
    #include<cstdli b>
    #include<string >
    #include<fstrea m>
    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(),io s::out);

    if(!fout.is_ope n())
    {
    cerr<<"Unable to open file "<<file_name<<e ndl;
    exit(10);
    }

    cout<<"Writing to file "<<file_name<<e ndl;

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

    fout<<
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The i'th sample means any one of your samples.

    Comment

    • Coco Agozzino
      New Member
      • Nov 2011
      • 2

      #3
      I tried the following:

      for(int i=1; i<=n; i++)
      {
      angle=2*M_PI*(i/n);
      x=r*cos(angle);
      y=r*sin(angle);
      }

      fout.precision( 2);
      fout<<setw(6)<< x<<setw(6)<<y<< endl;

      fout.close();

      return 0;
      }

      how do i get it to outpoint all the point?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You only have one radius and one name.

        You may need to use an array so you can have many radius and name variab;es.

        Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

        Comment

        Working...