fork download
  1. #include <iostream>
  2.  
  3. int main() {
  4. int *arr1, *arr2;
  5.  
  6. // Dynamically allocate an array of 10 integers
  7. arr1 = new int[10];
  8.  
  9. // Initialize arr1 with some values
  10. for (int i = 0; i < 10; ++i) {
  11. arr1[i] = i + 1; // Assign values 1 through 10
  12. }
  13.  
  14. // Dynamically allocate an array of 20 integers
  15. arr2 = new int[20];
  16.  
  17. // Copy elements from arr1 to the first 10 elements of arr2
  18. for (int i = 0; i < 10; ++i) {
  19. arr2[i] = arr1[i];
  20. }
  21.  
  22. // Initialize the remainder of arr2 to 0
  23. for (int i = 10; i < 20; ++i) {
  24. arr2[i] = 0;
  25. }
  26.  
  27. // Display arr2 for verification
  28. for (int i = 0; i < 20; ++i) {
  29. std::cout << arr2[i] << " ";
  30. }
  31. std::cout << std::endl;
  32.  
  33. // Free dynamically allocated memory
  34. delete[] arr1;
  35. delete[] arr2;
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5288KB
stdin
1
2
10
42
11
stdout
1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0