How do I tell a C program to create n arrays, reading n by scanf? -


i'd create small program adds, subtracts, multiplies , cross product vectors.

therefore i'd have user enter - amount of vectors - dimension of said vectors - wants vectors (one of above mentioned functions)

since quite new programming maybe haven't found correct source yet, in case grateful hint in right direction (especially search exactly, since not native speaker)

my problem:

i don't know how program sum function sums n vectors (n being amount of vectors user entered)

i have rough idea rest of functions maybe won't bother program again sum-problem problem me.

i'm sure answer somewhere near don't seem able find it.

so many in advance :)

simple vector

#include <stdio.h> #include <stdlib.h> #include <string.h>  typedef int type;  typedef struct vector {     size_t size;     size_t capacity;     type *array; } vector;  vector *vec_make(){     vector *v;     v = (vector*)malloc(sizeof(vector));     v->size = 0;     v->capacity=16;     v->array=(type*)realloc(null, sizeof(type)*(v->capacity += 16));     return v; }  void vec_pushback(vector *v, type value){     v->array[v->size] = value;     if(++v->size == v->capacity)         v->array=(type*)realloc(v->array, sizeof(type)*(v->capacity += 16)); }  size_t vec_size(vector *v){     return v->size; }  type *vec_getarray(vector *v){     return v->array; }  void vec_free(vector *v){     free(v->array);     free(v); }  int main(){     int n=5;//user input     vector *a, *b, *c;     int i, sum=0, size;     int *ap, *bp, *cp;     = vec_make(); b = vec_make(); c = vec_make();     (i=0; i<n; ++i) {         vec_pushback(a, i+1);//1,2,3,4,5     }     vec_pushback(b, 2);     vec_pushback(b, 4);     vec_pushback(b, 5);     vec_pushback(b, 6);     vec_pushback(b, 10);     ap=vec_getarray(a);bp=vec_getarray(b);cp=vec_getarray(c);     for(i=0;i<a->size;++i){         sum+=ap[i];     }     printf("sum(va)=%d\n", sum);     size=vec_size(b);     for(i=0;i<size;++i)         vec_pushback(c, ap[i]+bp[i]);     printf("va + vb = vc(");     for(i=0;i<size;++i){         printf("%d", cp[i]);         if(i<size-1)             printf(",");         else             printf(")\n");     }     vec_free(a);vec_free(b);vec_free(c);     return 0; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -