This blog is under construction

Wednesday 24 July 2013

C program to clear array contents

Write a C program to clear array contents.


  #include <stdio.h>
  #define MAXLIMIT 256

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

        /* get the number of elements from the user */
        printf("Number of elements:");
        scanf("%d", &n);

        /* Boundary Check */
        if (n < 0 || n > MAXLIMIT) {
                printf("Boundary level exceeded!!\n");
                return 0;
        }

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

        /* clear the array contents */
        for (i = 0; i < n; i++) {
                arr[i] = 0;
        }

        /* print the result */
        printf("After clearing the array contents:\n{");
        for (i = 0; i < n; i++) {
                printf("%d, ", arr[i]);
        }
        printf("\b\b}\n");

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Number of elements:5
  Enter your input array elements:
  Array[0]: 10
  Array[1]: 20
  Array[2]: 30
  Array[3]: 40
  Array[4]: 50

  After clearing the array contents:
  {0, 0, 0, 0, 0} 


No comments:

Post a Comment