Results 1 to 4 of 4

Thread: Question About Arrays inside functions

  1. #1
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Question About Arrays inside functions

    Hi,

    I am working in a simple QConsole application, and what I want to do now is some matrix operations.

    Let me explain on the go:

    Outside my main as global variables I have defined two matrices:

    Qt Code:
    1. QVector<QVector<int> > A, B;
    To copy to clipboard, switch view to plain text mode 

    Inside the main, I set the sizes to them using:

    Qt Code:
    1. A.resize(10);
    2. B.resize(10);
    3. for(int i=0; i<10; i++)
    4. {
    5. A[i].resize(10);
    6. B[i].resize(10);
    7. }
    To copy to clipboard, switch view to plain text mode 

    And then I fill the A matrix with random values from 1 to 10.

    What I want to do in some point, is to multiply matrix A by some scalar, but I want the result to be stored in matrix B.

    So, I wrote the following void function:

    Qt Code:
    1. void Multiply(int scalar, int rows, int cols, QVector<QVector<int> > matrix, QVector<QVector<int> > result)
    2. {
    3.  
    4. for(int i=0; i<rows; i++)
    5. {
    6. for(int j=0; j<cols; j++)
    7. {
    8. result[i][j]=scalar*matrix[i][j];
    9. }
    10. }
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    And then, inside main, I call, for example:

    Qt Code:
    1. Multiply(5, 10, 10, A, B);
    To copy to clipboard, switch view to plain text mode 

    But the function is not working because matrix B ends up with all values to 0.
    However, if I debug and print the values of result[i][j] inside my function, it shows the correct values for the operation.

    Do you know why is that happening?

    I tried the same multiply function using regular C arrays:

    Qt Code:
    1. void Multiply(int scalar, int Matrix[10][10], int Result[10][10])
    To copy to clipboard, switch view to plain text mode 

    And it works well that way...

    Can you tell me why my void function is not working properly? Should I try some other way to do the same, since I wanted to do some matrix operations the same way, for example, multiply matrix A with B, and store the result in other matrix C, using some similar void function.

    Thanks!

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question About Arrays inside functions

    You're passing your matrices by value. This results in a copy being made when the function is entered; all operations take place on those copies, not on the original matrices.

    You need to pass them by reference

    Qt Code:
    1. QVector<QVector<int> >& matrix, QVector<QVector<int> >& result
    To copy to clipboard, switch view to plain text mode 

    (Note the ampersands.)

    The C-style version works because you are effectively doing the same thing. You're passing in a pointer to the external arrays, not a copy. Pass-by-reference works exactly the same way, while providing some syntactic sugar by eliminating the need to dereference the arguments.

  3. The following user says thank you to SixDegrees for this useful post:

    rdelgado (15th June 2011)

  4. #3
    Join Date
    Jun 2011
    Posts
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Question About Arrays inside functions

    QVector<QVector<int> >& result

  5. The following user says thank you to deyili for this useful post:

    rdelgado (15th June 2011)

  6. #4
    Join Date
    Nov 2009
    Posts
    29
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Question About Arrays inside functions

    It Worked! Thanks!!!

Similar Threads

  1. Cannot call OpenCV 2.0 functions inside Qt slots
    By Asfer in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2010, 11:48
  2. debugging arrays
    By divide in forum Qt Tools
    Replies: 1
    Last Post: 12th October 2009, 21:17
  3. QtScript and arrays
    By supergillis in forum Qt Programming
    Replies: 1
    Last Post: 20th May 2009, 19:13
  4. Question about functions in classes
    By cwnelatury in forum Newbie
    Replies: 1
    Last Post: 13th May 2009, 06:05
  5. simple question on pointer-arrays
    By mickey in forum General Programming
    Replies: 2
    Last Post: 17th February 2007, 01:11

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.