0
#include<stdio.h>
int l;
int check(int m[][l],int a,int r,int c,int e)
{
    int t,i,j;
    for(i=0;i<c;i++)
    {
        if(m[a][i]==e)
            return 1;
    }
    for(i=0;i<c;i++)
    {
        for(j=0;j<r;j++)
        {
            if(a!=j)
            {
                if(m[a][i]==m[j][i]&&m[a][i]!=-1)
                {
                    m[a][i]=-1;
                    return check(m,j,r,c,e);
                }
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    int i,j;
    scanf("%d",&t);
    for(t;t>0;t--)
    {
        l=0;
        int n,e,a,b,x,y;
        scanf("%d%d%d%d",&n,&e,&a,&b);
        int m[e][n];
        l=n;
        for(i=0;i<e;i++)
        {
            for(j=0;j<n;j++)
            {
                m[i][j]=-1;
            }
        }
        for(i=0;i<e;i++)
        {
            scanf("%d%d",&x,&y);
            for(j=0;j<((n-y)/x)+1;j++)
            {
                m[i][y+(j*x)]=y+(j*x);
            }
        }
        int v,g=0;
        for(i=0;i<e;i++)
        {
            for(j=0;j<n;j++)
            {
                if(m[i][j]==a)
                {
                    v=check(m,i,e,n,b);
                    g++;
                    break;
                }
            }
        }
        if(v==1)
        {
            printf("It is possible to move the furniture.\n");
        }
        else if(v==0||g==0)
            printf("The furniture cannot be moved.\n");
    }
    return 0;
}

"I'm getting the correct answer for value of n<=50000, when i give more values i'm getting runtime error" "i have written a code for "http://www.spoj.pl/problems/SCRAPER/".. when i run it in ideone, i got the answers as "runtime error" for more 'n' values

yuvanesh
  • 1,093
  • 2
  • 9
  • 19

3 Answers3

1

You are trying to allocate more memory than it's available on the stack which causes stack overflow. Usually it is much better to allocate huge arrays like that dynamically by using malloc, calloc or realloc.

Check this question: C: Array initialization segfaults depending on size and call to printf()

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • Just a side-note. The OP is using VLAs and `gcc` stores them on the stack. But there is no compulsion on part of the compiler to do this. Other compilers might wish to store VLAs elsewhere ( heap etc ) – Pavan Manjunath Jun 28 '12 at 07:50
  • thanks a lot. And can you one tell me how to initialize a 2D array dynamically using malloc,calloc or realloc? – yuvanesh Jun 28 '12 at 08:41
  • @user1487765: Allocate an array of pointers first (first dimension) and then allocate an array representing each row and assign its address to each of those pointers (second dimension). – LihO Jun 28 '12 at 08:52
0

Your array may be stored onto the stack, and the stack is usually smaller than the heap. Try to allocate your array dynamically (malloc, calloc, free).

md5
  • 23,373
  • 3
  • 44
  • 93
  • thanks a lot. And can any one tell me how to initialize a 2D array dynamically using malloc,calloc or realloc – yuvanesh Jun 28 '12 at 08:40
0

When you write "int m[e][n]", the real space of the array is 1! The space of array is defiend when compiled, when 'e' 'n' are both 0. You have to use macro as the array's length, such as m[1000][1000]. And you need to make sure that e<1000.

Mr.go
  • 1