bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

assignment-s-13-1.c (1419B)


      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 void inputarr(int[], int);
     15 void display(int[], int);
     16 void reverse(int *);
     17 
     18 int main()
     19 {
     20     int size, *arr;
     21     printf("How many element do you want to add: ");
     22     scanf("%d", &size);
     23     arr = (int *)malloc((size + 1) * sizeof(int));
     24     inputarr(arr, size);
     25     printf("\n=== Before Reverse ===\n");
     26     display(arr, size);
     27     reverse(arr);
     28     printf("\n\n=== After Reverse ===\n");
     29     display(arr, size);
     30     free(arr);
     31     return 0;
     32 }
     33 
     34 void inputarr(int arr[], int n)
     35 {
     36     int i;
     37     arr[0] = n;
     38     for (i = 1; i <= n; i++)
     39     {
     40         printf("Enter element %d: ", i);
     41         scanf("%d", &arr[i]);
     42     }
     43 }
     44 
     45 void display(int arr[], int n)
     46 {
     47     int i;
     48     for (i = 1; i <= n; i++)
     49     {
     50         printf("\nIndex: %-2d | Value: %-5d | Address: %p", i, arr[i], (void *)&arr[i]);
     51     }
     52 }
     53 
     54 void reverse(int *arr)
     55 {
     56     int *start = arr + 1;
     57     int size = *arr;
     58     int *end = arr + size;
     59     while (start < end)
     60     {
     61         *start = *start ^ *end;
     62         *end = *start ^ *end;
     63         *start = *start ^ *end;
     64         start++;
     65         end--;
     66     }
     67 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror