Thread: Need help regarding assignment

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    7

    Need help regarding assignment

    Need help regarding assignment-screenshot-2018-10-16-09-08-55-jpg

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,922
    Quote Originally Posted by popo69
    Do anybody have idea how to implement this in 'C' language?
    Yes. Presumably you have completed parts (a) to (c)?

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    7
    Quote Originally Posted by laserlight View Post
    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 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));
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,922
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,880
    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.
    "...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

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    957
    Quote Originally Posted by stahta01 View Post
    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.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,169
    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.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,880
    Quote Originally Posted by john.c View Post
    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.
    "...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

  9. #9
    Registered User
    Join Date
    Oct 2018
    Posts
    7
    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]);
    
    
    }
    Any help now? I know it has many errors but i'm not able to figure out how to remove them.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,922
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2018
    Posts
    7
    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;
    
    
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #13
    Registered User
    Join Date
    Oct 2018
    Posts
    7
    Program is now working fine, but memory leak still exits. Any idea how to remove memory leak

  14. #14
    Registered User
    Join Date
    Oct 2018
    Posts
    7
    ==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. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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 subscribe to a feed

Similar Threads

  1. Need help with an assignment
    By Jordan Velez in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2015, 12:48 PM
  2. Need Help On an Assignment
    By Erik Ingvoldsen in forum C++ Programming
    Replies: 191
    Last Post: 04-07-2015, 11:03 AM
  3. my assignment
    By dalma in forum C Programming
    Replies: 3
    Last Post: 03-15-2011, 04:47 PM
  4. Help - Assignment
    By hasalams in forum C Programming
    Replies: 8
    Last Post: 06-07-2009, 02:17 AM
  5. Replies: 3
    Last Post: 04-26-2009, 08:54 AM

Tags for this Thread