1 / 2
Mar 2011

Hi
I'm currently working on Rotating Rings
https://www.spoj.pl/problems/ANARC07C/1
at first, I received a runtime error (I use Dev C++) when I declared an array of size 1000x1000, I solved the problem on my compiler by declaring the array globally.
The code works just fine on my end, I even tried it with many test cases I designed (up to 8x8), but when I submitted the solution I received a RE (SIGSEGV), which translates to "Invalid memory reference" (Wikipedia).
I suspect it's the same error I encountered originally on Dev C++, but I'm not sure.
Any suggestions about the cause of the error greatly appreciated.

PS: I could post the code if needed (and if it's not against the forum rules), I made sure it's easy to understand and supplied comments where necessary.

Here's the code, as I said, it works fine with the test cases I made, but it's kinda hard to make a test case with size 1000x1000,
anyway, every function is tested and the test codes can be found among the comments, thanks for your time

#include<iostream>
using namespace std;
int box[1000][1000];
/******Calculate Ring Number******/
int ringNo(int size, int r, int c)
  {
  if(size%2)
    {
     for(int i=1; i<=(size/2)+1; i++)
       if((r==i)||(c==i)||(r==size-i+1)||(c==size-i+1))
          return i;
    } 
  else
    for(int i=1; i<=size/2; i++)
       if((r==i)||(c==i)||(r==size-i+1)||(c==size-i+1))
          return i;               
  }
/*****Calculate Next Location in Ring*****/  
void nextLoc(int size, int &r, int &c)
  {
    int loc=ringNo(size,r,c);
    if(r==loc) 
      if(c!=size-loc+1) {c++; return;}
    if(c==size-loc+1) 
      if(r!=size-loc+1) {r++; return;} 
    if(r==size-loc+1)
      if(c!=loc)        {c--; return;}
    r--;      
  }
/*****Calculate Next Value in Ring*****/
int nextVal(int size, int crnt)
  {
   if(crnt==size*size) return --crnt;
   int r, c;
   if(crnt%size) c=crnt-(crnt/size)*size;
   else c=size;            
   if(crnt%size) r=crnt/size+1;
     else r=crnt/size;
   int loc=ringNo(size,r,c);
   if(r==loc) 
     if(c!=size-loc+1) return ++crnt;
   if(c==size-loc+1) 
     if(r!=size-loc+1) return crnt+size;
   if(r==size-loc+1)
      if(c!=loc)       return --crnt;
   return crnt-size;    
  }
/***## Main ##***/
int main()
{
int size,r,c,dummy; cin >> size; int tst=0; bool srt;
int len  ,  /*ring length*/
    start; /*starting value of ring*/
/*while(size) {cin >> r >> c; cout << ringNo(size,r,c) << endl; cin >> size;} //test for ringNo() */
/*while(size) {cin >> r >> c; nextLoc(size,r,c); cout << r << " " << c << endl; cin >> size;} //test for nextLoc() */
while (size)
  {
  for(int i=1; i<=size; i++)
     for(int j=1; j<=size; j++)
       cin >> box[i][j];
  cout << ++tst << ". ";
  dummy=(size/2)+1; 
  if(size%2) /*boxes with odd size have inner ring with size of 1 */
     {
      srt=box[dummy][dummy]==(size*size+1)/2;
      if(!srt){cout << "NO\n"; cin >> size; continue;}
     }
  for(int i=size/2; i>0; i--)
     {
      r=i; c=i; start=(i-1)*size+i; 
      len=4*(1+size-2*i); srt=false;     
      for(int k=0; k<len; k++){srt=box[r][c]==start; if(srt)break; nextLoc(size,r,c);} /*set r,c to location of starting value*/ 
      if(!srt) break;
      for(int j=1; j<size; j++) 
        {
         nextLoc(size,r,c); start=nextVal(size,start);
         srt=box[r][c]==start;
         if(!srt) break;
        }
      if(!srt) break;
     }
  if(!srt) cout << "NO\n";
  else cout << "YES\n";
  cin >> size;   
  }
return 0;
}
  • created

    Mar '11
  • last reply

    Mar '11
  • 1

    reply

  • 343

    views

  • 2

    users

  • 1

    link

Post the code. SIGSEGV has other causes and declaring a 1000x1000 area is not likely to do that. Acessing outside legal memory locations will. Check your array bounds.

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 13 6d

Want to read more? Browse other topics in C and C++ or view latest topics.