Incrementing a hex value for a function parameter in a loop instead of hardcoding it?

This is a discussion on Incrementing a hex value for a function parameter in a loop instead of hardcoding it? within the C++ Programming forums, part of the General Programming Boards category; I have a constructor that takes 9 arguments, instead of harcoding each value in an array equal to each argument ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    37

    Incrementing a hex value for a function parameter in a loop instead of hardcoding it?

    I have a constructor that takes 9 arguments, instead of harcoding each value in an array equal to each argument I was hoping I could run through a loop and set each value of the array column/row equal to the paramater name plus a counter. I named the arguments a, b, c, d, e, f, g, h, and i to try to achieve this.

    Code:
    Matrix::Matrix(float a, float b, float c, float d, float e, float f, float g, float h, float i)
    {
        int counter = 0;
    
        for(int i = 0; i < MAX_SIZE; i++)
            for(int j = 0; j < MAX_SIZE; j++)
                mVals[i][j] = 0x61 + counter++;
    
        //Don't want to hardcore 9 of these
        //mVals[0][0] = a; 
        //mVals[0][1] = b;
    }
    This is what I was hoping to do, but instead its giving me the decimal value of 0x61 which is 97 + 1, +2, +3...etc.

    I've tried a char() cast and I can't get anything to work. MAX_SIZE is 3, using this for a 3x3 matrix. Is this not possible since its a variable name?

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your idea would work in a scripting language, but a C++ (or C) program does not know the variable names after it is compiled. Why don't you just pass in an array?

    EDIT: Actually, another idea is to use a variable argument list. See the cstdarg.h header.

    EDIT2: Now that I think about it, using a variable argument list is not a very good idea as it forgoes any type checking.

    Since there's only 9 values, you may as well just bite the bullet and assign them all individually.
    Last edited by oogabooga; 10-04-2012 at 05:09 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,146
    O_o

    This is C++ so I could show you a hundred different ways to emulate the look, but you'd still be doing a lot of work.

    So yeah, this is one of those times when just doing the work for what you need done is for the best.

    Soma

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    If you are programming under C++11, you can take advantage of initializer lists:

    Code:
    class Matrix
    {
    public:
    
        Matrix(std::initializer_list<float> list) noexcept
        {
            auto it = list.begin();
            for (std::size_t row = 0; row < 3; ++row)
            {
                for (std::size_t column = 0; column < 3; ++column)
                {
                    if (it != list.end())
                    {
                        mElements[row][column] = *it;
                        ++it;
                    }
                    else
                    {
                        mElements[row][column] = 0;
                    }
                }
            }
        }
    
    private:
    
        float mElements[3][3];
    };
    
    int main()
    {
        Matrix matrix{100, 200, 300, 400, 500, 600, 700};
        ...
        return 0;
    }
    This also enables you to use a variable-length argument list, which fills the unspecified elements with zeros (you can make the initializer_lists nest).
    With this approach you could even make your Matrix a template and get rid of the hardcoded dimensions.

    The downside is that in the case of matrix math, the initializer_list solution may turn out not to be performance-wise.
    Last edited by kmdv; 10-07-2012 at 09:59 AM.
    I never put signature, but I decided to make an exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function parameter/ function argument
    By codecaine_21 in forum C Programming
    Replies: 2
    Last Post: 09-24-2010, 08:09 PM
  2. how to add a parameter to the function
    By jayfriend in forum C Programming
    Replies: 5
    Last Post: 01-16-2007, 08:31 PM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Incrementing a number each time a function is called.
    By Coder87C in forum C++ Programming
    Replies: 17
    Last Post: 01-31-2005, 06:04 PM
  5. Parameter in a function
    By cpluspluser in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2003, 07:48 PM

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