Rotate matrix without []










-2















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 '18 at 12:39











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

    – Itay Zemah
    Nov 10 '18 at 12:46















-2















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 '18 at 12:39











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

    – Itay Zemah
    Nov 10 '18 at 12:46













-2












-2








-2








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 '18 at 22:54









phuclv

14.5k852216




14.5k852216










asked Nov 10 '18 at 11:39









Itay ZemahItay Zemah

83




83












  • how is matrix rotation related to ?

    – phuclv
    Nov 10 '18 at 12:39











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

    – Itay Zemah
    Nov 10 '18 at 12:46

















  • how is matrix rotation related to ?

    – phuclv
    Nov 10 '18 at 12:39











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

    – Itay Zemah
    Nov 10 '18 at 12:46
















how is matrix rotation related to ?

– phuclv
Nov 10 '18 at 12:39





how is matrix rotation related to ?

– phuclv
Nov 10 '18 at 12:39













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

– Itay Zemah
Nov 10 '18 at 12:46





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

– Itay Zemah
Nov 10 '18 at 12:46












2 Answers
2






active

oldest

votes


















0














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 '18 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 '18 at 13:39



















1














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',
    autoActivateHeartbeat: false,
    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









    0














    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 '18 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 '18 at 13:39
















    0














    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 '18 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 '18 at 13:39














    0












    0








    0







    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 '18 at 13:04









    phuclvphuclv

    14.5k852216




    14.5k852216












    • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 13:39














    1














    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



























      1














      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

























        1












        1








        1







        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 '18 at 12:59









        Lev M.Lev M.

        618112




        618112



























            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.




            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

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)