C program to reverse the contents of an array without using temp variable
#include <stdio.h>
void printArray(int *arr, int length)
{
int i;
for (i = 0; i < length; i++)
printf("%d\t", arr[i]);
printf("\n");
}
void reverseArray(int *arr, int length)
{
int i;
for (i= 0; i < length/2 ; i++) {
arr[i] =arr[i] + arr[length - i -1];
arr[length - i - 1] = arr[i] - arr[length - i - 1];
arr[i] = arr[i]- arr[length - i - 1];
}
}
int main()
{
int arr1[5] = {1, 2, 3, 4, 5};
int i;
printf("Array Contents Before Swapping\n");
printArray(arr1, 5);
reverseArray(arr1, 5);
printf("Array Contents After Swapping\n");
printArray(arr1, 5);
return 0;
}
Comments
Post a Comment