0

http://www.spoj.com/problems/MORENA/ Getting WA in spoj, running fine otherwise on ideone, for the test cases. any idea? Earlier i wrote this in java, was getting NZEC. Wrote this in C then.

#include<stdio.h>

int main(){

    int n,i;
    scanf("%d",&n);
    long num[n];
    for(i=0;i<n;i++){
        scanf("%ld",&num[i]);
    }

    int flag;
    int l;
    for(l=0;l<n;l++){
        if(num[l+1] > num[l]){
            flag = 1;
            break;
        }
        else if(num[l+1] < num[l]){
            flag = 0;
            break;
        }
    }
    int count = 1,k;

    for(k =0; k<n-1; k++){
        if(flag){
            if (num[k+1] > num[k]){
                count++;
                flag = 0;
            }
            else if(num[k+1]==num[k]){
                flag = 1;
            }
            else if(num[k+1]<num[k]){
                //count++;
                flag=1;
            }
        }
        else{
            if(num[k+1] < num[k]){
                count++;
                flag = 1;
            }
            else if(num[k+1]==num[k]){
                flag = 0;
            }
            else if(num[k+1]>num[k]){
                //count++;
                flag = 0;
            }
        }
    }

    printf("%d",count);
    return 0;
}
4
  • 7
    Don't assume everyone knows what "WA" and "NZEC" mean. I certainly don't. Jun 22, 2014 at 5:45
  • 3
    Mini SPOJ Glossary: SPOJ: Sphere Online Judge (a programming problem/puzzle/contest site), WA: Wrong Answer, NZEC: Non-Zero Exit Code, TLE: Time limit Exceeded Jun 22, 2014 at 5:53
  • 4
    @MichaelBurr: I suggest editing that into the question. Jun 22, 2014 at 5:53
  • IMO long is not recommended since it's 32 bits on Windows and 64 bits on most Linux/Unix implementations. C standard only specifies that long has at least 32 bits and you can't know how long is long in the judge's compiler. Assuming it's 64 bits may lead to severe overflows. If you need 64-bit type you should use long long which is guaranteed to be at least 64 bits instead, or use int64_t in stdint.h. Note that if I didn't read MichaelBurr's comment above I would think that WA is warning. Don't assume everyone knows what you said
    – phuclv
    Jun 22, 2014 at 10:00

2 Answers 2

1

Wait what, MANY problems here. Here are few, first:

  int n,i;
  scanf("%d",&n);
  long num[n];

isn't possible (or isn't supposed to be possible at least) to declare an array in the size of a certain variable, use malloc() for that by doing so:

long* num = malloc(sizeof(long)*n);

Another problem, is that you cross the boundary of the array in the first loop, which is weird because you took care of it in the second one :P Just change: for(l = 0 ; l < n ; l++) to for(l = 0 ; l < n-1 ; l++) as in your IF statement you use the array l+1 element, and when l is n-1 you actually test n-1 element compared to the Nth one - which isn't in the boundary of your array. Other than that the code seems okay.

3
  • VLA is available since C99, but you have to explitcitly specify --std=c99
    – phuclv
    Jun 22, 2014 at 9:50
  • Lu'u - I know, but it's not correct in original (in my compiler you don't need to specify --std=c99 though) and I'm not sure what site runs what...
    – Zach P
    Jun 22, 2014 at 9:52
  • Yes I mean you can't assume anything if don't know exactly the compiler and the compile flags. Using only what is guaranteed is better
    – phuclv
    Jun 22, 2014 at 10:03
0

The question has strong bonds with competitive programming. Usually there is a problem displayed and user is expected to submit code which gives expected output for tested input for online judge, and SPOJ being one of them. And actually there is sample input and sample output displayed an almost all problems. So your code would give same output as shown in sample output for problem's sample input in ideone, but your code is tested to huge amount of data in online judge and that would result in NZEC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.