Results 1 to 5 of 5

Thread: Operations in 2 dimensional array

  1. #1
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Operations in 2 dimensional array

    Hello!
    I am facing a complex problem with a 2 dimensional array. I would be grateful if someone could help.
    Here is my code:

    Qt Code:
    1. std::string str1 = "ABABA";
    2. std::string str2 = "BBBBBAAA";
    3. double diag;
    4. typedef std::pair<double, Direction> Cell;
    5. matrix<Cell> m (str1.size()+1, str2.size()+1);
    6.  
    7. // Initialisation.
    8. for(size_t i=0;i<m.size1();++i)
    9. {
    10. m.at_element(i, 0).first = static_cast<float>(i)*(-1.f);
    11. }
    12. for(size_t j=1;j<m.size2();++j)
    13. {
    14. m.at_element(0, j).first = static_cast<float>(j)*(-1.f);
    15. }
    16. // Operations
    17. for(size_t i = 1; i<m.size1(); ++i)
    18. {
    19. for(size_t j = 1; j<m.size2(); ++j)
    20. {
    21. diag = m.at_element(i-1, j-1).first;
    22. diag += 1;
    23. }
    24. }
    25.  
    26. //Display
    27. std::cout<<" ";
    28. for(size_t j=0;j<m.size2 ();++j)
    29. {
    30. std::cout << str2[j] << " ";
    31. }
    32. std::cout << "\n";
    33.  
    34. for(size_t i=0;i<m.size1 ();++i)
    35. {
    36. if(i==0)
    37. {
    38. std::cout <<" ";
    39. }
    40. else
    41. {
    42. std::cout << str1[i-1]<<" ";
    43. }
    44.  
    45. for(size_t j=0;j<m.size2();++j)
    46. {
    47. std::cout << m.at_element(i,j).first<<" ";
    48. }
    49. std::cout <<"\n";
    50. }
    To copy to clipboard, switch view to plain text mode 

    The compilation is OK, but at the execution, I have only zeros in my cells.

    Question: What could be the source of this mistake?
    I would grateful to anyone you could help.

    Thanks in advance.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Operations in 2 dimensional array

    How does this affect to Qt? It's STL.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jan 2012
    Posts
    83
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Operations in 2 dimensional array

    I am using Qt. This is just a part of my program on which I am facing a problem.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Operations in 2 dimensional array

    Your problem has nothing to do with Qt. This is a Qt programming forum...

    Try here instead
    http://www.qtcentre.org/forums/9-General-Programming
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Operations in 2 dimensional array

    Qt Code:
    1. // Initialisation.
    2. for(size_t i=0;i<m.size1();++i)
    3. {
    4. m.at_element(i, 0).first = static_cast<float>(i)*(-1.f);
    5. }
    6. for(size_t j=1;j<m.size2();++j)
    7. {
    8. m.at_element(0, j).first = static_cast<float>(j)*(-1.f);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Even though this is the wrong place to answer a C++ / STL question...

    Do you know what this matrix looks like after this initialization step? I do not know if the first index ("i") in at_element( i, j ) refers to row or column, so let's just assume it is the column index. It doesn't matter for this answer - the matrix is just transposed if i is the row index.

    By the way, the std:: pair<double, Direction> constructor will put garbage into the elements of the pair. It will not initialize the double to zero or "Direction" to whatever it should hold by default.

    And why do you use static_cast<float> when your Cell pair's "first" member is a double?

    Your matrix is small enough that you can type it out:

    Qt Code:
    1. Length of "ABABA" is 5 and length of "BBBBBAAA" is 8, so 5 columns and 8 rows.
    2. Your first initialization loop goes from i = 0 - 4, and sets the double part of Cell to -i for all cells m[i, 0].
    3. The second loop goes from j = 1 - 7 and sets the double to -j for all cells m[0, j].
    4. The remainder of the cells are undefined.
    5.  
    6. i = 0 1 2 3 4
    7. j = 0 0 -1 -2 -3 -4
    8. j = 1 -1 ? ? ? ?
    9. j = 2 -2 ? ? ? ?
    10. j = 3 -3 ? ? ? ?
    11. ...
    12. j = 7 -7 ? ? ? ?
    To copy to clipboard, switch view to plain text mode 

    Then, in your nested loops starting in line 17, you start with i = 1 and j = 1, and access m[i-1, j-1]. For the first access m[0,0], this is in fact zero. For the next one m[0,1], you get -1.

    So if this isn't what you intended to do when you wrote this code, you need to sit down with a pencil and a piece of paper and write out what you think your matrix should look like, and figure out why your code doesn't do that.

    But don't ask here. Do as Amleto said and move your discussion to the general programming section.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 18th December 2011, 23:34
  2. QwtPolar: Plot using a 2-dimensional array
    By hgstoehr in forum Newbie
    Replies: 0
    Last Post: 14th December 2011, 21:39
  3. Multi-dimensional objects array
    By matulik in forum Newbie
    Replies: 3
    Last Post: 23rd October 2011, 02:20
  4. two dimensional array, size determined dynamically
    By feraudyh in forum General Programming
    Replies: 9
    Last Post: 10th June 2010, 03:00
  5. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 12:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.