Friday, December 9, 2022

Dynamic Array

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
  double a,b;
  int n;
  n=0;
  ifstream f ("data.txt", ios::in);
  
  // Opening and reading the file for the first time 
  // to count the number of (x, y) data pairs. 
  if (f.is_open())
  {
    while (!f.eof())
    {
      f>>a>>b;
      if (f.eof())break;
      n++;
      cout << a <<" "<<b<<" "<<n<<"\n";
    } 
    cout <<"n="<<n<<"\n";
    f.close();
  }
   else cout << "Unable to open file"; 
   f.close();
    
  // Declaration of dynamic arrays x and y.
  double * x,* y;
  x=new double [n];
  y=new double [n];
  // Opening and reading the file for the second time 
  // to input of (x, y) data pairs to dynamic arays x and y.     


  int i=0;
  f.open ("data.txt", ios::in);
    if (f.is_open())
  {
    while (!f.eof())
    {
      f>>a>>b;
      if(f.eof())break;
      x[i]=a;
      y[i]=b;
      cout << x[i] <<" "<<y[i]<<" "<<i<<"\n";
      i++;
    } 
  f.close();
  }
   else cout << "Unable to open file secomd time\n"; 
      return 0;
}

No comments:

Post a Comment

Create a C++ program for Visual Studio

Create a C++ program for Visual Studio that inputs a file with coordinates on the XY plane of N points, and then displays the location of th...