Help 101

All about problems in Volume I. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Postby dumbledore » Mon Nov 12, 2001 4:44 pm

Who can help me?
I got this when compiling:
Your program has died with signal 11 (SIGSEGV). Meaning:

Invalid memory reference

Before crash, it ran during 0.010 seconds.

and this is my source in C:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int table[200][200];
int a,b, posa, posb, cola, colb, n, soblock[200];


void moveonto(int a,int b);
void moveover(int a,int b);
void pileonto(int a,int b);
void pileover(int a,int b);
int search(int x, int a[], int n);

/* main program */
int main()
{
scanf("%d",&n);
int i, j;
for (i=0;i<n;i++)
{
table[i][0]=i;
soblock[i]=1;
}
fflush(stdin);
char s[20], *s1, *s2, *s3, *s4;
while (gets(s)!=NULL)
{
if (strcmp(s,"quit")==0) break;
s1=strtok(s," ");
a=atoi(strtok(NULL," "));
s2=strtok(NULL," ");
b=atoi(strtok(NULL," "));
if ((a>=n)||(b>=n)||(a==b)) continue;
for (cola=0;cola<n;cola++)
{
posa=search(a,table[cola],soblock[cola]);
if (posa!=-1) break;
}
for (colb=0;colb<n;colb++)
{
posb=search(b,table[colb],soblock[colb]);
if (posb!=-1) break;
}
if (cola==colb) continue;
if (strcmp(s1,"move")==0)
{
if (strcmp(s2,"onto")==0) moveonto(a,b);
else if (strcmp(s2,"over")==0) moveover(a,b);
}
else if (strcmp(s1,"pile")==0)
{
if (strcmp(s2,"onto")==0) pileonto(a,b);
else if (strcmp(s2,"over")==0) pileover(a,b);
}
}
for (i=0;i<n;i++)
{
printf("%d:",i);
for (j=0;j<soblock[i];j++) printf(" %d",table[i][j]);
printf("n");
}
return 0;
}
/* end of main program */

/* function */
/* search in an array */
int search(int x, int a[], int n)
{
int i;
for (i=0;i<n;i++) if (a[i]==x) return i;
return -1;
}

/* delete an element in array at pos */
void del(int x, int a[], int &n)
{
int i;
for (i=search(x,a,n);i<n-1;i++) a[i]=a[i+1];
n--;
}

/* insert a new element to an array at pos */
void insert(int x, int a[], int &n, int pos)
{
int i;
for (i=n;i>pos;i--) a[i]=a[i-1];
a[pos]=x;
n++;
}

/* the move onto function */
void moveonto(int a, int b)
{
del(a,table[cola],soblock[cola]);
insert(a,table[colb],soblock[colb],posb+1);

}

/* the move over function */
void moveover(int a, int b)
{
del(a,table[cola],soblock[cola]);
insert(a,table[colb],soblock[colb],soblock[colb]);
}

/* the pile onto function */
void pileonto(int a, int b)
{
int temp[200], ntemp, i;
for (i=posa;i<soblock[cola];i++) temp[i-posa]=table[cola][i];
ntemp=soblock[cola]-posa;
soblock[cola]=posa;
for (i=0;i<ntemp;i++)
insert(temp[i],table[colb],soblock[colb],i+posb+1);

}

/* the pile over function */
void pileover(int a, int b)
{
int temp[200], ntemp, i;
for (i=posa;i<soblock[cola];i++) temp[i-posa]=table[cola][i];
ntemp=soblock[cola]-posa;
soblock[cola]=posa;
for (i=0;i<ntemp;i++)
insert(temp[i],table[colb],soblock[colb],soblock[colb]);
}
dumbledore
New poster
 
Posts: 2
Joined: Mon Nov 12, 2001 2:00 am
Location: Vietnam

Postby fpnc » Sat Nov 24, 2001 10:18 pm

This usually means that a pointer is out of range, or a matrix index is out of range. Think about the limits of the problem, the range of the input... This kind of thinks.
fpnc
System administrator
 
Posts: 201
Joined: Sun Oct 07, 2001 2:00 am
Location: Valladolid, Spain

Postby ahanys » Mon Nov 26, 2001 12:56 pm

See the problem set now it is updated. Num of buildings is now 5000 i think.
ahanys
New poster
 
Posts: 24
Joined: Mon Nov 19, 2001 2:00 am
Location: N/A

Postby dumbledore » Mon Nov 26, 2001 6:37 pm

I can't understand, the problem say:
The input begins with an integer n on a line by itself representing the number of blocks in the block world. You may assume that 0 < n < 25.

So I can define table[25][25], but even when I use table[200][200], it's still out of range.
dumbledore
New poster
 
Posts: 2
Joined: Mon Nov 12, 2001 2:00 am
Location: Vietnam

Postby junjieliang » Tue Nov 27, 2001 5:53 am

No, an array[25][25] is big enough. I suppose the person who posted the previous message about 5000 buildings was referring to problem 105.
junjieliang
Experienced poster
 
Posts: 169
Joined: Wed Oct 31, 2001 2:00 am
Location: Singapore


Return to Volume I

Who is online

Users browsing this forum: No registered users and 1 guest

cron