Program to Copy Array to Another Array in C Language

Spread the love
program-to-copy-array-to-another-array-in-c

Program Description:

Write a Program to Copy Array to Another Array in C programming language. The program will take an input array from the user and copy all elements of the array to another array. So it is a deep copy.

Example Input and Output:

Input:

Output:

Prerequisites:

We are going to use the arrays and functions in C language in the array copy program. So it is recommended to go through the following articles, where we explained about the arrays and functions.

Copy Array to Another Array in C Program Explanation:

  1. Start the program by declaring two arrays numbers and dup with the max size of 1000.
  2. Take the desired Array size from the user and store the provided value in the size variable.
  3. Check the size. As we support max of 1000 elements in the array. Display an error message if the provided size is out of bounds.
  4. Now, Prompt the user to provide the values for array elements.
    • Use a For Loop to iterate over the array from to size-1 and accept the user input and update the &number[i] element using scanf function.
  5. Display the Original Array on the console.
  6. To copy the elements of the numbersarray to another array, We need to iterate over the elements of numbers array and copy each element to the new array. We have already created a new array dup at the start of the program.
    • So Start the loop from the first element and traverse till the size-1 and copy each element. – dup[i] = numbers[i];
  7. Once the above For loop is completed, The dup array will contain a copy of the numbers array.
  8. Display the dup array onto the console using another loop.

Copy Array to Another Array in C Algorithm:

  1. Declare numbers and dup array with size 1000 and also declare variables i and size
  2. Take the max size of the array from the user and update the size variable.
  3. Check the size, if it is out of bounds – if(size<=0 || size>1000)
  4. Update the array with user input.
    • Loop from 0 to size-1 using FOR.
    • update &numbers[i]
  5. Display the Original Array i.e numbers array.
    • Loop from 0 to size-1 using FOR.
    • Display the numbers[i]
  6. Copy the numbers array elements one by one to dup array.
    • Loop from 0 to size-1 using FOR.
    •  dup[i] = numbers[i];
  7. Display the Duplicate Array i.e dup array.
    • Loop from 0 to size-1 using FOR.
    • Display the dup[i]
  8. Stop the program.

Program to Copy Array to Another Array in C Language:

Let’s convert above algorithm into the code.

Program Output:

Compile the program.

$ gcc copy-array.c

Run the Program.

Test 1: Copy Array to Another Array.

deep-copy-array-in-c-program-output

As we can see from the above output, We are getting the expected results.

Test 2: Array Out of bounds :

As expected, The program is displaying the error message Invalid Size, Please try again.

Copy Array to Another Array in C using Functions:

In the above program we used only one function to write the program. Let’s divide the above program into the set of functions, So that each function will do a specific task.

Here is the re written version of the above program. This program uses three functions read, display and copyArray.

Here we have created three functions.

  1. The read() function.
    • Prototype of the read function – void read(int numbers[], int size)
    • read() function takes two formal arguments which are numbers array and it’s size.
    • The read() function is used to take the input numbers from the user and update the numbers array.
  2. The display function
    • Prototype of the display() function – void display(int numbers[], int size)
    • The display() function also takes two formal arguments, which are numbers array and it’s size.
    • display() function is used to display all array elements on the standard output i.e console.
  3. The copyArray() Function.
    • Here is the prototype of the copyArray() function – void copyArray(int source[], int destination[], int size)
    • The copyArray() function takes three formal arguments, which are source array and destination array and size parameter.
    • The copyArray() function will copy the elements of the source[] array to the destination[] array.

Program Output:

Let’s compile and run the program using GCC( any compiler)

deep-copy-array-to-another-array-in-c-using-functions

Test 1:

Test 2:

As we can see from the above outputs, The program is providing the desired output.

Related Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Copy array to Another Array […]

Leave a Reply