0
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    long long int a[10^9],sum=0;
    int n,i,length;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        if(0<=a[i]<=10^10)
        {
        scanf("%lld",&a[i]);
        }
   }

    for(i=0;i<n;i++)
    {
        sum=sum+a[i];   
    }

    printf("%lld",sum);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

i dont know the reason why i am getting the segentation fault this code runs fine for this input 1000000001 1000000002 1000000003 1000000004 1000000005

5
  • 7
    ^ is the XOR operator in C, not the exponent operator. Commented Jun 21, 2015 at 4:43
  • 2
    10 ^ 9 == 3; 10 ^ 10 == 0. 0<=a[i]<=10^10 is legal, but it doesn't do what you think it does. It's equivalent to (0 <= a[i]) <= 10^10. (0 <= a[i]) yields either 0 or 1; that value is then compared to 10^10, which is 0. Commented Jun 21, 2015 at 5:10
  • 3
    @KeithThompson ^ has lower priority than <=, so <= 10^10 should be change to <= (10^10). Commented Jun 21, 2015 at 5:35
  • @EricWang: Good catch! So 0<=a[i]<=10^10 is equivalent to ((0 <= a[i]) <= 10) ^ 10 (I hope I got that right this time!) Commented Jun 21, 2015 at 5:56
  • regarding this line: 'if(0<=a[i]<=10^10)' I think this is actually trying to check if the contents of a[i] is within a certain range. However, a[i] has not been set and 10^10 is 0, so the chances of a long long int located on the stack, being 0 are rather slim. Commented Jun 21, 2015 at 18:05

1 Answer 1

4

Issues in your code:

  • 0<=a[i]<=10^10 is not correct, should change to 0<=a[i] && a[i]<=(10^10)
  • ^ is a bitwise xor, not power,
  • In your for loop, you always compare before read element of a[], so you need to read first, then compare.
  • use unsigned long long, don't need int at end.

Check this code:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

#define MAX_NUM 1000000000ULL
#define MIN_NUM 0ULL

int main() {
    int n,i;
    printf("input number count: ");
    scanf("%d",&n);
    unsigned long long a[n],sum=0;

    for(i=0;i<n;i++) {
        printf("input number[%d]: ", i);
        scanf("%llu",&a[i]);
        if(a[i]<MIN_NUM || a[i]>MAX_NUM) {
            a[i] = 0;
            printf("\t(ignored, due to out of range [%llu, %llu])\n", MIN_NUM, MAX_NUM);
        }
    }

    for(i=0;i<n;i++) {
        sum+=a[i];
    }

    printf("\nsum: %llu\n",sum);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.