Page 5 of 11

482 Run Time Error (?)

Posted: Mon Jan 23, 2006 7:47 am
by Wei-Ming Chen
Why runtime error......

#include <stdio.h>
char y[1000][1000],z;
int main()
{
int x[1000],b,c,d,e,f;
scanf("%d",&b);
for(c=0;c<b;c++)
{
e=1;
while(scanf("%d%c",&x[e],&z)==2)
{
if(z=='\n')
{
break;
}
e++;
}
for(d=1;d<=e;d++)
{
f=x[d];
scanf("%s",y[f]);
}
for(d=1;d<=e;d++)
{
printf("%s\n",y[d]);
}
if(c!=b-1)
{
printf("\n");
}
}
return 0;
}

Posted: Tue Apr 11, 2006 4:27 am
by Roby
Anyone help me please... I also get Runtime Error without knowing where my mistake is... I've changed the array size into 100000 but still getting Runtime Error... here's my code:

Code: Select all

AC already
Please help me... :cry:

Posted: Tue Apr 11, 2006 5:18 am
by chunyi81
This part of your code:

Code: Select all

       // get the number 
       while ( dataIndex[i] != ' ' && dataIndex[i] != '\n' && 
               dataIndex[i] != '\0' ) 
       { 
          num *= 10; 
          num += dataIndex[i] - 48; 
          i++; 
       } 
What if num += dataIndex - 48 >= 10? You never handle the carry.

Posted: Tue Apr 11, 2006 6:45 am
by Roby
I'm confused with that part... I mean... I made that part to convert the index input from char to integer. So, I don't understand what you mean... can you give me the example? thanx :)

Posted: Wed Apr 12, 2006 10:38 am
by chunyi81
I am so sorry :oops:

Yes I can see that part of your code is changing the index form char to integer. That part should be ok, my mistake.

Can someone else help please?

Posted: Thu Apr 13, 2006 3:24 am
by Roby
Anyone, help me please... :cry:

Posted: Thu Apr 13, 2006 5:21 am
by Darko
I don't know - I used Java's StringTokenizer which, by default, considers all white spaces - try checking for tabs, carriage returns, that sort of thing.

I assumed lines to be at most 50000 characters long. I didn't do anything special, just read it and sorted (which was weird, usually I have problems with I/O with Java).

Maybe it's gets(), sometimes it behaves in misterious ways?

Darko

Posted: Mon Apr 17, 2006 6:56 pm
by yiuyuho
you had SIZE=100, may be you should try a bigger value (like 512)

Besides that I also wonder if scanf("\n%s\n",fp[index-1]); is the best way to scan the stuff, may be just scanf("%s",fp[index-1]);. Not too sure though.

Posted: Tue Apr 18, 2006 9:32 am
by Roby
I've changed the code like you said above, and there were no difference... My first approach just changed the SIZE value become 512 and I got RE and the other approach I changed the SIZE value and changed the scanf and I got WA...

OMG, how I should change my code... it seems perfect in my computer but why judge always gave me WA or RE... :cry:

Posted: Tue Apr 18, 2006 6:00 pm
by yiuyuho
ok, try to use gets only for the entire problem, and use strtok to tokenize your string. You will need a buffer of size 10000.

Posted: Wed Apr 19, 2006 10:09 am
by Roby

Code: Select all

AC already

Posted: Wed Apr 19, 2006 3:05 pm
by mamun
for ( i = 0, j = 0; dataIndex != '\n' && dataIndex != '\0'; i++ )

You are incrementing i in the loop block. So don't do it here.

Posted: Wed Apr 19, 2006 5:18 pm
by yiuyuho
I really didn't want to have to post AC code here, appearently you're too frustrated to think...

The code is really base on yours...When I said use strtok, I implicity says that SIZE = 100 is not enough..

Code: Select all

Cut after he got it

Posted: Wed Apr 19, 2006 5:21 pm
by yiuyuho
mamun wrote:
for ( i = 0, j = 0; dataIndex != '\n' && dataIndex != '\0'; i++ )

You are incrementing i in the loop block. So don't do it here.


I don't think that's an issue, because the pre-condition for i to enter the outer-loop is that i is pointing at a non space character (if not the while will make it so), the post-condition for leaving the inner loop is that i is pointing at a space, so the i++ will get rig of the space.

By the way, I'll take off the above code in 2 days.

Posted: Wed Apr 19, 2006 11:12 pm
by mamun
yiuyuho wrote:
mamun wrote:
for ( i = 0, j = 0; dataIndex != '\n' && dataIndex != '\0'; i++ )

You are incrementing i in the loop block. So don't do it here.


I don't think that's an issue, ...


But well, it is. Test the code with input like

Code: Select all

2

3 1 2
32.0 54.7 -2

3 1 2
32.0 54.7 -2
and you'll see the reason. I'll describe it if any doubt.