how do I set vectors values to one value?

This is a discussion on how do I set vectors values to one value? within the C Programming forums, part of the General Programming Boards category; I have a vector int a[100], initialised with values such as 9726745. How do I set them all to a ...

  1. #1
    Registered User fe00h's Avatar
    Join Date
    Jun 2003
    Posts
    7

    how do I set vectors values to one value?

    I have a vector int a[100], initialised with values such as 9726745. How do I set them all to a specific value x without a for statement?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I don't think you can do it without looping (at least indirectly).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Banned
    Join Date
    May 2003
    Posts
    124
    >without a for statement?
    With a while statement

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You can use memset() to do this.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Edited: Irrelevant.
    Last edited by XSquared; 07-01-2003 at 02:15 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by XSquared
    It's not safe to use memset( ) with a vector.
    Never mind the fact that there are no vectors in C.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Are we talking about an array here or a vector class? For an array, my example will most certainly work. For a vector class it won't, but this is the C programming board not C++.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Heh. Oops.

    As soon as I saw vector my mind snapped into C++ mode.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    148
    Proper handling of memset is save with an array.
    It is always save to set all bits to zero in an array of char (signed or unsigned),but thats all.C allows every single
    data type except {signed,unsigned} char to have padding bits, that are not part
    of the representation of the value. It is possible that improper
    combinations of these padding bits can generate what is called a trap
    representation.
    So,for example memseting an array of ints to zero is not save (nonportable).

    [OT]
    The memory of std::vector is continous,memseting a std::vector of {unsigned,signed} chars is save too.
    [/OT]

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Please, just take the easy obvious way:

    Code:
    int a[100];
    for (int i=0;i<100;i++)
    {
       a[i]=0;
    }
    Last edited by VirtualAce; 07-02-2003 at 06:37 AM.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    One way to assign to an array without a loop is to wrap the array in a structure.
    Code:
    #include <stdio.h>
    
    struct sType
    {
       int a[10];
    };
    
    const struct sType Default = {{56,7,8,1,27,13,54,82,23,16}};
    
    void show(const int *array, size_t size)
    {
       size_t i;
       for ( i = 0; i < size; ++i )
       {
          printf("%2d%c", *array++, i % 10 == 9 ? '\n' : ',');
       }
    }
    
    int main(void)
    {
       struct sType object = {{83,6,71/*remaining elements are 0*/}};
       fputs("Before: ", stdout);
       show(object.a, sizeof(object.a)/sizeof(*object.a));
    
       object = Default;
       fputs("After:  ", stdout);
       show(object.a, sizeof(object.a)/sizeof(*object.a));
       return 0;
    }
    
    /* my output
    Before: 83, 6,71, 0, 0, 0, 0, 0, 0, 0
    After:  56, 7, 8, 1,27,13,54,82,23,16
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's an obvious answer: at the time of declaration:

    int foo[5] = { 1, 1, 1, 1, 1 };

    Of course, you could always use recursion:
    Code:
    void fill( int array[], size_t size, int value )
    {
        if( size > -1 )
        {
            fill( array, size-1, value );
            array[size] = value;
        }
    }
    There's a few missed ways. That'll suffice for now.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  2. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  3. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 01:56 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM

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