Thread: for loops

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    Question for loops

    Hi all,

    Can anyone help me with the following problem:

    I want to cycle through a multidimensional matrix M.
    Suppose this matrix has dimension 2 with boundaries aa and bb, then I could use something like this:

    Code:
    for(a=0; a<aa; a++)
    {
       for(b=0; b<bb; b++)
       { 
          M[a][b] = ...;
       }
    }
    But now, suppose that at compile time I don't now the dimensions nor the boundaries of matrix M!
    I want to write some kind of general code that can cope with all possible dimensions of the matrix. So, if the matrix has dimension three, the code will automatically add a third 'for' loop.

    Is this possible or should I do this completely different?

    Thanks in advance!

    Bropolwig

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    In C++, is it possible to define an array with unknown dimensions (C99?)?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    How do you define your matrix?

    The recursive approach is most simple to produce:

    Code:
    func(matrix):
      if matrix is an array:
        for (i = 1 to len(matrix)) do func(matrix(i))
      else
        it's a scalar number, process it
    end func

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well to allow run-time choice of aa, bb you would do something like this
    Code:
    int **M;
    M = new int*[aa]
    for ( int i = 0 ; i < aa ; i++ ) M[i] = new int[bb]
    There isn't a good way of varying the number of dimensions at run-time, since the root type (int ** in this case) has to be fixed at compile time.

    I suppose you could code each matrix element as a list, which can contain other lists to inner dimensions of the matrix (and so on recursively), but you'd lose the easy and quick subscripted access method doing so.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Using dynamic memory allocation I can indeed choose aa and bb at runtime.

    The idea behind the undefined dimensions of the matrix is that I have an undefined parameterspace at start. When the program executes the user enters the number of parameters. Each matrix dimension represents a parameter and I want to evaluate the whole parameterspace by cycling through all dimensions of the matrix.

    Maybe I can do this smarter?

    Thanks

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Possibly. What are you trying to implement?

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Oh, it is just a hobby project of mine

    For this specific part, I just want to be able to store an outcome (int value) for every combination of parameters, given that the number and range of parameters is set at run-time.

    Thanks,
    Bropolwig

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    I meant, why do you go through those values? To find the optimum in some respect, to calculate a statistic, something else? There are better algorithms than a brute-force search.

    Also, let's say you have 10 parameters each of which can take 100 values which is pretty modest. Then the entire search space contains 100^10 values which is a mighty big number. You can't really loop over that. Not to mention the amount of memory storing the matrix would require.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Indeed, this is what I want to do: find an optimum.

    But even if there are smarter ways of searching instead of looping through all number, then I still need to store them first, right?

    Could you point me to some of those smarter algorithms joni?

    Thanks a lot for the help,

    Bropolwig

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    No, you don't need to store the values. You just need to have a way to calculate them, which you do seem to have.

    The technique you would use depends on the nature of the problem, such as if there are many optimums or only a unique one, or whether you are looking for local or global optimums. I'm no expert in optimization but I've heard evolutionary algorithms have often been used with good results when the parameter space has a big dimension. There's a pretty good list of different techniques at wikipedia:
    http://en.wikipedia.org/wiki/Optimiz...%29#Techniques

  11. #11
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    So you're saying a and b can be very high, but you dont know just how?

    One way is, use a single dimension array that can expand to your whim.

    Code:
    #include <vector>
    #include <iostream>
    using namespace std;
    
    int main() {
     vector <int> matrix; //Creates a vector with an expandable size.
     unsigned int x;
    
     matrix.push_back(10); //Adds an element to the vector, with a value of 10.
     matrix.push_back(5); //Adds another element with a value of 5.
    
     for(x=0; x < matrix.size(); x++)
      cout << matrix[x]; //Couts the integer value of matrix[x].
    
     matrix.pop_back(); //Deletes the last added element of the vector.
    }
    Your other option (WARNING! DO NOT RUN THIS CODE!)
    Code:
    int m[9999][9999];
    You might be able to use a 2d vector, but I dont know. Wait...

    Code:
    vector <int> MatrixPartOne;
    vector <int> MatrixPartTwo;
    Their :P. The only problem is its two seperate entities. If you want more on vectors, http://www.cppreference.com/cppvector/ .

    If I didint answer your question, I blame vectors for intoxicating me .
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Well, the real problem, Blackroot, is that I don't know at compile time whether or not there is another dimension c, or d, or etc... of the matrix.

Popular pages Recent additions subscribe to a feed