Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef enum error_e
- {
- SUCCESS=0,
- NULL_POINTER_ERROR,
- ALLOC_FAILURE_ERROR,
- OUT_OF_RANGE_ERROR,
- DIMENSION_MISMATCH_ERROR
- }error_t;
- typedef struct vector_s
- {
- double *values;
- long dim;
- }vector_t;
- error_t vector_allocate(vector_t *vec, long dim){
- if(dim<0) return DIMENSION_MISMATCH_ERROR;
- double *newvector;
- if((newvector = calloc(dim, sizeof(double)))==0)
- return ALLOC_FAILURE_ERROR;
- vec->values = newvector;
- vec->dim = dim;
- return SUCCESS;
- }
- error_t vector_clear(vector_t *vec){
- if(!(vec -> values)) return NULL_POINTER_ERROR;
- else{
- free(vec->values);
- vec ->values = 0;
- vec-> dim = 0;
- }
- return SUCCESS;
- }
- error_t vector_fill(vector_t *vec, double value, long index ){
- if(!(vec->values)) return NULL_POINTER_ERROR;
- if(index > vec->dim || index < 0) return OUT_OF_RANGE_ERROR;
- vec->values[index] = value;
- return SUCCESS;
- }
- error_t vector_print(vector_t *vec){
- long n;
- printf("[");
- for(n=0; n<(vec->dim); n++ ){
- if(n==(vec->dim)-1) printf("%.1f" , vec->values[n]);
- else printf("%.1f, " , vec->values[n]);
- }
- printf("]");
- return SUCCESS ;
- }
- int main() {
- vector_t vec1={NULL, 0};
- //vector_t vec2={NULL, 0};
- //vector_t vec3={NULL, 0};
- long i;
- long dimenzija;
- double vrijednost;
- int povratak;
- int pov2=0;
- //za prvi vektor
- printf("Koliko brojeva zelis unijeti? ");
- scanf("%ld", &dimenzija);
- povratak = vector_allocate(&vec1, dimenzija); //provjeravam jel alokacija ok
- printf("%d", povratak);
- for(i=0; i<dimenzija; i++){
- printf("Koji broj zelis unijeti na %ld mjesto ? ", i+1);
- scanf("%lf", &vrijednost);
- pov2 =pov2 + vector_fill(&vec1, vrijednost, i);
- }
- printf("%d", pov2); //provjeravam jel popunjavanje vektora ok
- vector_print(&vec1);
- vector_clear(&vec1);
- printf("SUPER "); //provjeravam jel stigne do kraja
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment