Page 1 of 1

Compiler problem?

Posted: Fri May 21, 2004 9:53 pm
by w k
Hi,
I'd like to know why at the code that begins like this:
# include <stdio.h>
# define TMAX 62600

unsigned short int k,m,n,i,p,q,j,l;
char *txt="Case ";
unsigned short int sko1[TMAX], sko2[TMAX], sko3[TMAX], sko4[TMAX];

int main() {
scanf("%d",&m);
for (i=1;i<=m;i++){
scanf("%d%d%d",&n,&p,&q);
for (j=0;j<=p;j++)
scanf("%d",&sko1[j]);
. . .
I got WA and execution tikme over 7s. But when I changed just the variable declaration part to the following:
# include <stdio.h>
# define TMAX 62600

unsigned short int i,k,m;
unsigned short int n,j,p;
unsigned short int q,l;
char *txt="Case ";
unsigned short int sko1[TMAX], sko2[TMAX], sko3[TMAX];
unsigned short int sko4[TMAX];

int main() {
scanf("%d",&m);
for (i=1;i<=m;i++){
scanf("%d%d%d",&n,&p,&q);
for (j=0;j<=p;j++)
scanf("%d",&sko1[j]);
. . .
I got AC and execution time less than 1s?? During debugging I recognized that the WA was linked to the strange behaviour of variable "i" which in the first version of programm surprisingly changed to 0 within the for loop after execution of scanf function.
Coudl somebody explain it?? :-?

Hi

Posted: Sat May 22, 2004 8:34 am
by sumankar
Are you using mail to submit the code ?

Then maybe your mail server treats your code as a little too
much for it and then does some "intelligent" processing
which our dear judge fails to comprehend.

Another small suggestion: when you are using ` unsigned short
int's` why are you using the specifiers for `signed int ` in
printf and scanf?I did get some weird behaviour being that lazy :-)

Hope that helps,
Suman

Posted: Sat May 22, 2004 9:32 am
by w k
Suman,

I don't submit this code by e-mail and compiling the first code on my computer I get the same problem. So this is not this case.
I tried Your second suggestion and I changed the code to the following:
# include <stdio.h>
# define TMAX 62600

unsigned short int k,m,n,i,p,q,l,j;
char *txt="Case ";
unsigned short int sko1[TMAX], sko2[TMAX], sko3[TMAX];
unsigned short int sko4[TMAX];

int main() {
scanf("%u",&m);
for (i=1;i<=m;i++){
scanf("%u%u%u",&n,&p,&q);
for (j=0;j<=p;j++)
scanf("%u",&sko1[j]);

for (j=0;j<=q;j++)
scanf("%u",&sko2[j]);
printf("%s%u%s",txt,i,": ");
. . .

but it doesn't improve the situation. The program still gives wrong output. But if I declare the variable "i" as the first one in the row - just after "unsigned short int" everything is OK!
What the problem could it be??
Do You have any idea?
Wojciech

Posted: Mon May 24, 2004 10:19 am
by CDiMa
w k wrote:[c]
unsigned short int k,m,n,i,p,q,l,j;
char *txt="Case ";
unsigned short int sko1[TMAX], sko2[TMAX], sko3[TMAX];
unsigned short int sko4[TMAX];

int main() {
scanf("%u",&m);
for (i=1;i<=m;i++){
scanf("%u%u%u",&n,&p,&q);
for (j=0;j<=p;j++)
scanf("%u",&sko1[j]);

for (j=0;j<=q;j++)
scanf("%u",&sko2[j]);
printf("%s%u%s",txt,i,": ");
/*. . .*/
[/c]
but it doesn't improve the situation. The program still gives wrong output. But if I declare the variable "i" as the first one in the row - just after "unsigned short int" everything is OK!
notice that i is declared after n. The compiler allocates space for i right after n. While you read n as an unsigned int *, it thrashes i. Read it as an unsigned short int ("%hu") and all should fit confortably in its allocated space ;).

Ciao!!!

Claudio

Hi

Posted: Mon May 24, 2004 10:34 am
by sumankar
Hopefully CDiMA has solved your problem.

I was wondering whether the for loop where i is
used as a counter does modify i or not: we don't have the
full code.If not, forgive my naivete!

Suman.

Posted: Mon May 24, 2004 9:48 pm
by w k
Claudio,

You touched the point! I applied "%hu" format and now it works fine!

Thanks,

Wojciech :wink:

Posted: Mon May 24, 2004 9:49 pm
by w k
Claudio,

You touched the point! I applied "%hu" format and now it works fine!

Thanks,

Wojciech :wink: