concatenate

This is a discussion on concatenate within the C Programming forums, part of the General Programming Boards category; Hi guys. I wanted to build a function with a variable number of arguments that is able to concatenate in ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    50

    concatenate

    Hi guys. I wanted to build a function with a variable number of arguments that is able to concatenate in the same vector the vectors given as input (that can be 2,3, 100). I was thinking to something like this
    Code:
    void concatenate(double *res, double *vec1, int length_vec1, ...)
    {
    
    }
    I tried to use va_args/start ecc but they seem (up to me) to handle only scalars...Has anyone a hint?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You've to make sure that res has enough space.
    scalars ? Are you perl programmer?! :P
    Code:
         
                 va_list ap;
                 va_start(ap,length_vec1); 
                 arg = va_arg(ap,double*);

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by Bayint Naung View Post
    You've to make sure that res has enough space.
    scalars ? Are you perl programmer?! :P
    Code:
         
                 va_list ap;
                 va_start(ap,length_vec1); 
                 arg = va_arg(ap,double*);
    OOOOOOOOK! For res there are no problems...No, I am only a poor statistician that is reaching for speed :-) . Now I am going to use your hint!
    Thanks a lot!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    50
    I wonder if is it possible to handle vectors with variable length, I mean
    Code:
    void concatenate(double *res, double *vec1, int length_vec1, double *vec2, int length_vec2)
    {
    ...
    }
    ?

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What are you trying to do?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    50
    I am trying to do a function that is able to concatenate a variable number of vectors, e.i.
    Code:
    concatenate(res,vec1,length_vec1,vec2 ,length_vec2) or
    concatenate(res,vec1,length_vec1,vec2 ,length_vec2, vec3 ,length_vec3)
    and so on. Maybe is more feasible to group the length of the vectors
    Code:
    concatenate(res,length_vec1,length_vec2,vec,vec21)

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    E.g variable argument function.
    Code:
    void show_strs(char *s,...)
    {
        va_list ap;
        va_start(ap,s);
        do {
          printf ("%s\n",s);
          s = va_arg(ap,char * );
       } while( s != NULL );
       va_end(ap);
    
    }
    call it like this:
    
      show_strs("hello", "world","variable",(char*) 0 );
      show_strs("only one string",(char*) 0 );
      show_strs("This is a ","1st","2nd","3rd","4th","end",(char*) 0);

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by Bayint Naung View Post
    E.g variable argument function.
    Code:
    void show_strs(char *s,...)
    {
        va_list ap;
        va_start(ap,s);
        do {
          printf ("%s\n",s);
          s = va_arg(ap,char * );
       } while( s != NULL );
       va_end(ap);
    
    }
    call it like this:
    
      show_strs("hello", "world","variable",(char*) 0 );
      show_strs("only one string",(char*) 0 );
      show_strs("This is a ","1st","2nd","3rd","4th","end",(char*) 0);
    of course, but with the difference that I use only arrays of numbers. This is, up to me, the main difficulty I have found in doing this function.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Post your code snippet and a sample of the data you're trying to manipulate.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by itCbitC View Post
    Post your code snippet and a sample of the data you're trying to manipulate.
    ok...I post you my barbarian solution ;-) to the problem. Here it is the case where I have to concatenate 3 vectors
    Code:
    void cb_c3(double *res, int *n1, int *n2, int *n3, double *vec1, double *vec2, double *vec3)
    {
    	int i;
    	for( i=0 ; i<*n1 ; i++) res[i] = vec1[i];
    	for( i=0 ; i<*n2 ; i++) res[i+*n1] = vec2[i];
    	for( i=0 ; i<*n3 ; i++) res[i+*n1+*n2] = vec3[i];
    }
    Now, my standard glod will be a function tha looks like this
    Code:
    void cb_c3(double *res,  int n1, double *vec1, ...)
    {
    ...
    }
    where I put the different lengths of the vectors and the first pointer to the vector.
    Do you understand? Does anyone knows R? There is a function called c() that concatenates vectors or scalars in the order they appear. This is what I want

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    You want to put together different types of vectors, as they are called in mathematics, to a single one (note the emphasis on types)? Or just the same type, and if so, what type?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by Elysia View Post
    You want to put together different types of vectors, as they are called in mathematics, to a single one (note the emphasis on types)? Or just the same type, and if so, what type?
    I want to pool only vectors of doubles or integers

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    Quote Originally Posted by violatro View Post
    where I put the different lengths of the vectors and the first pointer to the vector.
    Do you understand? Does anyone knows R? There is a function called c() that concatenates vectors or scalars in the order they appear. This is what I want
    Yeah, I know R and understand.

    With variable argument lists, C does not have any means to implicitly detect the number of arguments passed. You'll need to either pass the number of vectors explicitly or look for a marker indicating the last vector.

    A function of the form;
    Code:
    void c(double *result, int number_of_vectors, ...)
    will do the trick. After the number_of_vectors argument, interpret the arguments in pairs (eg an int specifying number of elements in the vector, a pointer to the first element).
    Right 98% of the time, and don't care about the other 3%.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    Quote Originally Posted by violatro View Post
    I want to pool only vectors of doubles or integers
    Ever considered you are using the wrong language? This would be easier (but still as fast) if you are using, say C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    50
    Quote Originally Posted by grumpy View Post
    Yeah, I know R and understand.

    With variable argument lists, C does not have any means to implicitly detect the number of arguments passed. You'll need to either pass the number of vectors explicitly or look for a marker indicating the last vector.

    A function of the form;
    Code:
    void c(double *result, int number_of_vectors, ...)
    will do the trick. After the number_of_vectors argument, interpret the arguments in pairs (eg an int specifying number of elements in the vector, a pointer to the first element).
    What it is not clear is how to tell C with va_ that I am handling vectors and not scalars. All the examples I have seen deals with scalars or with strings.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenate string and int
    By millsy5 in forum C Programming
    Replies: 1
    Last Post: 01-28-2010, 03:43 AM
  2. Concatenate a String?
    By sharkbate24 in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2009, 04:16 PM
  3. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 03:54 PM
  4. concatenate text files
    By acosgaya in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2006, 01:04 PM
  5. Concatenate chars and std::strings.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2005, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21