1

I'd like to create a small program that adds, subtracts, multiplies and does a cross product for vectors.

Therefore I'd like to have the user enter - the amount of vectors - the dimension of said vectors - what exactly he wants to do with the vectors (one of the above mentioned functions)

Since I am quite new to programming maybe I haven't found the correct source yet, in this case I would be grateful for a hint in the right direction (especially what to search exactly, since I am not a native speaker)

My problem:

I don't know how to program the sum function so that it always sums up n vectors (n being the amount of vectors the user entered)

I have a rough idea about the rest of the functions so maybe I won't bother you with this program again but this sum-problem really is a problem for me.

I'm sure the answer is somewhere near but I just don't seem to be able to find it.

So many thanks in advance :)

  • 1
    Just make `n` a parameter to your sum function, I guess. We're really here to help with specific programming questions. If you have tried something and have some code you'd like us to look at, that would be best. – Carl Norum Apr 19 '13 at 20:36
  • Thanks for the hint, I'll make sure to follow this lead next time. Sorry to have bothered you. – David_Maendlen Apr 22 '13 at 21:28

3 Answers3

0

Are you using straight C or C++?

Since you want dynamic memory allocation, I'd really recommend C++ since you can use std::vector for your array which is totally straightforward. The C way is not nearly as simple.

http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4027/C-Tutorial-A-Beginners-Guide-to-stdvector-Part-1.htm

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
0

This question explains how to malloc a 2-d array Using malloc for allocation of multi-dimensional arrays with different row lengths basically you start by allocating an array of n int pointers then you loop over that array and do array[i] = (int*)malloc(sizeof(int) * lenght))

Putting that into more a step by step how to;

1) allocate an array of int pointers - int** array = malloc(sizeof(int*) * n); you use ** because your first pointer points the the beginning of the array and the array is of type int*, each of these int pointers will point to another array.

2) loop over the array - for (i = 0; i < n; i++) in C there is no simple way to malloc 2-d arrays. you have to build each row 1 by 1.

3) inside the loop allocate the horizontal array - array[i] = (int*)malloc(sizeof(int) * length)

The question I link to includes some checks to make sure the allocation was successful. I don't know how necessary this is, but it can't hurt.

Community
  • 1
  • 1
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • Thanks for the help, much appreciated :) seems like I wanted too much too fast, I'll have to do some more learning before I can really delve into that. I'll reach out again as soon as I've done my homework on this one here :) – David_Maendlen Apr 22 '13 at 21:30
0

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;
    a = vec_make(); b = vec_make(); c = vec_make();
    for (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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70