Rotate matrix without []











up vote
-2
down vote

favorite












I need to rotate a matrix and call the funcion without using ,
I can't even think on a solution.



void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}


I don't know how to insert a value to matrix or how to swap










share|improve this question









New contributor




Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • how is matrix rotation related to ?
    – phuclv
    yesterday










  • I can't swap elements by using: a[i][j] = temp ,etc.
    – Itay Zemah
    yesterday















up vote
-2
down vote

favorite












I need to rotate a matrix and call the funcion without using ,
I can't even think on a solution.



void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}


I don't know how to insert a value to matrix or how to swap










share|improve this question









New contributor




Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • how is matrix rotation related to ?
    – phuclv
    yesterday










  • I can't swap elements by using: a[i][j] = temp ,etc.
    – Itay Zemah
    yesterday













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I need to rotate a matrix and call the funcion without using ,
I can't even think on a solution.



void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}


I don't know how to insert a value to matrix or how to swap










share|improve this question









New contributor




Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I need to rotate a matrix and call the funcion without using ,
I can't even think on a solution.



void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}


I don't know how to insert a value to matrix or how to swap







c arrays pointers






share|improve this question









New contributor




Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









phuclv

14.1k846203




14.1k846203






New contributor




Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









Itay Zemah

31




31




New contributor




Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Itay Zemah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • how is matrix rotation related to ?
    – phuclv
    yesterday










  • I can't swap elements by using: a[i][j] = temp ,etc.
    – Itay Zemah
    yesterday


















  • how is matrix rotation related to ?
    – phuclv
    yesterday










  • I can't swap elements by using: a[i][j] = temp ,etc.
    – Itay Zemah
    yesterday
















how is matrix rotation related to ?
– phuclv
yesterday




how is matrix rotation related to ?
– phuclv
yesterday












I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
yesterday




I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
yesterday












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You're mixing up the pointer and the dereferenced value in the 3 lines



In the first line temp = (int)(pS); you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i); which makes no sense.



Then in the last line (int*)((pS + j) + i) = temp; doesn't work because you're a value to an address instead of a memory location



Remember to use * to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b] is equivalent to *(a + b) so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j)) but didn't apply that to others



Even then you're calculating the index incorrectly. *((int*)((pS + i) + j)) is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]



So to do



temp = pS[i + j*width];
pS[i + j*width] = pS[j + i*width];
pS[j + i*width] = temp;


just change it to



temp = *(pS + i + j*width);
*(pS + i + j*width) = *(pS + j + i*width);
*(pS + j + i*width) = temp;


You must enable all compiler warnings. They're very helpful and help you solve most of the above problem






share|improve this answer





















  • First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
    – Itay Zemah
    yesterday












  • I don't know. If you do a manual compilation then just use -Wall -Wextra Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
    – phuclv
    yesterday




















up vote
1
down vote













To solve this, you need to understand how arrays work in C.



Lets say you have a 3 * 3 matrix, that is declared like this:



int matrix[3][3];


While you imagine this to be a square like this:



+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+


For the computer it is a consecutive "line" in memory looking like this:



+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
^
|____matrix


And variable matrix holds the address of the first cell



So, if you want to access any cell without using the operator, you need to calculate the address of that cell.



Lets do this for the middle cell:



matrix[1][1];


This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:



*(matrix + (3 * 1) + 1);


What if we wanted the middle cell of the third row? Same thing, but add width of two rows:



*(matrix + (3 * 2) + 1);


To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:



*(matrix + (with * y) + x);





share|improve this answer








