# Thread: Need help regarding assignment

1. ## Need help regarding assignment Do anybody have idea how to implement this in 'C' language?

How to add two vectors with the help of dimension only? Any help ?

Thank you. 2. Originally Posted by popo69
Do anybody have idea how to implement this in 'C' language?
Yes. Presumably you have completed parts (a) to (c)? Originally Posted by popo69
How to add two vectors with the help of dimension only? Any help ?
You have both dimension and the pointer x for each of the two Vector objects. It looks like you should create a new Vector object of the same dimension, and have that hold the result of adding one of the Vector objects to the other. 3. Originally Posted by laserlight Yes. Presumably you have completed parts (a) to (c)?

You have both dimension and the pointer x for each of the two Vector objects. It looks like you should create a new Vector object of the same dimension, and have that hold the result of adding one of the Vector objects to the other.
Code:
```#include <stdio.h>#include <stdlib.h>
typedef struct Vector{
int dim;
double *x;
}Vector;

Vector* allocVector(int);
void freeVector(Vector*);
void writeVector(Vector);

int main(){
return 0;
}

Vector* allocVector(int n){
Vector *v;
v = (Vector *)malloc(n*sizeof(Vector));
if(v==NULL){
printf("\nNot enough memory available!");
return NULL;
}
return v;
}

void freeVector(Vector* v){
free(v);
}

printf("\nEnter the dimension of the vector : ");
scanf("%d",&v->dim);
printf("\nEnter the value of x : ");
scanf("%lf",&v->x);
}

void writeVector(Vector v){
printf("Dimension of vector : %d",*(v->dim));
printf("Value of x : %lf",*(v->x));
}``` 4. Your allocVector function is wrong. Notice that your assignment specifies a return type of Vector, not Vector*. The idea is that because the Vector object only consists of an integer and a pointer, it is cheap to pass around by value, so you don't need to simulate pass by reference by passing a pointer. Furthermore, instead dynamically allocating space for the Vector object itself, you should dynamically allocate space for the dynamic array of double that has dim number of elements. Likewise, your freeVector is also wrong in its parameter and what it does. 5. Code:
`v = (Vector *)malloc(n*sizeof(Vector));`
The way I read the assignment you need to allocate an group of doubles not a group of vectors in allocVector.

Note: I use "group" because I could not think of the correct term to use.

Tim S. 6. Originally Posted by stahta01 I use "group" because I could not think of the correct term to use.
It's "bevy".

A gaggle of chars.
A pack of shorts.
A herd of ints.
A pride of longs.
A throng of floats.
A bevy of doubles.
A murder of pointers. 7. I suppose it might help to know what a vector is in the first place.
Vectors in Mathematics 8. Originally Posted by john.c It's "bevy".

A gaggle of chars.
A pack of shorts.
A herd of ints.
A pride of longs.
A throng of floats.
A bevy of doubles.
A murder of pointers.
LOL.

I esp. like the "murder of pointers".

Tim S. 9. Code:
```#include <stdio.h>
#include <stdlib.h>
typedef struct Vector{
int dim;
double *x;
}Vector;

Vector allocVector(int);
void freeVector(Vector);
void writeVector(Vector);

int main(){

Vector v1,v2,v3;
int dim;
printf("Enter the dimension of the vector ");
scanf("%d",&dim);

v1 = allocVector(dim);
v2 = allocVector(dim);
v3 = allocVector(dim);

return 0;
}

Vector allocVector(int dimension){
double *x = (double*)malloc(dimension*(sizeof(double)));
if(x==NULL){
printf("Not enough memory available!");
}
}

int i;
printf("Enter the %d-dimensional vector of real numbers ",v->dim);
for(i=0;i<v->dim;i++)
scanf("%lf ",&(v->x[i]));
}

void writeVector(Vector v){
int i;
printf("A %d-dimensional vector of real numbers is : ",v.dim);
for(i=0;i<v.dim;i++)
printf("%lf ",*(v->x[i]));
}

int i;

for(i=0;i<v.dim;i++)
v3->x[i] = *(v1->x[i]) + *(v2->x[i]);

}```
Any help now? I know it has many errors but i'm not able to figure out how to remove them. 10. Your allocVector function is closer to the mark, but still wrong: you need to create a Vector object and have that Vector object's x member point to the memory allocated. You also need to set its dimension, and then return the object. 11. Modified whole code. Geting Segmentation fault (core dumped)

Code:
```#include <stdio.h>
#include <stdlib.h>
typedef struct Vector{
int dim;
double *x;
}Vector;

Vector allocVector(int);
void freeVector(Vector);
void writeVector(Vector);

int main(){

Vector v1,v2,v3;
int dim;
printf("Enter the dimension of the vector ");
scanf("%d",&dim);

v1 = allocVector(dim);
v2 = allocVector(dim);
v3 = allocVector(dim);

writeVector(v1);
writeVector(v2);

writeVector(v3);

return 0;
}

Vector allocVector(int dimension){
Vector v;
v.x = (double*)malloc(dimension*(sizeof(double)));
if(v.x==NULL){
printf("Not enough memory available!");
}
return v;
}

int i;
printf("Enter the %d-dimensional vector of real numbers ",v->dim);
for(i=0;i<(v->dim);i++)
scanf("%lf ",&(v->x[i]));
}

void writeVector(Vector v){
int i;
printf("A %d-dimensional vector of real numbers is : ",v.dim);
for(i=0;i<v.dim;i++)
printf("%lf ",(v.x[i]));
}

int i;
Vector v3;
v3 = allocVector(dim);
for(i=0;i<v3.dim;i++)
v3.x[i] = (v1.x[i]) + (v2.x[i]);

return v3;

}``` 12. Somebody has to set the dimension of your vectors. You can do it inside the allocVector function, I suppose, or before you call the alloc function, but right now all your vectors have a random number for the dimension.

(In addition, you have a memory leak, as you allocate memory for v3 in your main program, but then allocate a new bunch of memory inside your addVector and overwrite the pointer in v3, so that memory is no longer accessible.) 13. Program is now working fine, but memory leak still exits. Any idea how to remove memory leak 14. ==3637==
==3637== HEAP SUMMARY:
==3637== in use at exit: 16 bytes in 1 blocks
==3637== total heap usage: 6 allocs, 5 frees, 2,112 bytes allocated
==3637==
==3637== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3637== at 0x4C2DBF6: malloc (vg_replace_malloc.c:299)
==3637== by 0x400839: allocVector (oct7.c:49)
==3637== by 0x400747: main (oct7.c:27)
==3637==
==3637== LEAK SUMMARY:
==3637== definitely lost: 16 bytes in 1 blocks
==3637== indirectly lost: 0 bytes in 0 blocks
==3637== possibly lost: 0 bytes in 0 blocks
==3637== still reachable: 0 bytes in 0 blocks
==3637== suppressed: 0 bytes in 0 blocks
==3637==
==3637== For counts of detected and suppressed errors, rerun with: -v
==3637== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0) 15. Your addVector creates a new vector, so the v3 you initially created is leaked. Since addVector is required to return a Vector, you shouldn't alloc v3 before you call the add function.

(EDIT TO ADD: Your valgrind output above even tells you that line 27 is the problematic line....) Popular pages Recent additions add, dimension, language, structures pointers array, vectors 