EldiraSesto

ue1/A

Oct 19th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef enum error_e
  5. {
  6.  SUCCESS=0,
  7.  NULL_POINTER_ERROR,
  8.  ALLOC_FAILURE_ERROR,
  9.  OUT_OF_RANGE_ERROR,
  10.  DIMENSION_MISMATCH_ERROR
  11.    
  12. }error_t;
  13.  
  14. typedef struct vector_s
  15. {
  16.     double *values;
  17.     long dim;
  18. }vector_t;
  19.  
  20. error_t vector_allocate(vector_t *vec, long dim){
  21.    
  22.     if(dim<0) return DIMENSION_MISMATCH_ERROR;
  23.     double *newvector;
  24.    
  25.     if((newvector = calloc(dim, sizeof(double)))==0)
  26.     return ALLOC_FAILURE_ERROR;
  27.    
  28.        
  29.     vec->values = newvector;
  30.     vec->dim = dim;
  31.    
  32.     return SUCCESS;
  33.    
  34. }
  35.  
  36. error_t vector_clear(vector_t *vec){
  37.     if(!(vec -> values)) return NULL_POINTER_ERROR;
  38.     else{
  39.     free(vec->values);
  40.     vec ->values = 0;
  41.     vec-> dim = 0;
  42.     }
  43.    
  44.     return SUCCESS;
  45.    
  46. }
  47.  
  48. error_t vector_fill(vector_t *vec, double value, long index ){
  49.     if(!(vec->values)) return NULL_POINTER_ERROR;
  50.     if(index > vec->dim || index < 0) return OUT_OF_RANGE_ERROR;
  51.    
  52.     vec->values[index] = value;
  53.     return SUCCESS;
  54.    
  55. }
  56.  
  57.  
  58. error_t vector_print(vector_t *vec){
  59.     long n;
  60.         printf("[");
  61.     for(n=0; n<(vec->dim); n++ ){
  62.         if(n==(vec->dim)-1)     printf("%.1f" , vec->values[n]);
  63.         else printf("%.1f, " , vec->values[n]);
  64.        
  65.     }
  66.     printf("]");
  67.     return SUCCESS ;
  68.    
  69.     }
  70.  
  71.  
  72. int main() {
  73.  
  74.   vector_t vec1={NULL, 0};
  75.   //vector_t vec2={NULL, 0};
  76.   //vector_t vec3={NULL, 0};
  77.  
  78.   long i;
  79.   long dimenzija;
  80.   double vrijednost;
  81.   int povratak;
  82.   int pov2=0;
  83.   //za prvi vektor
  84.   printf("Koliko brojeva zelis unijeti? ");
  85.   scanf("%ld", &dimenzija);
  86.   povratak = vector_allocate(&vec1, dimenzija); //provjeravam jel alokacija ok
  87.   printf("%d", povratak);
  88.    for(i=0; i<dimenzija; i++){
  89.    printf("Koji broj zelis unijeti na %ld mjesto ? ", i+1);
  90.     scanf("%lf", &vrijednost);
  91.     pov2 =pov2 + vector_fill(&vec1, vrijednost, i);
  92.     }
  93.      printf("%d", pov2); //provjeravam jel popunjavanje vektora ok
  94.    
  95.     vector_print(&vec1);
  96.     vector_clear(&vec1);
  97.     printf("SUPER "); //provjeravam jel stigne do kraja
  98.    
  99.    
  100.    
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment