Python program to copy all elements of one array into another array

Created with Sketch.

Python program to copy all elements of one array into another array

In this program, we need to copy all the elements of one array into another. This can be accomplished by looping through the first array and store the elements of the first array into the second array at the corresponding position.

ARRAY 1

  1. 1   2  3  4  5

ARRAY 2

  1. 1   2  3  4  5

ALGORITHM:

  • STEP 1: Declare and initialize an array.
  • STEP 2: Declare another array of the same size as of the first one
  • STEP 3: Loop through the first array from 0 to length of the array and copy an element from the first array to the second array that is arr1[i] = arr2[i].

PROGRAM:

  1. #Initialize array   
  2. arr1 = [12345];
  3. #Create another array arr2 with size of arr1  
  4. arr2 = [None] * len(arr1);
  5. #Copying all elements of one array into another  
  6. for i in range(0, len(arr1)):
  7.     arr2[i] = arr1[i];
  8. #Displaying elements of array arr1   
  9. print(“Elements of original array: “);
  10. for i in range(0, len(arr1)):
  11.    print(arr1[i]),
  12. print();
  13. #Displaying elements of array arr2   
  14. print(“Elements of new array: “);
  15. for i in range(0, len(arr2)):
  16.    print(arr2[i]),

Output:

Elements of original array
1 2 3 4 5
Elements of new array:
1 2 3 4 5

 

Leave a Reply

Your email address will not be published. Required fields are marked *