"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Friday, January 29, 2010

Creating a Spiral Matrix

eg.    Input : 3
        Output:      1   2   3
                           8   9   4
                           7   6   5



#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include(<stdio.h>

void main()
{
    clrscr();
    int user_no = 1;
    int sa[100][100];

    cout<< "\nEnter a no.";
    cin>> user_no;

    int sqr = user_no*user_no;

    int r = 1,c = user_no;

    int n = 1;
    do{
    for(int b=r; b< =c; b++)
    {sa[r][b]=n;
    n++;
    }

    for(b=r+1; b< =c; b++)
    {
    sa[b][c] = n;
    n++;
    }

   
    n--;

    for(b=c; b > =r;b--)
    {sa[c][b]=n;
    n++;
    }

    for(b=c-1; b > =r+1;b--)
    {sa[b][r] = n;
    n++;
    }
    r++;
    c--;
    }while(n< = sqr);

    if(user_no%2 != 0)
    {
    int mid = ((user_no/2) + 1);
    sa[mid][mid] = sqr;
    }

    //display
    for(int a = 1; a< =user_no; a++)
    {
    for(int b = 1; b< =user_no; b++)
    {
    cout<< sa[a][b]<<"\t";
    }
        cout<< endl << endl;

    }
  getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.