C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Q. Program to copy all the elements of one array into another array.ExplanationIn 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. Algorithm
SolutionPython#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 JAVApublic 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#
|