array copx c++
#include <iostream>
int main() {
int arr1[] = {1, 2, 3, 4, 5};
int arr2[5];
for (int i = 0; i < 5; ++i) {
arr2[i] = arr1[i];
}
// Displaying elements of arr2
std::cout << "Elements of arr2: ";
for (int i = 0; i < 5; ++i) {
std::cout << arr2[i] << " ";
}
std::cout << std::endl;
return 0;
}
This C++ code snippet includes an array arr1
with initial values and another array arr2
of the same size. It uses a for
loop to copy the elements of arr1
to arr2
element by element. After copying, it displays the elements of arr2
using a second for
loop.