-1

I have been using DEV C++ for the last 2 months and never faced such an issue. But today when I am running the follow code, the execution screen is displaying nothing.

#include <bits/stdc++.h>

using namespace std;
int main()
{
    int n;
    cout<<"ENTER THE LENGTH OF THE ARRAY:";
    cin>>n;
    
    int a[n];
    
    cout<<"ENTER THE ELEMENTS OF THE ARRAY:";
    for(int i=0; i<n; i++){
        cin>>a[i];
    }
    
    const int N = 1e6+2;
    
    int idx[N];
    
    for(int i=0;i<N;i++){
        idx[i]=-1;
    }
    
    int minidx = INT_MAX;
    
    for(int i=0; i<n; i++){
        if(idx[a[i]]!=-1){
            minidx = min(minidx, idx[a[i]]);
        }
        else{
            idx[a[i]]=i;
        }
    }
    
    if(minidx == INT_MAX){
        cout<<"NO REPEATING ELEMENT FOUND";
    }
    else{
        cout<<"REPEATING ELEMENT FOUND AT:"<<minidx+1;
    }
    
    return 0;
}

This is the screenshot of the output screen.

Pls suggest me a solution to this problem.

  • 2
    Time to switch to a better C++ compiler, such as [GCC](http://gcc.gnu.org/) (with [GDB](https://www.gnu.org/software/gdb/) debugger). Use GCC with all warnings and debug info: `g++ -Wall -Wextra -g` and read [a good C++ programming book](https://www.stroustrup.com/programming.html) then see [this C++ reference](https://en.cppreference.com/w/cpp) – Basile Starynkevitch Dec 14 '20 at 06:31
  • Take also inspiration from existing C++ open source software: [Qt](https://qt.io/), [FLTK](https://fltk.org/), [Fish](https://fishshell.com/), [RefPerSys](http://refpersys.org/), [Clang static analyzer](http://clang-analyzer.llvm.org/) (it should be useful to you). Consider, if so permitted, to install [Debian](http://debian.org/) on your laptop – Basile Starynkevitch Dec 14 '20 at 06:34
  • 1
    Why do you have to use DevC++? – Basile Starynkevitch Dec 14 '20 at 06:52
  • 3
    see [Why should I not #include ?](https://stackoverflow.com/q/31816095/995714), [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Dec 14 '20 at 06:56
  • 1
    Also, about `a`: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – Bob__ Dec 14 '20 at 07:22

2 Answers2

1

I have been using DEV C++ for the last 2 months

Notice that DEV C++ is an old, non-standard conforming, and buggy compiler.

int n;
cout<<"ENTER THE LENGTH OF THE ARRAY:";
cin>>n;

int a[n];

What would happen if your user has input -1234? Arrays cannot have negative dimensions, and your code did not test that!

You deserve using a better C++ compiler

E.g. the ones (GCC or Clang) inside a Debian distribution, with a good source code editor such as GNU emacs and some good build automation tool like GNU make.

#include <bits/stdc++.h>

is wrong and non standard conforming, and is slowing down your compilation.

const int N = 1e6+2;

int idx[N];

This is not reasonable: you want more than four millions bytes on your call stack, which is often limited to a megabyte. My guess is that you are experimenting some stack overflow for your automatic variable idx

Consider using standard C++ containers, and upgrade your compiler to a decent one.

I recommend using a recent GCC (at end of 2020, this means GCC 10) compiler as g++ -Wall -Wextra -g since you want all warnings and debug info. You could even use static analysis options to g++

Once you got no warnings, use the GDB debugger to understand the behavior of your program.

Read a good C++ programming book

See this C++ reference, the documentation of your C++ compiler, and of your debugger.

Consider using tools like the Clang static analyzer on your code.

Refer to a recent C++ draft standard, such as n4659. Be afraid of undefined behavior. Take inspiration from existing C++ open source code on github or gitlab.

In a few months, if so allowed, consider coding your GCC plugin to find some bugs in your programs. Be aware of Rice's theorem.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

DevC++ seems now supported by Embarcadero: https://github.com/Embarcadero/Dev-Cpp/releases checked it, and it is looking ok enough. I will assume that you are not using the 1991 version of this IDE and the compiler it came with; Of course, if you want to use it with Windows 98 then the old version might be an option,

Anyhow, I will try to list the issues in your code. Most of them are a trap for beginners:

int n;
cout<<"ENTER THE LENGTH OF THE ARRAY:";
cin>>n;

in this part, you are not forcing flushing before asking for user input. It is always a good practice to flush before user-input. How this can be an issue? Long story short you can get for example C/C++ printf() before scanf() issue solution: c++ force std::cout flush (print to screen).

Let's continue you are using Variable-length array (VLA) here which is not supported by C++ stdandard,

int a[n];

You can check for more Why aren't variable-length arrays part of the C++ standard?

Recommend using std::vector a(n); there which it will allocate in the heap. If yo want the allocation in the stack, within standards then you either need to wait for "dynarray" (which may never be available in C++) or switch to C...

const int N = 1e6+2;

int idx[N];

for(int i=0;i<N;i++){
    idx[i]=-1;
}

in the code below, you are trying to allocate a gigabyte of buffer in the stack. I suspect this is the reason why your program is not running properly. If you want a cheap hack then you can convert this allocation to "static int idx[N];" which will move this buffer to the heap. Beware using a static variable is not a good practice at all and not recommending it. Recommend either "new []" or "vector" there too,

   for(int i=0; i<n; i++){
        if(idx[a[i]]!=-1){
            minidx = min(minidx, idx[a[i]]);
        }
        else{
            idx[a[i]]=i;
        }
    }

idx[a[i]] has no boundary check. you should check the boundary for both arrays. First of all, this is a security leak and a very bad-way of writing a program: Example of a buffer overflow leading to a security leak

Also, this part looks like it has a logic issue: Withing the boundary all the "idx" elements should be "-1" so you should never get into the else-condition (in a normal way)

Abdurrahim
  • 2,078
  • 1
  • 17
  • 23