Thread: Can anyone help me out with this program of spiral matrix?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Can anyone help me out with this program of spiral matrix?

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        int arr[100][100];
        int i,j,n,c;
        printf("Enter size of spiral matrix ");
        scanf("%d",&n);
        c=1;
        for(i=0;i<n;i++)
        {
            arr[0][i]=c++;
        }
        for(i=1;i<n;i++)
        {
            arr[i][n-1]=c++;
        }
        for(i=(n-1);i>=0;i--)
        {
            arr[n-1][i]=c++;
        }
        for(i=(n-1);i>=1;i--)
        {
            arr[i][0]=c++;
        }
        for(i=1;i<=(n-2);i++)
        {
            arr[1][i]=c++;
        }
        for(i=2;i<=3;i++)
        {
            arr[i][3]=c++;
        }
        for(i=2;i>=1;i--)
        {
            arr[n-2][i]=c++;
        }
        arr[2][2]=c;
        
        printf("Printing the spiral matrix \n");
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                printf("%d\t",arr[i][j]);
            }
            printf("\n");
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What's wrong with the program? Please be specific.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    The program is giving a wrong output.And also this code does not work for matrix of all orders.I just want my code to corrected.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by daggerhunt View Post
    The program is giving a wrong output.
    In what sense? What did you expect, and what happened? Be explicit. Use details.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Give us the simplest example you have of wrong output, and show us what the correct output is.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    For array size 3, the output I got is this?

    1 2 3
    10 15 14
    9 7 16

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What is the correct output?

    EDIT: I think I know (from google):
    Code:
    1 2 3 
    8 9 4
    7 6 5
    Is that right?
    Last edited by oogabooga; 04-05-2012 at 10:07 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What if you had two functions. Call one "down", and the other "up". You call them one after the other, pass them a parameter of 1 for rows, and 0 for columns.

    So you start from 0,0 in the array. You go up(0), up(1), then down(0), and down(1), as you spiral. When you reach row 0 col 0 again, you increase the row variable by 1, and increase the column variable by 1, and that becomes the new starting square index (1 row, and 1 column). An outer while loop see's that this is all repeated until the number of squares visited in the array, equals the size of the array (length * width) minus one.
    Last edited by Adak; 04-06-2012 at 12:46 AM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In that case, you can't do this with a fixed number of non-nested loops, one for each lot of consecutive numbers in a row. If it were 1000x1000 then you'd need far more loops than for 3x3.

    Delete the loops that try and fill the matrix and come up with a different approach.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    OK ..I am going to rethink my logic. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with a program for a LED matrix.
    By Kakefarmer in forum C Programming
    Replies: 15
    Last Post: 01-13-2012, 04:34 PM
  2. Spiral Number Matrices
    By kakaboo in forum C Programming
    Replies: 8
    Last Post: 09-14-2010, 11:06 PM
  3. Matrix c program help!!!
    By kclew in forum C Programming
    Replies: 21
    Last Post: 02-25-2009, 04:46 AM
  4. matrix program
    By J in forum C Programming
    Replies: 4
    Last Post: 04-21-2002, 08:07 PM
  5. drawing a spiral
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-11-2001, 09:03 PM