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

×

Different output on ideone and devc++

For the problem CHEFBM I wrote this program ... It seems to run fine on devc++ and gives the expected output 3 3 -1 4 but on ideone, it gives the output -1 -1 -1 -1

If I write a statement on line 52 cout<<""; which basically shouldn't have any effect, the output changes to the expected 3 3 -1 4... link here

Can someone please point out what is happening?

asked 14 May '14, 01:02

siddhanjay's gravatar image

4★siddhanjay
3123
accept rate: 50%

edited 14 May '14, 01:07


One of the reasons such a problem can occur, is that adding the useless statements changes the way data and code are stored in the different segments (text, stack, heap etc.) of the program.

When you access information that you haven't initialized or a variable index is out of bounds, you may get unexpected behavior because of the reason above.

I found two mistakes in the program:

Line 61: while((a[curr].first==i)&&curr!=p)

Let's address this one first. The way short-circuiting (google it) works, a[curr] gets accessed before curr != p is checked. Indices of a must be between 0 and p - 1, making this an instance of variable index out of bounds situation. Since curr!=p does get checked, it should not cause unexpected behavior (it might throw a RTE though, so reorder the conditions anyway.)

Line 75: if(z[j-1]-z[j]>1)

z[m] here, gets accessed because of the for loop right above this line. Out of bounds again. This is probably why your program does't work.

Fix that and your program should run just fine.

link

answered 14 May '14, 03:48

chetan_kakkar's gravatar image

3★chetan_kakkar
714
accept rate: 50%

edited 14 May '14, 03:49

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:

×46
×22

question asked: 14 May '14, 01:02

question was seen: 2,548 times

last updated: 14 May '14, 03:49