-2

I've tried to perform a simple binary/linear search but there's a problem with the output, I suspect the function isn't being called

A small portion of void main:

 void main()
 {  cout<<"Linear or Binary? (1/2)"<<endl;
      cin>>ch;
   switch(ch)
   {
       case '1': pos = linear();
               cout<<"Position: "<<pos;
               break;
       case '2': pos = binary();
               cout<<"Position: "<<pos;
               break;
       default: cout<<"error"<<endl;
    }
 } 

                                             //here is a function:
int linear()
{    
     int a, n, ar[n], posn;
     cout<<"Enter size of array and array"<<endl;
     cin>>n;

     for(int i =0; i<n; i++)
     {
         cin>>ar[i];
     }

     cout<<"enter element to be found"<<endl;
     cin>>a;

     for(int j=0; j<n; j++)
     {
         if(ar[j]==a)
         {
             posn= j+1;
         }
     }
     return posn;
}

The output is just garbage or junk. None of my couts are showing up, simply one random int junk value.

Confused
  • 1
  • 2
  • `int a, n, ar[n]` can only lead to problems. Raise the warning level of your compiler. You might be solve the problem by yourself. – R Sahu Feb 03 '19 at 05:51
  • 1
    **Don't use `void main()`**!, there is no overload of main with `void` as the return type... Use `int main()`... See [here](https://stackoverflow.com/questions/636829/difference-between-void-main-and-int-main/636834#636834)... – Ruks Feb 03 '19 at 06:06
  • `posn` is left uninitialized when the given char is not found in the string and thus the output being `garbage` (*Undefined behavior*) means that it did not find any occurrence of the given char anywhere in the string... To prevent that, it is better to initialize `posn` to a default value, like: `int posn = -1;`... Also, `int ar[n]` is also undefined behavior, since `n` was not initialized before it... And, unfortunately C++ as of yet does not support [*variable length arrays*](https://www.geeksforgeeks.org/variable-length-arrays-in-c-and-c/)... – Ruks Feb 03 '19 at 06:12
  • Stepping through the code in your debugger will tell you whether a function is being called or not, as well as allowing you to see exactly what is happening in the code as it executes. – Ken White Feb 03 '19 at 06:13

1 Answers1

1

There are quite a few problems in your code:-

  • Leaving the values of important varibles such as posn and n uninitialized...

    C++ invokes undefined behavior the time you try to access the value of a variable that is not initialized... And UB can be anything, so the garbage value is expected...

    To prevent that from happening, initialize them before use...

    int a, n, posn = -1;
    cout<<"Enter size of array and array"<<endl;
    cin>>n;
    
  • C++ does not have the feature of variable length arrays yet (Only in C99 and above...) and hence ar[n] is not possible (Though there are some compilers like Ideone which support it, but according to the standard, it is not possible), that is why we have got the advantages of a class called vector inside of the namespace std

    Note: #include <vector> before use...

    int linear() {
        int a, n, posn = -1;
    
        cout << "Enter size of array and array: " << endl;
        cin >> n;
        // Use vectors for dynamic arrays (or pointers if you want to stay old-school...)
        vector ar(n);
    
        for(auto i = 0; i < n; i++)
            cin>>ar[i];
    
        cout << "Enter element to be found: " << endl;
        cin >> a;
    
        for(auto j = 0; j < n; j++)
            if(ar[j] == a)
                posn= j + 1;
    }
    
Kunal Puri
  • 3,419
  • 1
  • 10
  • 22
Ruks
  • 3,886
  • 1
  • 10
  • 22
  • @KunalPuri `ar[n]` is possible IF the compiler being used supports it. It's not required by the C++ Standard ([for a number of good reasons](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard)) so in addition to placing a stack overflow generator in the middle of your program, you've restricted your program's portability. – user4581301 Feb 03 '19 at 06:56
  • @KunalPuri Try the same code [with Visual C++](https://rextester.com/l/cpp_online_compiler_visual). VC++ is one of the most important compilers out there. – Sebastian Redl Feb 03 '19 at 06:59
  • @KunalPuri *Ideone* is **strictly not** a portable compiler and is *not used to generate portable code either...* Try the same code is any other compiler: [Even GCC does not support it!](https://coliru.stacked-crooked.com/a/b9f55a11bc70a510)... – Ruks Feb 03 '19 at 07:07