SHARE
TWEET

Untitled

Bisus Oct 30th, 2019 129 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int read_array(const char *name, double *a, unsigned int n);
  4. void print_array(double *a, unsigned int n);
  5. int f(double *a, unsigned int n);
  6.  
  7. /* Ф-ия возвращает 1, если масиив симметричный. 0, если нет.*/
  8. int f(double *a, unsigned int n)
  9. {
  10.     unsigned int i;
  11.  
  12.     for( i = 0; i<n/2; i++ )
  13.     {
  14.         if( a[i]!=a[n - 1 - i] )
  15.             return 0;
  16.     }
  17.  
  18.     return 1;
  19. }
  20.  
  21. void print_array(double *a, unsigned int n)
  22. {
  23.     unsigned int i;
  24.  
  25.     for( i = 0; i<n; i++ )
  26.         printf("a[%u]==%lf\t", i, a[i]);
  27.  
  28.     printf("\n");
  29. }
  30.  
  31. int read_array(const char *name, double *a, unsigned int n)
  32. {
  33.     FILE *input;
  34.     unsigned int i;
  35.  
  36.     if( !(input = fopen(name, "r")) ) return -1;
  37.  
  38.     for( i = 0; i<n; i++ )
  39.     {
  40.         if( fscanf(input, "%lf", a + i)!=1  )
  41.         {
  42.             fclose(input);
  43.             return -2;
  44.         }
  45.     }
  46.  
  47.     fclose(input);
  48.     return 0;
  49. }
  50.  
  51. int main(int argc, const char *argv[])
  52. {
  53.     unsigned int n;
  54.     int res;
  55.     double *a;
  56.  
  57.     if( (argc!=3) || (sscanf(argv[1], "%u", &n)!=1) )
  58.     {
  59.         fprintf(stderr, "Usage: %s [length] [file]\n", argv[0]);
  60.         return -1;
  61.     }
  62.  
  63.     a = (double *)malloc(n*sizeof(double));
  64.     if( !a )
  65.     {
  66.         fprintf(stderr, "Not enough memory!\n");
  67.         return -2;
  68.     }
  69.  
  70.     res = read_array(argv[2], a, n);
  71.     if( res<0 )
  72.     {
  73.         switch(res)
  74.         {
  75.         case -1:
  76.             fprintf(stderr, "Can not open %s\n", argv[2]);
  77.             return 0;
  78.         case -2:
  79.             fprintf(stderr, "Can not read %s\n", argv[2]);
  80.             return 0;
  81.         default:
  82.             fprintf(stderr, "Unknown error %d in %s\n", res, argv[2]);
  83.             return 0;
  84.         }
  85.     }
  86.  
  87.     printf("f() returned %d\n", f(a, n));
  88.  
  89.     print_array(a, n);
  90.  
  91.     return 0;
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top