i have two arrays, i wanted to output only the later one, but in the output an element just got a garbage value. but, if i printed the first array before it then the garbage value in the second array isn't outputted anymore (well, i had to compile & run 3/4 times in order to get the correct value once, the remaining 2/3 times i get the garbage value)
( i run my code without printing the first array in codechef.com/ide , and it gave the desired output every time. )
so i have two questions.
- why did this happen? 
- why did this happen only to my & not to the oj? 
i was trying to implement heap build, that is to take an array a , put this array to another array called heap in a way that the heap follows the property of Binary Min Heap . lastly, print the elements of heap . when i compiled and run it, it outputted a garbage value instead of a certain element's value each time. then , to debug it , i took a loop before printing the heap in order to print the a array to see if it takes the inputs correctly. this time, the heap array was printed correctly! but when i again compile and run it, it gave me the same garbage value instead of an element's value as before. i just began to compile and run the code for some times. i noticed that, in every 3/4 compilations , it gave correct answer just once.
later i submitted my code in codechef's ide, this time i got the correct output every time without having printing the extra array a.
#include <bits/stdc++.h>
using namespace std;
int main(void)
{ 
    int a[10];
    for (int i = 0; i < 6; i++)
    {
        cin >> a[i];
    }
    int heap[10];
    for (int i = 0, h = 1; i < 6; i++, h++)
    {
        if(h == 1) heap[h] = a[i];
        else{
            heap[h] = a[i];
            int k = h;
            //checking if the new heap element violates the heap property
            //if it does then swap with it's parent , and repeat
            while(k){
                if(heap[k] < heap[k/2]) swap(heap[k], heap[k/2]);
                k /= 2;
            }
        }
    }
    /* if this part is executed then the heap printing
    // part prints the correct heap array atleast one time
    // in every 3 or more than 3 "build & run" without any garbage value
    //if not executed, then the heap printing part prints a 
    //garbage value each and every time 
    */
    // weird start
    //prints the main array
    // for (int i = 0; i <= 5; ++i)
    // {
    //  cout << a[i] <<' ';
    // }
    // cout << endl;
    //weird end
    //heap printing part
    for (int i = 1; i <= 6; ++i)
    {
        cout << heap[i] << ' ';
    }
    cout << endl;
    return 0;
}
for input: 6 5 1 4 2 3
i expected: 1 2 3 6 4 5
i got: 2 4 3 6 5 4199040
(editor: sublime text and codeblocks & compiler: minGW)
update: replacing while(k) with while(k/2) did the job for outputting the answer without any help printing of another array. but how did the code generated a two different output in several build & run operations in my device?  and it every time outputted the right answer in the online ide but not in mine. (this was the main question actually) 
