This blog is under construction

Wednesday, 24 July 2013

C program to copy elements from one array to another

Write a C program to copy elements from one array to another.


  #include <stdio.h>
  #define MAXLIMIT 256

  int main() {
        int arr1[MAXLIMIT], arr2[MAXLIMIT];
        int i, n;

        /* get the number of elements from the user */
        printf("Enter the number of element in i/p array:");
        scanf("%d", &n);

        /* boundary check */
        if (n > MAXLIMIT || n < 0) {
                printf("Boundary Level Exceeded!!\n");
                return 0;
        }

        /* input array elements from the user */
        printf("Enter your array inputs:\n");
        for (i = 0; i < n; i++) {
                printf("Array[%d]: ", i);
                scanf("%d", &arr1[i]);
        }

        /* copy contents of input array to output array */
        for (i = 0; i < n; i++) {
                arr2[i] = arr1[i];
        }

        /* print the output array */
        printf("Contents in output array:\n");
        for (i = 0; i < n; i++) {
                printf("%d ", arr2[i]);
        }

        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of element in i/p array:5
  Enter your array inputs:
  Array[0]: 10
  Array[1]: 20
  Array[2]: 30
  Array[3]: 40
  Array[4]: 50
  Contents in output array:
  10  20  30  40  50 


No comments:

Post a Comment