New contributor




Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238553%2frotate-matrix-without%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You're mixing up the pointer and the dereferenced value in the 3 lines



    In the first line temp = (int)(pS); you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i); which makes no sense.



    Then in the last line (int*)((pS + j) + i) = temp; doesn't work because you're a value to an address instead of a memory location



    Remember to use * to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b] is equivalent to *(a + b) so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j)) but didn't apply that to others



    Even then you're calculating the index incorrectly. *((int*)((pS + i) + j)) is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]



    So to do



    temp = pS[i + j*width];
    pS[i + j*width] = pS[j + i*width];
    pS[j + i*width] = temp;


    just change it to



    temp = *(pS + i + j*width);
    *(pS + i + j*width) = *(pS + j + i*width);
    *(pS + j + i*width) = temp;


    You must enable all compiler warnings. They're very helpful and help you solve most of the above problem






    share|improve this answer





















    • First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
      – Itay Zemah
      yesterday












    • I don't know. If you do a manual compilation then just use -Wall -Wextra Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
      – phuclv
      yesterday

















    up vote
    0
    down vote



    accepted










    You're mixing up the pointer and the dereferenced value in the 3 lines



    In the first line temp = (int)(pS); you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i); which makes no sense.



    Then in the last line (int*)((pS + j) + i) = temp; doesn't work because you're a value to an address instead of a memory location



    Remember to use * to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b] is equivalent to *(a + b) so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j)) but didn't apply that to others



    Even then you're calculating the index incorrectly. *((int*)((pS + i) + j)) is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]



    So to do



    temp = pS[i + j*width];
    pS[i + j*width] = pS[j + i*width];
    pS[j + i*width] = temp;


    just change it to



    temp = *(pS + i + j*width);
    *(pS + i + j*width) = *(pS + j + i*width);
    *(pS + j + i*width) = temp;


    You must enable all compiler warnings. They're very helpful and help you solve most of the above problem






    share|improve this answer





















    • First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
      – Itay Zemah
      yesterday












    • I don't know. If you do a manual compilation then just use -Wall -Wextra Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
      – phuclv
      yesterday















    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    You're mixing up the pointer and the dereferenced value in the 3 lines



    In the first line temp = (int)(pS); you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i); which makes no sense.



    Then in the last line (int*)((pS + j) + i) = temp; doesn't work because you're a value to an address instead of a memory location



    Remember to use * to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b] is equivalent to *(a + b) so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j)) but didn't apply that to others



    Even then you're calculating the index incorrectly. *((int*)((pS + i) + j)) is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]



    So to do



    temp = pS[i + j*width];
    pS[i + j*width] = pS[j + i*width];
    pS[j + i*width] = temp;


    just change it to



    temp = *(pS + i + j*width);
    *(pS + i + j*width) = *(pS + j + i*width);
    *(pS + j + i*width) = temp;


    You must enable all compiler warnings. They're very helpful and help you solve most of the above problem






    share|improve this answer












    You're mixing up the pointer and the dereferenced value in the 3 lines



    In the first line temp = (int)(pS); you're assigning the pointer value to temp instead of the value that the pointer points to. Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i); which makes no sense.



    Then in the last line (int*)((pS + j) + i) = temp; doesn't work because you're a value to an address instead of a memory location



    Remember to use * to dereference a pointer to get the variable, i.e. the memory location the pointer points to. In C a[b] is equivalent to *(a + b) so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j)) but didn't apply that to others



    Even then you're calculating the index incorrectly. *((int*)((pS + i) + j)) is just pS[i + j] which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]



    So to do



    temp = pS[i + j*width];
    pS[i + j*width] = pS[j + i*width];
    pS[j + i*width] = temp;


    just change it to



    temp = *(pS + i + j*width);
    *(pS + i + j*width) = *(pS + j + i*width);
    *(pS + j + i*width) = temp;


    You must enable all compiler warnings. They're very helpful and help you solve most of the above problem







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered yesterday









    phuclv

    14.1k846203




    14.1k846203












    • First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
      – Itay Zemah
      yesterday












    • I don't know. If you do a manual compilation then just use -Wall -Wextra Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
      – phuclv
      yesterday




















    • First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
      – Itay Zemah
      yesterday












    • I don't know. If you do a manual compilation then just use -Wall -Wextra Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
      – phuclv
      yesterday


















    First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
    – Itay Zemah
    yesterday






    First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
    – Itay Zemah
    yesterday














    I don't know. If you do a manual compilation then just use -Wall -Wextra Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
    – phuclv
    yesterday






    I don't know. If you do a manual compilation then just use -Wall -Wextra Enabling flags (Wall, pedantic) for C/C++ compilation within Eclipse
    – phuclv
    yesterday














    up vote
    1
    down vote













    To solve this, you need to understand how arrays work in C.



    Lets say you have a 3 * 3 matrix, that is declared like this:



    int matrix[3][3];


    While you imagine this to be a square like this:



    +---+---+---+
    | 0 | 1 | 2 |
    +---+---+---+
    | 3 | 4 | 5 |
    +---+---+---+
    | 6 | 7 | 8 |
    +---+---+---+


    For the computer it is a consecutive "line" in memory looking like this:



    +---+---+---+---+---+---+---+---+---+---+
    | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
    +---+---+---+---+---+---+---+---+---+---+
    ^
    |____matrix


    And variable matrix holds the address of the first cell



    So, if you want to access any cell without using the operator, you need to calculate the address of that cell.



    Lets do this for the middle cell:



    matrix[1][1];


    This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:



    *(matrix + (3 * 1) + 1);


    What if we wanted the middle cell of the third row? Same thing, but add width of two rows:



    *(matrix + (3 * 2) + 1);


    To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:



    *(matrix + (with * y) + x);





    share|improve this answer








    New contributor




    Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote













      To solve this, you need to understand how arrays work in C.



      Lets say you have a 3 * 3 matrix, that is declared like this:



      int matrix[3][3];


      While you imagine this to be a square like this:



      +---+---+---+
      | 0 | 1 | 2 |
      +---+---+---+
      | 3 | 4 | 5 |
      +---+---+---+
      | 6 | 7 | 8 |
      +---+---+---+


      For the computer it is a consecutive "line" in memory looking like this:



      +---+---+---+---+---+---+---+---+---+---+
      | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
      +---+---+---+---+---+---+---+---+---+---+
      ^
      |____matrix


      And variable matrix holds the address of the first cell



      So, if you want to access any cell without using the operator, you need to calculate the address of that cell.



      Lets do this for the middle cell:



      matrix[1][1];


      This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:



      *(matrix + (3 * 1) + 1);


      What if we wanted the middle cell of the third row? Same thing, but add width of two rows:



      *(matrix + (3 * 2) + 1);


      To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:



      *(matrix + (with * y) + x);





      share|improve this answer








      New contributor




      Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        1
        down vote










        up vote
        1
        down vote









        To solve this, you need to understand how arrays work in C.



        Lets say you have a 3 * 3 matrix, that is declared like this:



        int matrix[3][3];


        While you imagine this to be a square like this:



        +---+---+---+
        | 0 | 1 | 2 |
        +---+---+---+
        | 3 | 4 | 5 |
        +---+---+---+
        | 6 | 7 | 8 |
        +---+---+---+


        For the computer it is a consecutive "line" in memory looking like this:



        +---+---+---+---+---+---+---+---+---+---+
        | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
        +---+---+---+---+---+---+---+---+---+---+
        ^
        |____matrix


        And variable matrix holds the address of the first cell



        So, if you want to access any cell without using the operator, you need to calculate the address of that cell.



        Lets do this for the middle cell:



        matrix[1][1];


        This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:



        *(matrix + (3 * 1) + 1);


        What if we wanted the middle cell of the third row? Same thing, but add width of two rows:



        *(matrix + (3 * 2) + 1);


        To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:



        *(matrix + (with * y) + x);





        share|improve this answer








        New contributor




        Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        To solve this, you need to understand how arrays work in C.



        Lets say you have a 3 * 3 matrix, that is declared like this:



        int matrix[3][3];


        While you imagine this to be a square like this:



        +---+---+---+
        | 0 | 1 | 2 |
        +---+---+---+
        | 3 | 4 | 5 |
        +---+---+---+
        | 6 | 7 | 8 |
        +---+---+---+


        For the computer it is a consecutive "line" in memory looking like this:



        +---+---+---+---+---+---+---+---+---+---+
        | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
        +---+---+---+---+---+---+---+---+---+---+
        ^
        |____matrix


        And variable matrix holds the address of the first cell



        So, if you want to access any cell without using the operator, you need to calculate the address of that cell.



        Lets do this for the middle cell:



        matrix[1][1];


        This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:



        *(matrix + (3 * 1) + 1);


        What if we wanted the middle cell of the third row? Same thing, but add width of two rows:



        *(matrix + (3 * 2) + 1);


        To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:



        *(matrix + (with * y) + x);






        share|improve this answer








        New contributor




        Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered yesterday









        Lev M.

        1265




        1265




        New contributor




        Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Lev M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.













            Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.












            Itay Zemah is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238553%2frotate-matrix-without%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine