daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. typedef struct values_s
  6. {
  7.     double *x;
  8.     double *funcval ;
  9.     double *derivate ;
  10.     long size ;
  11. } values_t ;
  12.  
  13. typedef enum boolean_e
  14. {
  15.     FALSE = 0,
  16.     TRUE = 1
  17. } boolean_t ;
  18. values_t * values_allocate (long size );
  19. boolean_t values_insert ( values_t *val , double x1 , double x2 , double x3 , long index );
  20. boolean_t values_free ( values_t *val);
  21. boolean_t values_print ( values_t *val);
  22. int main()
  23. {
  24.     double a,b,c;
  25.     values_t *t;
  26.     long i;
  27.  
  28.     t=values_allocate(3);
  29.     for(i=0;i<3;i++)
  30.     {
  31.         printf("\nx:");
  32.         scanf("%lf",&a);
  33.  
  34.         printf("\nfunval:");
  35.         scanf("%lf",&b);
  36.  
  37.         printf("\nderivate:");
  38.         scanf("%lf",&c);
  39.  
  40.         values_insert(t,a,b,c,i);
  41.     }
  42.  
  43.     values_print(t);
  44.  
  45.     values_free(t);
  46.     return 0;
  47. }
  48. values_t * values_allocate (long size )
  49. {
  50.     values_t *elem;
  51.     elem=calloc(1,sizeof(values_t));
  52.     if(!elem)
  53.     {
  54.         fprintf(stderr,"Error");
  55.         return 0;
  56.     }
  57.     elem->x=calloc(size,sizeof(double));
  58.     if(!(elem->x))
  59.     {
  60.         fprintf(stderr,"Error");
  61.         free(elem);
  62.         return 0;
  63.     }
  64.     elem->funcval=calloc(size,sizeof(double));
  65.     if(!(elem->funcval))
  66.     {
  67.         fprintf(stderr,"Error");
  68.         free(elem->x);
  69.         free(elem);
  70.         return 0;
  71.     }
  72.     elem->derivate=calloc(size,sizeof(double));
  73.     if(!(elem->derivate))
  74.     {
  75.         fprintf(stderr,"Error");
  76.         free(elem->x);
  77.         free(elem->funcval);
  78.         free(elem);
  79.         return 0;
  80.     }
  81.     elem->size=size;
  82.     return elem;
  83. }
  84. boolean_t values_insert ( values_t *val , double x1 , double x2 , double x3 , long index )
  85. {
  86.     if(!val||index<0||index>=val->size)
  87.         return FALSE;
  88.     val->x[index]=x1;
  89.     val->funcval[index]=x2;
  90.     val->derivate[index]=x3;
  91.  
  92.     return TRUE;
  93. }
  94. boolean_t values_free ( values_t *val)
  95. {
  96.     if(!val)
  97.         return FALSE;
  98.     free(val->derivate);
  99.     val->derivate=0;
  100.     free(val->funcval);
  101.     val->funcval=0;
  102.     free(val->x);
  103.     val->x=0;
  104.     free(val);
  105.  
  106.     return TRUE;
  107. }
  108. boolean_t values_print ( values_t *val)
  109. {
  110.     if(!val)
  111.         return FALSE;
  112.     long i;
  113.     for(i=0;i<val->size;i++)
  114.         printf("%ld %lf %lf %lf\n",i,val->x[i],val->funcval[i],val->derivate[i]);
  115.     return TRUE;
  116. }
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