Thread: exercice help

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

    exercice help

    hi all , i need help in solving this exercice which asking to make a rotation to a 2d array without using any other array , so here's an example :

    1 2 3 4
    5 6 7 8
    9 10 11 12
    13 14 15 16

    i want to add 90 degree so :

    13 9 5 1
    14 10 6 2
    15 11 7 3
    16 12 8 4

    if you know how to do it plz help ,and thnx .

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Compare this
    Code:
    for ( r = 0 ; r < 4 ; r++ )
      for ( c = 0 ; c < 4 ; c++ )
        printf("%d ", mat[r][c] );
      printf("\n");
    With this
    Code:
    for ( r = 3 ; r >= 0 ; r-- )
      for ( c = 0 ; c < 4 ; c++ )
        printf("%d ", mat[r][c] );
      printf("\n");
    Can you figure it out now, now that you know loops can go in either direction?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    2
    no i didnt , case u need to permute between array , nt just show it on the screen , i find this code but i didnt understand it but it work , its full with fuction and i still didnt study them , so can u explain it to me and give me a simple prog and thnx .

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void displayMatrix(unsigned int const *p, unsigned int row, unsigned int col);
    void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col);
     
    int main()
    {
        // declarations
        unsigned int image[][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
        unsigned int *pSource;
        unsigned int *pDestination;
        unsigned int m, n;
     
        // setting initial values and memory allocation
        m = 3, n = 4, pSource = (unsigned int *)image;
        pDestination = (unsigned int *)malloc(sizeof(int)*m*n);
     
        // process each buffer
        displayMatrix(pSource, m, n);
     
        rotate(pSource, pDestination, m, n);
     
        displayMatrix(pDestination, n, m);
     
        free(pDestination);
     
        getchar();
        return 0;
    }
     
    void displayMatrix(unsigned int const *p, unsigned int r, unsigned int c)
    {
        unsigned int row, col;
        printf("\n\n");
     
        for(row = 0; row < r; row++)
        {
            for(col = 0; col < c; col++)
            {
                printf("%d\t", *(p + row * c + col));
            }
            printf("\n");
        }
     
        printf("\n\n");
    }
     
    void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col)
    {
        unsigned int r, c;
        for(r = 0; r < row; r++)
        {
            for(c = 0; c < col; c++)
            {
                *(pD + c * row + (row - r - 1)) = *(pS + r * col + c);
            }
        }
    }
    Last edited by medyas; 12-07-2014 at 07:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exercice - Calculate the fee
    By khelkely in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2013, 06:04 AM
  2. need Exercice on Functions in C
    By Dr.Vip in forum C Programming
    Replies: 8
    Last Post: 01-03-2012, 04:37 AM
  3. need help for a programming exercice
    By thibault0613 in forum C# Programming
    Replies: 4
    Last Post: 11-12-2011, 07:38 PM
  4. help for my programming exercice
    By thibault0613 in forum C# Programming
    Replies: 5
    Last Post: 11-09-2011, 01:52 PM