Thread: Fast transposing matrix in C

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    5

    Fast transposing matrix in C

    Hey everyone!
    I'm kind of a beginner so please bear with me.
    I was given pseudo-code and using that I needed to build my own function. That said I'm finding a problem during the execution even though i get no particular error after compiling

    My code is as follows:

    Code:
    void transposeSparseMatrix(Triple * a, Triple * b){    
        int i;
        int * cur_pos = 0;
        int num_col = a[0].column;
        int num_val = a[0].value;
        b[0].row = num_col;
        b[0].column = a[0].row;
        b[0].value = num_val;
    
    
        int * terminiRiga = malloc(sizeof(Triple *)*NUMR);
        int * posIniziale = malloc(sizeof(Triple *)*NUMC);
    
    
        if (num_val > 0){
            for (i = 0; i<=num_col-1; i++){
                terminiRiga[i] = 0;
            }
            for (i = 1; i<=num_val; i++){
                terminiRiga[a[i].column] = terminiRiga[a[i].column]+1;
            }
            posIniziale[0] = 1;
            for (i = 1; i <= num_col-1; i++){
                posIniziale[i] = posIniziale[i-1] + terminiRiga[i-1];
            }
            for (i = 1; i<=num_val; i++){
                *cur_pos = posIniziale[a[i].column];  //this line is what my debugger points as reason for the function not working
                printf("\ncur_pos: %d\n", *cur_pos);  //printf best debugger, it gives me weird values
                posIniziale[a[i].column] = posIniziale[a[i].column]+1;
                b[*cur_pos].row = a[i].column;
                b[*cur_pos].column = a[i].row;
                b[*cur_pos].value = a[i].value;
            }
        }
    
    
    }
    Would anyone mind explaining what I'm doing wrong?

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This declares a pointer and initialises it to be a null pointer:
    Code:
    int * cur_pos = 0;
    Consequently, cur_pos needs to actually point to an int object, otherwise you cannot do this:
    Code:
    *cur_pos = posIniziale[a[i].column];
    since you cannot dereference a null pointer. Perhaps you should just make cur_pos an int instead.

    Also, this looks suspect:
    Code:
    int * terminiRiga = malloc(sizeof(Triple *)*NUMR);
    int * posIniziale = malloc(sizeof(Triple *)*NUMC);
    terminiRiga is a pointer to an int, so we would expect that it either points to a single standalone int object, or it points to some int object (typically the first) in an array of int objects, or perhaps one past the end of such an array of int objects. However, according to the malloc call, you allocate space for NUMR objects of type pointer to Triple. If this malloc call is correct, then we would expect terminiRiga to be a pointer to a pointer to Triple:
    Code:
    Triple **terminiRiga = malloc(sizeof(Triple*) * NUMR);
    and in fact we could write:
    Code:
    Triple **terminiRiga = malloc(sizeof(terminiRiga[0]) * NUMR);
    However, it looks like you use terminiRiga as if it were an array of int, in which case you should have written:
    Code:
    int *terminiRiga = malloc(sizeof(terminiRiga[0]) * NUMR);
    Likewise for posIniziale.

    That said, this might make no real difference because it could be the case that sizeof(Triple *) == sizeof(int), i.e., it would be good for correctness, but not necessarily change anything in the resulting compiled code.
    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
    Jun 2018
    Posts
    5
    I managed to make it work, thanks! I did also use your correction regarding this

    1
    int *terminiRiga = malloc(sizeof(terminiRiga[0]) * NUMR);
    so I'm guessing I was having troubles with memory allocation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. possible fast rearrange 3D matrix?
    By robness in forum C Programming
    Replies: 6
    Last Post: 11-01-2015, 08:14 PM
  2. Matrix math logic with array! Need Fast Help!
    By loserone+_+ in forum C Programming
    Replies: 14
    Last Post: 01-14-2013, 05:30 AM
  3. Transposing a matrix made from a 1d array
    By brown in forum C Programming
    Replies: 4
    Last Post: 10-06-2009, 02:05 PM
  4. Transposing matrix
    By 2112 in forum C Programming
    Replies: 16
    Last Post: 02-08-2008, 05:29 PM
  5. transposing matrix from file to file
    By inky in forum C Programming
    Replies: 4
    Last Post: 07-19-2005, 08:25 PM

Tags for this Thread