In this article, we are going to learn how to copy an array to another in C++ index by index. Or you can say how to create a copy of a C++ array.
How to copy an array to another in C++
#include <iostream>
#define MAX_ARRAY 250
using namespace std;
int main()
{
int Array1[MAX_ARRAY], Array2[MAX_ARRAY];
int Size;
cout<<"Please enter size of the array (Max 250): ";
cin>>Size;;
cout<<"Please enter elements in array: ";
for(int i=0; i<Size; i++)
{
cin>>Array1[i];
}
cout<<"All Elements of First array are:"<<endl;
for(int i=0; i<Size; i++)
{
cout<<Array1[i]<<"\t";
}
for(int i=0; i<Size; i++)
{
Array2[i] = Array1[i];
}
cout<<"\nAfter Copy , All Elements of Second array are: \n ";
for(int i=0; i<Size; i++)
{
cout<<Array2[i]<<"\t";
}
return 0;
}
Output
Please enter size of the array (Max 250): 9
Please enter elements in array: 12
3
6
9
12
15
18
21
24
All Elements of First array are:
12 3 6 9 12 15 18 21 24
After Copy , All Elements of Second array are:
12 3 6 9 12 15 18 21 24