Hey guys,I'm new,but I study C programming language and I have this awfull problem.
I have make a matrix A[n][n],in which n is an odd positive number(of course),and the matrix must "unwrap" itself with the number 1 in the middle then 2 to the top,then clockwise 3,4,5,..etc,until it wraps the numbers 1 and finishes with the n*n number at the upper left corner...
I've seen the algorithm for the spiral matrix which wraps itself from the outside in,mine is the absolute opposite...I need help fast on this one,if anyone has suggestions I would be very thankful!
Only C,haven't started C++ yet.
=Landoro= matrix1

Recommended Answers

All 5 Replies

Assuming a square NxNmatrix where N is an odd number, it looks like:
Find the center (x,y) where x and y equal N/2 rounded down. Or N>>1;
Go Up One.
Go Right One.
Go Down Two.
Go Left Two.
Go Up Three.
Go Right Three.
...
Happy coding.

I made it and I would like to share,it was a lot of fun,the logic goes by doing upper and lower rows ,as well as right and left columns simultaniously.check it out:

#include<stdio.h>
void fsb(int b[],int n);
int N(void);
int main(void){
    int n,mm;
    printf("==The spiral matrix==\n");
    do{
        printf("Enter number of entries:\n");
        scanf("%d",&mm);
    }while(mm<=0);
    do{
       n=N();
       int b[n*n];
       fsb(b,n);
       int x=1,y=0,c=1,cc=0;
       int p,brm=0;
       int bb=1,aa=2;
       int a[n][n];
       int m=n;
       int i,j,k,h,f=1;
        a[n/2][n/2]=b[0];
       for(p=0;p<n-1;){
       i=(n/2)-c;
       j=(n/2)-y;
       k=(n/2)+x;
       h=(n/2)+cc;
              for(;brm<n-(n-aa);){
                            a[i][j]=b[bb];
                            a[k][h]=b[bb+4*f];
                            j++;
                            h--;
                            bb++;
                            brm++;

              }
       p++;
        j--;
        i++;
        k--;
        h++;
        brm=0; 
        for(;brm<n-(n-aa);){
                            a[i][j]=b[bb];
                            a[k][h]=b[bb+4*f];
                            i++;
                            k--;
                            bb++;
                            brm++;

              }
              p++;
              bb=bb+4*f;
              f++;
              brm=0;
        aa+=2;x++;cc++;c++;y++;
       } 

       for(x=0;x<n;x++){
           for(y=1;y<=n;y++)
                       printf("%3d ",a[x][y-1]);
                            printf("\n");
        }            

      printf("Entries left:%d\n",--mm);
      }while(mm);
      system("pause");
}
int N(void){
    int n;
    do{
        printf("Enter odd number:\n");
        scanf("%d",&n);
    }while(n%2==0);
    return n;
}
void fsb(int b[],int n){
     int i,j;
     for(i=0;i<n*n;i++){
                        b[i]=i+1;
     }
}

Concrats! Now that you have logic right, why not practise some commenting and/or changing the variable names descriptive?

Maybe also you could find little better indention standard as I find for example indention of the for loop lines 22-56 kind of "interesting" ;)

any one can help me for the same problem but n can be even or odd and also without using arrays?

It's best to state your question in a new thread for two reasons:

1) it's not the same as the original problem, and

2) it may not get the attention it deserves, here.

You can't use an array to hold the numbers? What CAN you use? No sense speculating about it, when you must have been told what to use, or at least given a hint.

I'll find you in your new thread - or here, as you prefer. Show or tell us specifically just WHAT you can use. And do you have any attempts in code to show us? It's difficult to know what to advise, without a concrete "something" in code, from you.

So give it a try, and post back. You might try working it through by paper and pen, and see what kinds of patterns you see in your own solutions to it, apart from the computer.
That could serve as the backbone of the programs logic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.