#On submission showing wrong answer

21 messages · Page 1 of 1 (latest)

lavish wave
noble furnaceBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

covert hinge
#

Are you getting an error? what kind of error? cant really help if you dont tell us whats wrong

lavish wave
#

its not showing any error its just telling code is wrong!

#

that means some test case is not passing

#

no where i m getting the solution for this question

remote olive
#

its just telling code is wrong!

Might that have something to do with the VLAs in your code?

novel windBOT
#
What Is a VLA And Why Is It 'Bad'?

A Variable Length Array (VLA) is an array where the size is not constant and depends on a variable.

VLAs have poor compiler support and can lead to inefficient code. The core issue with VLAs is that the compiler doesn't know the size of the stack frame. Without warning flags like -Wvla (turned on by -Wall) it can be easy to create a VLA by accident, even in C++ with some compilers.

Compiler Support

✅ available since C99
⛔ not available in C++ at all
⛔ was never supported by MSVC
⚠ optional feature since C11
⚠ supported as non-standard extension by GCC, clang

remote olive
#

I suggest you turn these vlas into vector<int>.

lavish wave
#

basically somewhere my code is wrong but not where it is?? VLA is okay in that site i have solved other questions also

#

but in this ques i m not getting the ques properly

remote olive
#

You'll have to adapt for user input yourself...

#

;compile

#include<bits/stdc++.h>
using namespace std;

int main(){
    constexpr int n = 5;

    int test[n] = { 1, 2, 0, 1};
    int a[n] = {};

    a[0] = test[0];

    for(int i = 1; i < n; i++) {

        switch (test[i-1]) {

        case 0:
            // same as previous element
            a[i] = a[i-1];
            break;

        case 1:
            // one more than previous element
            a[i] = a[i-1] + 1;
            break;

        case 2:
            // one less than previous element
            a[i] = a[i-1] - 1;
            break;
        }

    }

    for(int i = 0; i < n; i++) {
        cout << a[i] << ' ';
    }

    cout << endl;
}
honest jettyBOT
#
Program Output
1 2 1 1 2
remote olive
#

I think you overcomplicated your test[j-1] == 2 clause.

Having said that, your solution still comes up with the correct answer for the 1 2 0 1 sample set.
So could it be that this last space you print throws the spanner in the works?

#

Try this:

for(int i = 0; i < n; i++) {
    if (i > 0) cout << ' ';
    cout << a[i];
}
lavish wave
#

thanks!!!!!!

#

!close

noble furnaceBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] On submission showing wrong answer

noble furnaceBOT
#

@lavish wave

This question thread is being automatically marked as solved.