-3

The Question:- https://www.codechef.com/JUNE17/problems/NEO01

My solution, which according to me should be the right solution to the aforementioned question but I am constantly getting a RTE.

https://ideone.com/COnlog

#include<stdio.h>
int main()
{
    long arr[10000];
    int t,n,p=0;
    scanf("%d",&t);

    while(t--)
    {
        long hap1=0,hap2=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&arr[i]);
        }

        for(int i=0;i<n;i++)
        {
            if(arr[i]>=0)
            {
                hap1+=arr[i];
                p++;
            }
            else
            {
                hap2+=arr[i];
            }   
        }
        printf("%d\n",hap1*p+hap2);
    }
    return 0;
} 

Edit: Sorry, I am a beginner who usually uses cout and cin, used scanf and printf to save time, and just forgot to add & while using scanf.

Although my code works fine on Dev-C++ 5.11 it shows a Runtime error(SIGSEGV) and I have no idea why is it happening.

  • 1
    You need to learn more about how to use [`scanf`](http://en.cppreference.com/w/c/io/fscanf). I suggest [finding a good beginners book to read](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to learn to program C instead of using sites like codechef. – Some programmer dude Jun 06 '17 at 13:13
  • Add some error checking. To start, `scanf` returns a value that tells you if it found the right number of values. Without your input it's pretty hard for anyone to know exactly where your program is failing, so you should at least add that to your question. – Retired Ninja Jun 06 '17 at 13:20
  • Corrected it, still getting an error RE (SIGSEGV) using Codechef ide. – Arpit Sardana Jun 06 '17 at 13:44

2 Answers2

0

You're using scnaf incorrectly. Change scanf("%d",t); into scanf("%d",&t);

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

The constraint for N in the problem is 1 ≤ N ≤ 10^5 so you need to declare a[size],size=10^5.

sharath
  • 33
  • 5