Q:

Reverse array in C

0

Reverse array in C

 

C program to reverse an array , if 'A' is an array of integers with three elements such that,
A[0] = 1, A[1] = 2, A[2] = 3
Then after reversing, the array will be
A[0] = 3, A[1] = 2, A[0] = 1

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
int main()
{
  int n, c, d, a[100], b[100];

  printf("Enter the number of elements in array\n");
  scanf("%d", &n);

  printf("Enter array elements\n");

  for (c = 0; c < n ; c++)
    scanf("%d", &a[c]);

  // Copying elements into array b starting from the end of array a

  for (c = n - 1, d = 0; c >= 0; c--, d++)
    b[d] = a[c];

  // Copying reversed array into the original, we are modifying the original array.

  for (c = 0; c < n; c++)
    a[c] = b[c];

  printf("The array after reversal:\n");

  for (c = 0; c < n; c++)
    printf("%d\n", a[c]);

  return 0;
}

outputs:

Enter the number of elements in array

2

Enter array elements

1

2

The array after reversal:

2

1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now