A copy() function for arrays


#include <iostream.h>
double* copy(double [], int);
void print(double [], int);
void main()
{
    double a[8] = {22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9};
      print(a, 8);
      double* b = copy(a, 8);
      a[2] = a[4] = 11.1;
      print(a, 8);
      print(b, 8);
}
double* copy(double a[], int n)
{     double* p = new double[n];
      for (int i = 0; i < n; i++)
       p[i] = a[i];
      return p;
}
void print(double a[], int n)
{     for (int i = 0; i < n; i++)
            cout << a[i] << " ";
      cout << endl;
}

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|