482 - Permutation Arrays

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

Moderator: Board moderators

Post Reply
Wei-Ming Chen
Experienced poster
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

482 Run Time Error (?)

Post 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;
}

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post 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:
Last edited by Roby on Thu Apr 20, 2006 9:54 am, edited 1 time in total.

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post 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.

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post 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 :)

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post 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?

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post by Roby »

Anyone, help me please... :cry:

Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post 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

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post 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.

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post 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:

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post 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.

Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post by Roby »

Code: Select all

AC already
Last edited by Roby on Thu Apr 20, 2006 9:44 am, edited 1 time in total.

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post 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.

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post 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
Last edited by yiuyuho on Thu Apr 20, 2006 5:50 am, edited 1 time in total.

yiuyuho
A great helper
Posts: 325
Joined: Thu Feb 21, 2002 2:00 am
Location: United States
Contact:

Post 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.

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post 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.

Post Reply

Return to “Volume 4 (400-499)”