What's the advantage of using pointers vs using the structs direcxtly in this program?
Ie should it be
vector* vector_make_from(int vsize, double* d);
instead?
_____________
#include <math.h>
#include <stdlib.h>
#include "vector.h"
#include <stdio.h>
vector vector_make(int size)
{
vector vr;// = malloc(sizeof(vector));
vr.size = size;
vr.v = malloc(size * sizeof(double));
return vr;
}
vector vector_make_from(int vsize, double* d)
{
vector vr;// = malloc(sizeof(vector));
vr.size = vsize;
int i;
for(i=0; i<vsize; i++) {
vr.v = d;
}
return vr;
}
void vector_destroy(vector vr)
{
free(vr.v);
//free(vr);
}
/* double vector_length(double* xs, int size) */
/* { */
/* sqrt(sum); */
/* } */
double vector_get(vector vr, int index)
{
return vr.v[index];
}
void vector_set(vector vr, int index, double d)
{
vr.v[index] = d;
}
double vector_dot_product(double* xs, double* ys, int size)
{
int i;
int sum = 0;
for(i=0; i<size; i++) {
sum += xs * ys;
}
return sum;
}
double vector_euclidean_distance(vector v1, vector v2)
{
int i;
double sum = 0;
for(i=0; i<v1.size; i++) {
sum += pow(v1.v - v2.v, 2);
}
return sqrt(sum);
}
#include <stdio.h>
#include "vector.h"
#include <stdlib.h>
int main(void)
{
vector a = vector_make(3);
vector b = vector_make(3);
double z[3] = {5,1,3};
vector c = vector_make_from(3,z);
printf("\nWhere is jsjsjerror?");
vector_set(c,2,4);
printf("\nz[2] is:%f\n", z[2]);
double x[3] = {1,2,3};
double y[3] = {2,3,5};
a.v = x;
b.v = y;
return 0;
}
Ie should it be
vector* vector_make_from(int vsize, double* d);
instead?
_____________
#include <math.h>
#include <stdlib.h>
#include "vector.h"
#include <stdio.h>
vector vector_make(int size)
{
vector vr;// = malloc(sizeof(vector));
vr.size = size;
vr.v = malloc(size * sizeof(double));
return vr;
}
vector vector_make_from(int vsize, double* d)
{
vector vr;// = malloc(sizeof(vector));
vr.size = vsize;
int i;
for(i=0; i<vsize; i++) {
vr.v = d;
}
return vr;
}
void vector_destroy(vector vr)
{
free(vr.v);
//free(vr);
}
/* double vector_length(double* xs, int size) */
/* { */
/* sqrt(sum); */
/* } */
double vector_get(vector vr, int index)
{
return vr.v[index];
}
void vector_set(vector vr, int index, double d)
{
vr.v[index] = d;
}
double vector_dot_product(double* xs, double* ys, int size)
{
int i;
int sum = 0;
for(i=0; i<size; i++) {
sum += xs * ys;
}
return sum;
}
double vector_euclidean_distance(vector v1, vector v2)
{
int i;
double sum = 0;
for(i=0; i<v1.size; i++) {
sum += pow(v1.v - v2.v, 2);
}
return sqrt(sum);
}
#include <stdio.h>
#include "vector.h"
#include <stdlib.h>
int main(void)
{
vector a = vector_make(3);
vector b = vector_make(3);
double z[3] = {5,1,3};
vector c = vector_make_from(3,z);
printf("\nWhere is jsjsjerror?");
vector_set(c,2,4);
printf("\nz[2] is:%f\n", z[2]);
double x[3] = {1,2,3};
double y[3] = {2,3,5};
a.v = x;
b.v = y;
return 0;
}