0

can anyone please solve the error i am facing in this problem please can you try to figure it out ? i will provide link for this question below. https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/practice-problems/algorithm/a-game-of-numbers-1-5d3a8cb3/.

i was not getting output

#include<iostream>

using namespace std;

int main()

{ int l,n,j;
    int a[n],f[n],g[n];

    cin>>n;
    l=n;

    for(int i=n-1;i>=0;i--)
    {
        cin>>a[i];
    }

    for(int i=n-1;i>=0;i--)
    {
        for(j=i-1;j>=0;j--)
        {
           if(a[j] > a[i])
           {
             f[l-i] = l-j;
           }
           else
           {
             f[l-i] = -1;
           }
        }
    }

    for(int i=n-1;i>=0;i--)
    {
        for(j=i-1;j>=0;j--)
        {
           if(g[j] < g[i])
           {
             g[l-i] = l-j;
           }
           else
           {
             g[l-i] = -1;    
           }
        }
    }

    for(int i=0;i<l;i++)
    {
        if (f[i]== -1)
        {
            cout<<-1;
        }
        else
        {
            cout<< a[g[f[i]]];
        }
    }
}
bruno
  • 32,421
  • 7
  • 25
  • 37

1 Answers1

1

in

int main()

{ int l,n,j;
    int a[n],f[n],g[n];

n is not yet initialized, the behavior is undefined as the size of your arrays, at least do first cin>>n;

but

1) I encourage you to not use VLAs (variable length arrays) 'C' arrays

2) do not do just cin>>n; without checking the input was correct, else n will be set to 0 (since C++11) which is probably not what you expect and without clearing the error and bypass invalid input the next cin>>a[i]; will also fill your array with 0

3) you do if(g[j] < g[i]) without having initialized g, that will also have consequence later in cout<< a[g[f[i]]]

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 1. it isn't deprecated, its never been valid c++, 2. as of c++11 failed reads set the value to 0 – Alan Birtles May 27 '20 at 09:36
  • @AlanBirtles there is no C++11 tag in OP question, just C++ ;-) – bruno May 27 '20 at 09:42
  • @AlanBirtles yes c++ but not c++11 – bruno May 27 '20 at 09:44
  • by now c++11 is 9 years old, so if the question isnt tagged as C++03 it should be safe to assume that C++11 or later is used. Actually the C++ tag is for the current standard which is C++17 – 463035818_is_not_an_ai May 27 '20 at 10:02
  • in any case "else n can be still not initialized" is generally speaking wrong – 463035818_is_not_an_ai May 27 '20 at 10:03
  • @idclev463035818 unfortunately I still have to use c++ before c++11 (cannot give more details about where) ^^ – bruno May 27 '20 at 10:05
  • @idclev463035818 anyway it is not clear for me the c++ tag in S.O. means C++17, this is not written in C++ tag definition, may be tag need to be edited ? – bruno May 27 '20 at 10:09
  • from the tag description: "Unless the question explicitly mentions which version of the C++ standard is used, it is assumed that the current version is used. That is, whichever version of ISO 14882 that ISO currently lists as active. Please have this in mind when answering or commenting on questions tagged c++." – 463035818_is_not_an_ai May 27 '20 at 10:23
  • @idclev463035818 where do you read that ? the definition of the tag is `C++ is a general-purpose programming language. It was originally designed as an extension to C, and has a similar syntax, but is now a completely different language. Use this tag for questions about code (to be) compiled with a C++ compiler. Use a version-specific tag for questions related to a specific standard revision [C++11], [C++14], [C++17] or [C++20] etc.` – bruno May 27 '20 at 10:26
  • that is the short version (don't know the official names). Click "learn more..." to see the full tag info – 463035818_is_not_an_ai May 27 '20 at 10:27
  • @idclev463035818 ah ok, that was *much* better to put that precision in the 'short' description. Thank you – bruno May 27 '20 at 10:29
  • maybe the short info could be made more clear by saying "...compiled with a C++ compiler using the current standard", but that would imply that the C++ tag should not be used for questions that are not about the current standard, well, i dont know... – 463035818_is_not_an_ai May 27 '20 at 10:31
  • "dynamic C arrays" are fine also in C++, VLAs (variable length arrays) are not. And as already mentioned, they arent deprecated. They never were standard C++ – 463035818_is_not_an_ai May 27 '20 at 10:36
  • @idclev463035818 I always said "dynamic array" for VLA and see other OP doing the same, ok I edit and will try to memorize VLA ^^ – bruno May 27 '20 at 10:38