TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to copy all the elements of one array into another array

Program to copy all the elements of one array into another array on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to copy all the elements of one array into another array.

Explanation

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.

Program to copy all the elements of one array into another array

Algorithm

  1. Declare and initialize an array.
  2. Declare another array of the same size as of first one
  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].

Solution

Python

#Initialize array 
arr1 = [1, 2, 3, 4, 5]; 
 
#Create another array arr2 with size of arr1
arr2 = [None] * len(arr1);
 
#Copying all elements of one array into another
for i in range(0, len(arr1)):
    arr2[i] = arr1[i]; 
 
#Displaying elements of array arr1 
print("Elements of original array: ");
for i in range(0, len(arr1)):
   print(arr1[i]),
 
print();
 
#Displaying elements of array arr2 
print("Elements of new array: ");
for i in range(0, len(arr2)):
   print(arr2[i]),

Output:

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

C

#include <stdio.h>
 
int main()
{
    //Initialize array 
    int arr1[] = {1, 2, 3, 4, 5};
    
    //Calculate length of array arr1
    int length = sizeof(arr1)/sizeof(arr1[0]);
    
    //Create another array arr2 with the size of arr1.

    int arr2[length];
    
    //Copying all elements of one array into another
    for (int i = 0; i < length; i++) { 
        arr2[i] = arr1[i]; 
    }  
    
    //Displaying elements of array arr1 
    printf("Elements of original array: \n");
    for (int i = 0; i < length; i++) { 
        printf("%d ", arr1[i]);
    }
    
    printf("\n");
    
    //Displaying elements of array arr2 
    printf("Elements of new array: \n");
    for (int i = 0; i < length; i++) { 
        printf("%d ", arr2[i]);
    }
    return 0;
}

Output:

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

JAVA

public class CopyArray {
    public static void main(String[] args) {    
        
        //Initialize array 
        int [] arr1 = new int [] {1, 2, 3, 4, 5}; 
        
        //Create another array arr2 with size of arr1
        int arr2[] = new int[arr1.length];
        
        //Copying all elements of one array into another
        for (int i = 0; i < arr1.length; i++) { 
            arr2[i] = arr1[i]; 
        }  
        
        //Displaying elements of array arr1 
        System.out.println("Elements of original array: ");
        for (int i = 0; i < arr1.length; i++) { 
           System.out.print(arr1[i] + " ");
        } 
        
        System.out.println();
        
        //Displaying elements of array arr2 
        System.out.println("Elements of new array: ");
        for (int i = 0; i < arr2.length; i++) { 
           System.out.print(arr2[i] + " ");
        } 
    }
}

Output:

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

C#

using System;
                    
public class CopyArray
{
    public static void Main()
    {
        //Initialize array 
        int [] arr1 = new int [] {1, 2, 3, 4, 5}; 
        
        //Create another array arr2 with size of arr1
        int [] arr2 = new int[arr1.Length];
        
        //Copying all elements of one array into another
        for (int i = 0; i < arr1.Length; i++) { 
            arr2[i] = arr1[i]; 
        }  
        
        //Displaying elements of array arr1 
        Console.WriteLine("Elements of original array: ");
        for (int i = 0; i < arr1.Length; i++) { 
           Console.Write(arr1[i] + " ");
        } 
        
        Console.WriteLine();
        
        //Displaying elements of array arr2 
        Console.WriteLine("Elements of new array: ");
        for (int i = 0; i < arr2.Length; i++) { 
           Console.Write(arr2[i] + " ");
        } 
    }
}

Output:

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

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array 
$arr1 = array(1, 2, 3, 4, 5); 
 
//Create another array arr2
$arr2 = array();    
    
//Copying all elements of one array into another
for ($i = 0; $i < count($arr1); $i++) { 
    $arr2[$i] = $arr1[$i]; 
}  
 
//Displaying elements of array arr1 
print("Elements of original array: <br>");
for ($i = 0; $i < count($arr1); $i++) { 
   print($arr1[$i] . " ");
} 
 
print("<br>");
 
//Displaying elements of array arr2 
print("Elements of new array: <br>");
for ($i = 0; $i < count($arr2); $i++) { 
   print($arr2[$i] . " ");
} 
?>
</body>
</html>

Output:

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

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf