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.
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.
Yes. Presumably you have completed parts (a) to (c)?Originally Posted by popo69
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.Originally Posted by popo69
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Code:#include <stdio.h>#include <stdlib.h> typedef struct Vector{ int dim; double *x; }Vector; Vector* allocVector(int); void freeVector(Vector*); void readVector(Vector *); void writeVector(Vector); void addVector(Vector,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); } void readVector(Vector *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)); }
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
The way I read the assignment you need to allocate an group of doubles not a group of vectors in allocVector.Code:v = (Vector *)malloc(n*sizeof(Vector));
Note: I use "group" because I could not think of the correct term to use.
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell
I suppose it might help to know what a vector is in the first place.
Vectors in Mathematics
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Any help now? I know it has many errors but i'm not able to figure out how to remove them.Code:#include <stdio.h> #include <stdlib.h> typedef struct Vector{ int dim; double *x; }Vector; Vector allocVector(int); void freeVector(Vector); void readVector(Vector *); void writeVector(Vector); Vector addVector(Vector, 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); readVector(&v1); readVector(&v2); return 0; } Vector allocVector(int dimension){ double *x = (double*)malloc(dimension*(sizeof(double))); if(x==NULL){ printf("Not enough memory available!"); } } void readVector(Vector *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])); } Vector addVector(Vector v1,Vector v2){ int i; for(i=0;i<v.dim;i++) v3->x[i] = *(v1->x[i]) + *(v2->x[i]); }
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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 readVector(Vector *); void writeVector(Vector); Vector addVector(Vector, Vector,int); 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); readVector(&v1); readVector(&v2); writeVector(v1); writeVector(v2); v3 = addVector(v1,v2,dim); 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; } void readVector(Vector *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])); } Vector addVector(Vector v1,Vector v2,int dim){ 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; }
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.)
Program is now working fine, but memory leak still exits. Any idea how to remove memory leak
==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)
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....)