You are not logged in. Please login at www.codechef.com to post your questions!

×

why i am getting SIGSEGV error...problem code walk(practice easy)?

#include<stdio.h>
int main()
{
     int arr[1000],n,j,max=0,m,i;
    scanf("%d",&i);
    while(i--)
    {
        scanf("%d",&n);
        scanf("%d",&max);
        arr[0]=max;
        for(j=1;j<n;j++)
        {
            scanf("%d",&arr[j]);
            if(max<arr[j])
            max=arr[j];
        }
        m=max;
        for(j=0;j<n;j++)
        {
            //  printf("\t%d %d",max,j);
            if(max<arr[j])
            {
                //j=0;
                j=-1;
                m++;
                max=m;
            //  printf("uu-%d",max);
                continue;

            }
            max--;

        }
        printf("%d\n",m);
        max=0;
    }
    return 0;
}

asked 17 Aug '17, 23:32

pant0000's gravatar image

3★pant0000
111
accept rate: 0%

edited 18 Aug '17, 00:31

vijju123's gravatar image

5★vijju123 ♦
12.2k1319

1

Please provide link to the problem

(17 Aug '17, 23:45) kunnu1203★

An also either format your code properly, or just give an ideone link :/

(17 Aug '17, 23:50) ista2000 ♦5★

Hi, Look initially, you initialized the array with 1000 elements (int arr[1000]) and then when the compiler gets to

           for(j=1;j<n;j++)
    {
            scanf("%d",&arr[j]);   
        if(max<arr[j])             this statement
        max=arr[j];
    }

look n value can get up to 10^5 but you have the arr(the array) with the size of 10^3. so it will overflow. so try fixing that I believe it's the reason you getting runtime error. Try initializing that array to int arr[100000].

............

link

answered 18 Aug '17, 00:16

kunnu120's gravatar image

3★kunnu120
5038
accept rate: 5%

Pay a closer attention to constraints next time. Index-out-bound exception is giving you runtime error, as kunnu rightly pointed out.

link

answered 18 Aug '17, 00:33

vijju123's gravatar image

5★vijju123 ♦
12.2k1319
accept rate: 18%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×530
×207
×7

question asked: 17 Aug '17, 23:32

question was seen: 191 times

last updated: 18 Aug '17, 00:33