Guest User

Untitled

a guest
Jan 24th, 2018
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. /*
  2.     [invert_array.c]
  3. Author : Alessandro Suglia ( alessandro.suglia@gmail.com )
  4. Date : 10/17/2011 15:37
  5.  
  6. Simple program which shows how to manage a structured
  7. data type like the array. It asks to the user the size of
  8. the array, asks to fill it with n values, and then prints
  9. them to the video, firstly in the inserting order and,
  10. afterthat, inverted.
  11.  
  12. */
  13.  
  14. /* <------------- Header declaration -------------------- > */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. /* <-------------- Useful Macros and define --------------> */
  19. #define LEN(x) sizeof(x)/sizeof(int)
  20. #define PRINT_ARRAY(x, n) for ( i = 0; i < n; i++ ) printf("%d)\t%d\n", i+1, x[i])
  21.  
  22. /* < ------------- Functions' prototype declaration ------> */
  23. void get_array( int [], int );
  24. void invert_array( int [], int );
  25. void swap( int *, int * );
  26.  
  27. /* <--------------- main() function ----------------------> */
  28. int main()
  29. {
  30.   int *num;
  31.   int n = 0;
  32.   int i  = 0;
  33.  
  34.   printf("::::::: INVERT ARRAY :::::::::::\n\n");
  35.   printf("What's the array's size?\n");
  36.   scanf("%d", &n);
  37.  
  38.   if ( n < 0 ) /* Check the size of the array inserted by the user, if it's negative, exit*/
  39.     {
  40.       perror("Nothing to be done with an array with negative size!!\n");
  41.       exit(-1);
  42.     }
  43.   else /* Else, allocates memory for the defined array num */
  44.     {
  45.       num = (int*)malloc( n * sizeof(int));
  46.       if ( !(num) ) /* Check for some errors in the dynamic allocation */
  47.        {
  48.            perror("MALLOC ERROR : FATAL!!\n");
  49.            exit(-1);
  50.        }
  51.       get_array( num, n );
  52.       printf("Value inserted in the array : \n");
  53.       PRINT_ARRAY( num, n );
  54.       printf("\n\n");
  55.       printf("Inverted array values : \n");
  56.       invert_array( num, n );
  57.       PRINT_ARRAY( num, n );
  58.      
  59.     }
  60.     /* Some sort of stuff that I MUST do in order to make the code portable also with linux */
  61.   #ifdef _WIN32
  62.     system("PAUSE");
  63.   #endif  
  64.   return EXIT_SUCCESS;
  65.  
  66.  
  67. }
  68. /* It receives the array and its length and then grants to the user to fill it */
  69. void get_array( int a[], int len )
  70. {
  71.   int i;
  72.  
  73.   printf("Insert %d values : \n", len);
  74.   for ( i = 0; i < len; i++ )
  75.     scanf("%d", &a[i]);
  76.  
  77. }
  78. /* A kinda of function used to swap the value */
  79. void swap( int *a, int *b )
  80. {
  81.   int temp = *a;
  82.   *a = *b;
  83.   *b = temp;
  84.  
  85. }
  86. /* Inverts the order of the array's elements */
  87. void invert_array( int a[], int len )
  88. {
  89.   int m = len/2;
  90.   int i;
  91.  
  92.   for ( i = 0; i < m; i++ )
  93.     swap(&a[i], &a[len-1-i]);
  94. }
Add Comment
Please, Sign In to add comment