Thread: Saving matrices with data structures-"get" and "set" confusion

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    4

    Question Saving matrices with data structures-"get" and "set" confusion

    Hi everyone,
    I´m taking a programming class and currently we´re doing data structures. Today, we discussed how to save square matrices column-wise with data structures. The teacher said that after the first few steps where you declare the structure, allocate the matrix, free it and get the dimension (which mostly make sense to me I think), you have to "get" and "set" the matrix by putting in something like

    Code:
    void setMatrixEntry(Matrix* A, int i, int j, double Aij) 
    {
    
    A->entries[i+j*A->m] = Aij;
    
    }
    
    
    double getMatrixEntry(Matrix* A, int i, int j) 
    {
    
    return A->entries[i+j*A->m]
    }
    I´m really confused here because I can´t figure out what "A->entry[i+j*A->m] = Aij;" and "return A->entry[i+j*A->m]" do exactly or how you get to them.

    I´d be really grateful for any help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should show the definition of Matrix to make it easier to explain.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    4
    The data structure is

    Code:
    
    
    typedef struct _Matrix_ {
    
    int m;
    
    int n;
    
    double* entry;
    
    } Matrix;
    
    




    and the allocation is:



    Code:
    
    
    Matrix* newMatrix(int m, int n) {
    
    int i = 0;
    
    Matrix* A = malloc(sizeof(Matrix));
    
    A->m = m;
    
    A->n = n;
    
    A->entry = malloc(m*n*sizeof(double));
    
    for (i=0; i<m*n; ++i) {
    
    A->entry[i] = 0;
    
    }
    
    return A;
    
    }
    
    


    Is that what you meant?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. Let's consider getMatrixEntry:
    Code:
    return A->entries[i + j * A->m];
    The A-> part is simple: A is a pointer, so we use -> to dereference A while accessing the entries member of the Matrix object.

    Now, consider the entries member: this is a pointer to double. Obviously, we cannot use normal 2D array notation. Therefore, we need to compute the index of the element. This is what the i + j * A->m expression is about. In C, we normally use row-major ordering, so matrix[i][j] would correspond to A->entries[i * A->m + j]. The idea here is that we skip i * A->m number of elements in the dynamic array (i.e., skip i number of rows), then skip j number of elements (i.e., j number of columns within a row) to get to the element corresponding to matrix[i][j].

    In this case though, you're using column-major ordering ("save square matrices column-wise"), hence we use the opposite: skip j * A->m number of elements (i.e., j number of columns) then skip i number of elements (i.e., i number of rows within a column), thus A->entries[i + j * A->m].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    4
    Thank you, I think I understand how to get to it now!

    I´ve still got one question, though...
    What exactly is
    return A->entries[i + j * A->m]; supposed to do? The idea is that a user puts in entries and they get saved in the matrix (I think). Why do we need A->entries[i + j * A->m]; ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Suuper Susi
    What exactly is
    return A->entries[i + j * A->m]; supposed to do? The idea is that a user puts in entries and they get saved in the matrix (I think). Why do we need A->entries[i + j * A->m]; ?
    Return the value of the entry at position i, j in the matrix.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2014
    Posts
    4

    Smile

    Ok, thank you!

    And just to make sure I´ve understood this...does setMatrixEntry give every entry the value/name Aij?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Suuper Susi View Post
    Ok, thank you!

    And just to make sure I´ve understood this...does setMatrixEntry give every entry the value/name Aij?
    Every entry? Can you explain why you think that? It sounds like you are just guessing and you don't understanding laserlight's explanations at all. I'm being genuine, you have a misunderstanding that needs to be sorted out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Edible data structures" presents...
    By Matticus in forum General Discussions
    Replies: 6
    Last Post: 08-03-2014, 06:34 AM
  2. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  3. "Data Fork"/"Alternate Data Streams"
    By phantomotap in forum Tech Board
    Replies: 3
    Last Post: 08-06-2010, 11:01 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread