shamir shakir <dewswo...@...> wrote:
> I found a contrast .....
> Please have a look here
> 
>     unsigned long min, max, num = 0;
> 
>     while(scanf("%li %li", &min, &max) != EOF)

You need %lu to scan an unsigned long. Comparing to EOF isn't
correct. You need to know that _both_ numbers were input, so
you need to compare scanf with 2.

> ...
>         if(min > max)
>         {
>             min^=max;
>             max^=min;
>             min^=max;

At least with an unsigned type this has correct semantics.
But seriously, don't do this! It may impress your mates,
but any production house is going to ask why you want to
make life difficult for your fellow code reviewers.

> ...
> unsigned long findMaxCycleLength(unsigned long min, unsigned
> long max)

The unsigned long return is overkill.

> {
>     unsigned long temp, i;
Choice 1:
>     unsigned long n = findCycleLength(max);
Choice 2:
>     unsigned long n = 0 ;
>     for(i = min; i < max; i++)
>     {
>         temp = findCycleLength(i);
> ...
> here,
> When I compile it with [Choice 1] it seems ok.
> But when I use [Choice 2] it gives me wrong answer.

With the first one you have the cycle length of the largest
number being tested. With the second choice, you never test
the largest number.

> Could you please tell me why it gives me WA

What is WA? Wrong answer? See above.

-- 
Peter

Reply via email to