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

×

Runtime error (SIGSEGV). Please check my code.

include<stdio.h>

int main() { int n, h[20],i,d=0,k,j; printf("\nNo of Students: "); scanf("%d",&n); printf("\nHeight of students: "); for (i=0; i<n; i++) scanf("%d",&h[i]); i=0; for (j=0; j<n; j++) { if (h[i]<h[i+1]) {

  i++;

  }
else
  {
  ++d;
    for (k=0; k<n; k++)
      h[k]=h[k+1];
  }

}

printf("\noutput is : %d",d); return 0; }

asked 14 Apr '15, 23:42

anwer_zaki's gravatar image

2★anwer_zaki
11
accept rate: 0%

Can you please specify which question this code belongs to.

(16 Apr '15, 22:07) arun_as1★

It seems you are allocating less memory,but the question requires more.i.e size of the array.It would have been easy to tell if you would have shared the link of the problem.Because usually run time error comes when you are trying to access those locations which you haven't allocated.
Increase the size of the array,or make it

h[n];

Hope this helps.
Happy Coding!!

link

answered 15 Apr '15, 01:55

ansh1star033's gravatar image

3★ansh1star033
206129
accept rate: 9%

You are allocating less memory as said above , and you are trying to access the nth index at the end of both of your loops even if you get memory fixed you will get wrong answer so run your loop till n-1.

link

answered 16 Apr '15, 19:46

ayush_awasthi's gravatar image

4★ayush_awasthi
16113
accept rate: 0%

Since you have not specified the question, it's hard to tell exactly. But one of the problems might be h[20]. You might need more than 20.

You also have bugs here

for (k=0; k<n; k++)
  h[k]=h[k+1];
         ^
        at k=19, h[k+1] will try to access h[20] which is out of bounds.

Also, if you're trying to submit an answer to one of the questions on codechef, then you don't need output like " No of Students:" and all. They will just give you Wrong Answers. You can read this
It has been stated there that

Submitted solutions are automatically compiled and run under Linux test system. You must keep strictly the input output specification. In particular your program must not printout any additional messages like 'input the number please', which are not specified in the problem formulation.

link

answered 16 Apr '15, 22:17

arun_as's gravatar image

1★arun_as
7413618
accept rate: 10%

edited 16 Apr '15, 22:19

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:

×545
×280
×231

question asked: 14 Apr '15, 23:42

question was seen: 1,191 times

last updated: 16 Apr '15, 22:19