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























  • how is matrix rotation related to ?
    – phuclv
    Nov 10 at 12:39










  • I can't swap elements by using: a[i][j] = temp ,etc.
    – Itay Zemah
    Nov 10 at 12:46














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























  • how is matrix rotation related to ?
    – phuclv
    Nov 10 at 12:39










  • I can't swap elements by using: a[i][j] = temp ,etc.
    – Itay Zemah
    Nov 10 at 12:46












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















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















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 22:54









phuclv

14.3k850209




14.3k850209










asked Nov 10 at 11:39









Itay Zemah

83




83











  • how is matrix rotation related to ?
    – phuclv
    Nov 10 at 12:39










  • I can't swap elements by using: a[i][j] = temp ,etc.
    – Itay Zemah
    Nov 10 at 12:46
















  • how is matrix rotation related to ?
    – phuclv
    Nov 10 at 12:39










  • I can't swap elements by using: a[i][j] = temp ,etc.
    – Itay Zemah
    Nov 10 at 12:46















how is matrix rotation related to ?
– phuclv
Nov 10 at 12:39




how is matrix rotation related to ?
– phuclv
Nov 10 at 12:39












I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
Nov 10 at 12:46




I can't swap elements by using: a[i][j] = temp ,etc.
– Itay Zemah
Nov 10 at 12:46












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
    Nov 10 at 13:34











  • 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
    Nov 10 at 13:39


















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




















    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
    );



    );













    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















    Required, but never shown

























    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
      Nov 10 at 13:34











    • 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
      Nov 10 at 13:39















    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
      Nov 10 at 13:34











    • 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
      Nov 10 at 13:39













    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 Nov 10 at 13:04









    phuclv

    14.3k850209




    14.3k850209











    • First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
      – Itay Zemah
      Nov 10 at 13:34











    • 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
      Nov 10 at 13:39

















    • First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
      – Itay Zemah
      Nov 10 at 13:34











    • 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
      Nov 10 at 13:39
















    First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
    – Itay Zemah
    Nov 10 at 13:34





    First, thank for the really helpful explanation! Second, I work in Ubunto enviroment, in eclips. How can I enable the warning?
    – Itay Zemah
    Nov 10 at 13:34













    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
    Nov 10 at 13:39





    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
    Nov 10 at 13:39













    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
























      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






















        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












        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












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 12:59









        Lev M.

        582111




        582111



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            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















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo