two array questions

This is a discussion on two array questions within the C++ Programming forums, part of the General Programming Boards category; Is there a way to set all the elements of an array to a certain number, say 1, without doing ...

  1. #1
    Railgun God |Wiz|'s Avatar
    Join Date
    Sep 2005
    Posts
    23

    two array questions

    Is there a way to set all the elements of an array to a certain number, say 1, without doing something like

    Code:
    hello[] = (1, 1, 1, 1, 1, 1, 1, 1, 1...)
    ?

    Currently I'm using (for a 3 by 3 array)

    Code:
    int cx = 0;
    int cy = 0;
    
    while (cy != 3)
    {
    array[cx][cy] = 1;
    
    if (cx == 2)
    {
    cx = 1;
    cy++;
    }
    else
    {
    cx++;
    }
    }
    and it's terribly bulky.

    Also, if you want to pass an array to a function, do you do something like

    Code:
    int main()
    {
    int *ptr;
    ptr = array;
    function(ptr);
    ...
    int function(int ptr)
    or just

    Code:
    int main()
    {
    function(array)
    ...
    function(int *array)
    ?

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by |Wiz|
    Is there a way to set all the elements of an array to a certain number, say 1, without doing something like

    Code:
    hello[] = (1, 1, 1, 1, 1, 1, 1, 1, 1...)
    there's no way to initialise them to 1 without doing that, but you can set them using memset

    Code:
    memset(hello, 1, sizeof(hello));
    Quote Originally Posted by |Wiz|
    Also, if you want to pass an array to a function, do you do something like

    Code:
    int main()
    {
    function(array)
    ...
    function(int *array)
    ?
    yes. do the second one.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    there's no way to initialise them to 1 without doing that, but you can set them using memset
    Only if he wants to set each byte to 1. If, for example, he wants to set each element of an integer array to one memset won't work properly.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by JaWiB
    Only if he wants to set each byte to 1. If, for example, he wants to set each element of an integer array to one memset won't work properly.
    very true. In that case ye olde for loop comes to the rescue...
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You should be using vector (or tr1::array), which happens to have a constructor that does that for you:
    Code:
    // Single array with 'size' values
    std::vector<int> hello(size, 1);
    // 3x3 array
    std::vector<std::vector<int> > array(3, std::vector<int>(3, 1));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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