Hi! I have this kind of project and im really needing help for it. The compiling and print have to be a spiral matrix. it must start in the center and then move to the right and then up and then to the left, making a spiral. I have to introduce an initial and final value. the initial value must be in the center and the final value would be the end of the spiral.
Like this
initial Value = 4
final Value = 18

      4   5   6
  14  15  16  7
  13  18  17  8
  12  11  10  9

Code:

#include < stdio.h >
#include < conio.h > 

void main()
{
    int m[20][20],i,j;
    int lc,hc,lr,hr,r,c,cnt;    
    clrscr();
    printf("\nEnter r & c :");
    scanf("%d %d",&r,&c);
    cnt = 1;
    lr = 0; lc = 0;
    hr = r - 1;
    hc = c - 1;
    while ( lr <=hr && lc <= hc )
    {
        i = lr;
        for(j=lc;j <= hc;j++)
        {
            m[i][j] = cnt++;    
            j = hc;
            for(i=lr+1;i<=hr;i++)
            {
                m[i][j] = cnt++;    
                if( lr != hr )
                {
                    i = hr;
                    for(j=hc-1;j>=lc;j--)
                    m[i][j] = cnt++;
                }   
                if ( lc != hc )
                {
                    j = lc;
                    for(i=hr-1;i>lr;i--)
                    m[i][j] = cnt++;
                }
            }
        }
        lr++;lc++;
        hr--;hc--;
    }//while    
    printf("\nSpirally filled matrix is\n");
    for(i=0;i < r;i++)
    {
        for(j=0;j < c;j++)
        {
            printf("%4d",m[i][j]);
            printf("\n");
        }       
    }
} // main

I have this program but isnt runnig well. Please please help

Recommended Answers

All 3 Replies

Member Avatar for iamthwee

Example makes little sense.

Your example does not match what you said should be the pattern.

it must start in the center and then move to the right and then up and then to the left, making a spiral.

Yet your example starts at the top and moves right and down, as in:

      4   5   6
  14  15  16  7
  13  18  17  8
  12  11  10  9

ok now i figured that out. youre right.
it would be like this:
initial number = 4
final number = 18

      18  17  16
  8   7   6   15
  9   4   5   14
  10  11  12  13
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.