Here is the C program to copy all elements from one array to another array.
Required Knowledge
Algorithm to copy elements of one array to another array
Let inputArray is an integer array having N elements and copyArray is the array where we want to copy all N elements of inputArray. Size of copyArray is >= size of inputArray.
Let inputArray is an integer array having N elements and copyArray is the array where we want to copy all N elements of inputArray. Size of copyArray is >= size of inputArray.
- Using for loop, we will traverse inputArray from array from index 0 to N-1.
- We will copy the ith(0 <= i <= N-1) element of inputArray to ith index of copyArray.
copyArray[i] = inputArray[i];
C program to copy all elements of an array to another array
#include <stdio.h> int main(){ int inputArray[100], copyArray[100], elementCount, counter; printf("Enter Number of Elements in Array\n"); scanf("%d", &elementCount); printf("Enter %d numbers \n", elementCount); for(counter = 0; counter < elementCount; counter++){ scanf("%d", &inputArray[counter]); } for(counter = 0; counter < elementCount; counter++){ copyArray[counter] = inputArray[counter]; } printf("Duplicate Array\n"); for(counter = 0; counter < elementCount; counter++){ printf("%d ", copyArray[counter]); } return 0; }Output
Enter Number of Elements in Array 5 Enter 5 numbers 5 3 8 1 -3 Duplicate Array 5 3 8 1 -3
Related Topics