Clockwise Matrix

2 views
Skip to first unread message

ashish

unread,
Apr 11, 2007, 2:48:49 PM4/11/07
to Programming-Challenges

In a n*n matrix print all the elements clockwise.
for example..
if a matrix is 4*4 as follow

11 12 13 14
15 16 17 18
19 20 21 22
23 24 25 26
then output should be...
11 12 13 14 18 22 26 25 24 23 19 15 16 17 21 20

This program can be done by difffernt ways matter is of complexity..
so do post ur ideas.....

Amaj Nash (Suhas)

unread,
Apr 13, 2007, 6:26:28 AM4/13/07
to Programming-Challenges
int main()
{
int n,lrb,dub;
int i,j,k,sqn;
int x,y,dir,turnx,turny,matrix[10][10];


x=y=0;
dir=RIGHT;
printf ("\nEnter the order:");
scanf ("%d",&n);
printf ("\nEnter the matrix\n");
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf ("%d",&matrix[i][j]);
lrb=n;
dub=n-1;
turnx=0;
turny=1;
sqn=n*n;
for (i=0;i<sqn;i++)
{
printf (" %d",matrix[x][y]);
switch(dir)
{
case 0:

y++;
turny++;
if (turny==lrb)
{
dir=3;
lrb--;
turny=0;
}
break;

case 1:
y--;
turny++;

if (turny==lrb)
{
dir=2;
lrb--;
turny=0;
}
break;

case 2:
x--;
turnx++;

if (turnx==dub)
{
dir=0;
dub--;
turnx=0;
}
break;

case 3:
x++;
turnx++;

if(turnx==dub)
{
dir=1;
dub--;
turnx=0;
}
break;
}
}

return 0;

ajinkya kale

unread,
Apr 13, 2007, 10:21:25 AM4/13/07
to programming...@googlegroups.com
Hey please write our logic first (with may be a pseudo-code and an example may be) before posting the actual code on the group so that everyone will understand the logic first followed by the actual solution code.

sumedh sakdeo

unread,
Apr 15, 2007, 8:39:28 AM4/15/07
to programming...@googlegroups.com
Clockwise Matrix:
I go and access elements of an array in a clockwise fashion. Works for any order array.. I use for loop to move from left->right and right->left and top->down and bottom->top and keep on changing values of variable. and use a dynamic array.
If you dont understand my logic you can meet me in person or use the wonderful debug option which Compiler provides....


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
    int n;
    int *arr;
    clrscr();
    cout<<"Enter the order of matrix";
    cin>>n;
    arr=new int[n*n];
    int c=11;
    for(int p=0;p<n;p++)
    {
        for(int r=0;r<n;r++)
        {
            *(arr+n*p+r)=c;
            printf("%d  ",c);
            c++;
        }
        printf("\n");
    }
    printf("\n\n\n");
    int j=0,cnt=1;
    for(int i=0;i<n;i++)
    {
          for(j;j<n-i;j++)
          {
                
                 printf("%d  ",*(arr+n*i+j));
          }
          for(int k=i+1;k<=n-cnt;k++)
          {
                
                 printf("%d  ",*(arr+n*k+j-1));
          }
          j--;
          for(int s=j;s>=cnt;s--)
          {
                
                 printf("%d  ",*(arr+n*j+s-1));
          }
          j--;
          for(j;j>=cnt;j--)
          {
                
                    printf("%d  ",*(arr+n*j+cnt-1));
          }
          cnt++;
          j++;
    }
    getch();
}
Cheers,
Sumedh.






sumedh sakdeo

unread,
Apr 15, 2007, 8:46:08 AM4/15/07
to programming...@googlegroups.com
Suhas your program is not working on my machine... means its not showing the correct output..May be you missed out some lines to be added to your code...  So wud u pl check it again and post it again and explain the logic you have used.
Cheers,
Sumedh

On 4/13/07, Amaj Nash (Suhas) <amaj...@gmail.com> wrote:

Ajinkya

unread,
Apr 15, 2007, 2:46:59 PM4/15/07
to Programming-Challenges
Firstly the program to print the n*n matrix was not the problem
statement which ashish posted. In that one you are given any random
matrix(pls do read it) Pls post your problem before posting any
solution.
Secondly you are storing the whole matrix elements(we never said co-
ordinates). What if the input is 50,000 space complexity
50,000*50,000*size of int(if i am not wrong)
Thirdly this group is not a college group(so pls dont give the option
to explain the algo in person)
Finally, your code is working for only 3 as the input.....its not
working for 1 either and neither for inputs after 5(ie 5,7,9,...)

Do tell me if i am wrong somewhere or missed a trick..

Ciao,
Ajinkya.

> - Hide quoted text -
>
> - Show quoted text -

Reply all
Reply to author
Forward
0 new messages