assignment-s-13-2.c (1537B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 19 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a function that reverses the elements of an array in place, using only a single 9 pointer argument, and return void. */ 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 14 struct Array 15 { 16 int size; 17 int *arr; 18 }; 19 20 void inputarr(int[], int); 21 void display(int[], int); 22 void reverse(struct Array *); 23 24 int main() 25 { 26 struct Array array; 27 printf("How many element do you want to add: "); 28 scanf("%d", &array.size); 29 array.arr = (int *)malloc((array.size) * sizeof(int)); 30 inputarr(array.arr, array.size); 31 printf("\n=== Before Reverse ===\n"); 32 display(array.arr, array.size); 33 reverse(&array); 34 printf("\n\n=== After Reverse ===\n"); 35 display(array.arr, array.size); 36 free(array.arr); 37 return 0; 38 } 39 40 void inputarr(int arr[], int n) 41 { 42 int i; 43 for (i = 0; i < n; i++) 44 { 45 printf("Enter element %d: ", i + 1); 46 scanf("%d", &arr[i]); 47 } 48 } 49 50 void display(int arr[], int n) 51 { 52 int i; 53 for (i = 0; i < n; i++) 54 { 55 printf("\nIndex: %-2d | Value: %-5d | Address: %p", i, arr[i], (void *)&arr[i]); 56 } 57 } 58 59 void reverse(struct Array *array) 60 { 61 int *start = array->arr; 62 int *end = array->arr + (array->size - 1); 63 while (start < end) 64 { 65 *start = *start ^ *end; 66 *end = *start ^ *end; 67 *start = *start ^ *end; 68 start++; 69 end--; 70 } 71 }