dmilicev

reverse_array_of_integers_v1.c

Nov 14th, 2019
211
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     reverse_array_of_integers_v1.c      by Dragan Milicev
  4.  
  5.  
  6.     You can find all my C programs at Dragan Milicev's pastebin:
  7.  
  8.     https://pastebin.com/u/dmilicev
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13.  
  14.  
  15. // Displays an arr of n integers
  16. void display_array_of_integers( char text[], int arr[], int n ) {
  17.  
  18.     int i;
  19.  
  20.     printf("%s ( n = %d )\n\n", text, n );
  21.  
  22.     for( i=0; i<n ; i++ )   // display of n array items
  23.         printf("%4d", arr[i] );
  24.  
  25.     printf("\n");
  26. }
  27.  
  28. // swap two integers using pointers
  29. void swap_two_integers( int *pointer_a, int *pointer_b )
  30. {
  31.     int mem_int;            // memory to store integer
  32.  
  33.     mem_int = *pointer_a;
  34.     *pointer_a = *pointer_b;
  35.     *pointer_b = mem_int;
  36. }
  37.  
  38. // reverse array of integers
  39. void reverse_array_of_integers_1( int arr[], int n )
  40. {
  41.     int i, j, mem_int;      // n is number of integers in arr[]
  42.  
  43.     // reverse array
  44.     for( i=0,j=n-1; i<n/2; i++,j-- )
  45.     {
  46.         mem_int    = arr[i];
  47.         arr[i]   = arr[j];
  48.         arr[j] = mem_int;
  49.     }
  50. }
  51.  
  52. // reverse array of integers calling function swap_two_integers()
  53. void reverse_array_of_integers_2( int arr[], int n )
  54. {
  55.     int i, j;
  56.  
  57.     // reverse array
  58.     for( i=0,j=n-1; i<n/2; i++,j-- )
  59.     {
  60.         swap_two_integers( &arr[i], &arr[j] );
  61.     }
  62. }
  63.  
  64. int main()
  65. {                                                       // n is number of integers in arr[]
  66.     int i, j, n, mem_int;                               // mem_int is memory to store integer
  67.     int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };      // array of integers
  68.  
  69.     // a way to get n, number of elements in arr[]
  70.     // ( This only works in main() because there is arr[] declared )
  71.  
  72.     //n = sizeof(arr) / sizeof(arr[0]);
  73.     // or
  74.     //n = sizeof(arr) / sizeof(*arr);
  75.     // or
  76.     n = sizeof(arr) / sizeof(int);
  77.  
  78.     display_array_of_integers( "\n Before:  ", arr, n );
  79.  
  80.     // reverse array
  81.     for( i=0, j=n-1; i<n/2; i++,j-- )
  82.     {
  83.         mem_int = arr[i];
  84.         arr[i] = arr[j];
  85.         arr[j] = mem_int;
  86.     }
  87.  
  88.     // Pointers ...
  89.     // arr == &arr[0]
  90.     // *arr == arr[0]
  91.     // pointer_to_arr[i] == &arr[i]
  92.  
  93.     display_array_of_integers( "\n After:  ", arr, n );
  94.  
  95.  
  96.     reverse_array_of_integers_1( arr, n );
  97.  
  98.     display_array_of_integers( "\n After calling function reverse_array_of_integers_1()  ", arr, n );
  99.  
  100.  
  101.     reverse_array_of_integers_2( arr, n );
  102.  
  103.     display_array_of_integers( "\n After calling function reverse_array_of_integers_2()  ", arr, n );
  104.  
  105.  
  106.     return 0;
  107. }
RAW Paste Data Copied