Video Game : Push back gives WA?

Hi ,

These are my two submissions of Video Game problem in ZCO :

Push back , SIGABRT : CodeChef: Practical coding for everyone
No Push back , No SIGABRT :slight_smile: : CodeChef: Practical coding for everyone

Can someone pls tell me why this happens ?
Pls dont downvote the post , i am newbie here , it will be really demotivating for me
Pls dont downvote

Thankyouu
:slight_smile:

1 Like

This answer by admin explains why you get a SIGABRT error.
Also, it looks like you get WA with your code. You can learn from mine:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, h;
    cin >> n >> h;
    vector <int> heights;
    heights.resize(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> heights[i];
    }
    vector <int> queries;
    queries.resize(0);
    int q = 1;
    while (q != 0)
    {
        cin >> q;
        queries.push_back(q);
    }
    int current = 0;
    bool hasBox = false;
    for (int i = 0; i < queries.size(); ++i)
    {
        if (queries[i] == 1)
        {
            if (current == 0)
            {
               ; 
            } else {
                current--;
            }
        } else if (queries[i] == 2)
        {
            if (current == (n - 1))
            {
                ;
            } else {
                current++;
            }
        } else if (queries[i] == 3) {
            if (hasBox == true || heights[current] == 0)
            {
                ;
            } else 
            {
                heights[current]--;
                hasBox = true;
            }
        } else if (queries[i] == 4) {
            if (hasBox == false || heights[current] == h)
            {
                ;
            } else 
            {
                heights[current]++;
                hasBox = false;
            }
         } else {
                break;
         }
    }
    for (int i = 0; i < n; ++i)
    {
        cout << heights[i] << " ";
    }
}

P.S. I wrote this code a long time ago so I use a lot of noob practices (like only using a semi-colon in a line :sweat_smile:) .
Hope you understand.

1 Like

Saw that , didnt understand why i got SIGABRT
Can you pls explain ?
Thanks

Try initializing your o vector with size 0.

Tried it but no luck

a[i]--;

This is what’s causing the crash in the push_back version, though I’m not sure (yet) why it’s not crashing in the non-push_back version.

Edit:

Oh, I see - in the non-push_back version, you’re only reading in one instruction, so it doesn’t get to the instruction that causes the crash:

        cin >> o[cnt];
    	cnt++;
    	if(o[cnt] == 0) break; // o[cnt] == 0 will always be true.
1 Like

Then what to replace ?

Think about it some more. What do you do in the o[i] == 4 case? Why are you doing it differently for o[i] == 3?

2 Likes

Got it :sob:
Thanks @ssjgz

1 Like

wait a minute…we can downvote a post?!

Nope :slight_smile:

1 Like

I dont know
I am new to codechef

1 Like

@trial_codes

1 Like

yeah same…started last week or something :flushed:

Komedy Achived :upside_down_face:

1 Like