Thread: Why the following code not working?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    6

    Post Why the following code not working?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void magicsq(int **ar,int n);
    int main()
    {
        int n;
        printf("enter size");
        scanf("%d",&n);
        int a[n][n];
        int i=0,j=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                a[i][j]=0;
            }
        }
        magicsq(a,n);
    
    
        return 0;
    }
    void magicsq(int **ar,int n)
    {
        printf(ar[1][1],"\n");
        int i=0,j=0,k=1,previ,prevj;
       while(k<=(n*n))
       {
           if(k==1)
           {
           ar[0][n/2]=k;
           printf("dd");
           }
           else
        {
            printf("\ndd");
          previ=i;
          prevj=j;
          if((i-1)>=0 && (j-1)>=0)
          {
              i=i-1;
              j=j-1;
          }
          else if((i-1)==-1 && (j-1)>=0)
          {
              i=n-1;
              j=j-1;
          }
          else if((i-1)==-1 && (j-1)==-1)
          {
              i=n-1;
              j=n-1;
          }
          else if((i-1)>=0 && (j-1)==-1)
          {
              i=i-1;
              j=n-1;
          }
        if(!ar[i][j])
        {
            ar[i][j]=k;
            printf("\n",ar[i][j]);
        }
        else
        {
            if((previ+1)<n)
            {
                i=previ+1;
                ar[i][j]=k;
                printf("\n",ar[i][j]);
            }
            else
            {
                i=0;
                ar[i][j]=k;
                printf("\n",ar[i][j]);
            }
        }}
        k++;
        }
         for(i=0;i<n;i++)
        {
            printf("\n");
            for(j=0;j<n;j++)
            {
                printf("\t",ar[i][j]);
    
    
            }
        }//printf("dd");
        }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Without more information I will guess user error.

    Edit: This phrase "not working" is meaninglessly; please post how it is NOT working!

    Example: The code gives the output of "XYZ"; but, I want it to give "ABC".

    Tim S.
    Last edited by stahta01; 01-22-2014 at 04:02 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    6
    On running this code it gives error:Program not responding.
    I guess there is some problem with the passing of array to function.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI:

    This line is NOT safe on old C Compilers.

    I think variable length arrays (VLA) was added in C99; but, I am NOT sure.

    Code:
    int a[n][n];
    Edit: This line is NOT safe on any Compiler!!!!
    Code:
    printf(ar[1][1],"\n");

    Tim S.
    Last edited by stahta01; 01-22-2014 at 04:08 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Did you compile this program with warnings turned on? For gcc you should use -Wall -Wextra -Werror and fix all the problems first.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by stahta01 View Post
    Edit: This line is NOT safe on any Compiler!!!!
    Code:
    printf(ar[1][1],"\n");

    I suggest reading my edit to last post and turning on Compiler Warnings.

    Tim S.
    Last edited by stahta01; 01-22-2014 at 04:15 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    For passing the array I would suggest this prototype instead:

    Code:
    void magicsq(int n, int ar[n][n]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone tell me why this code isn't working please?
    By psynt555 in forum C Programming
    Replies: 5
    Last Post: 04-24-2012, 05:14 PM
  2. Why is this code not working
    By valthyx in forum C Programming
    Replies: 15
    Last Post: 08-17-2009, 06:04 PM
  3. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  4. code not working as it should..
    By transgalactic2 in forum C Programming
    Replies: 21
    Last Post: 12-05-2008, 02:10 PM
  5. Why is the above code working???
    By chottachatri in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2008, 07:29 AM

Tags for this Thread