-1

The terminal works fine while running my previous codes except the recent one. Most likely there's some error in my code but I tried running the same in an online compiler and it worked just fine.

Here's the screenshot of the terminal: enter image description here

Here's my code:

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

void stair_search(int a[][1000], int row, int col, int k){
    int i=0, j=col-1;
    while(i < row and j >= 0){
        if(a[i][j] == k){
            cout<<k<<" found at "<<i<<", "<<j<<endl;
            return;
        }

        if(a[i][j] > k){
            j--;
        }else{
            i++;
        }
    }

    cout<<"Element not present!"<<endl;
}

int main(){

    int a[1000][1000];
    int row, col;
    cin>>row>>col;

    // Take input
    for (int i=0; i<row; i++){
        for (int j=0; j<col; j++){
            cin>>a[i][j];
        }
    }

    stair_search(a,row,col,16);

    return 0;
}
Killjoy
  • 51
  • 1
  • 6
  • 1
    `int a[1000][1000];` - How much stack space that would take? – Evg Aug 14 '21 at 06:01
  • 2
    [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Aug 14 '21 at 06:02
  • 1
    VTC for lack of any functional description of what this is even supposed to do, much less how it appears to be doing it wrong vs. the expectation of what it should look like when right. And for the record, [Getting a stack overflow exception when declaring a large array](https://stackoverflow.com/questions/571945/getting-a-stack-overflow-exception-when-declaring-a-large-array) is likely highly relevant. – WhozCraig Aug 14 '21 at 07:28

1 Answers1

0

Try adding a try/catch block to your functions and see if you get an error. Like this:

void stair_search(int a[][1000], int row, int col, int k){
try{
int i=0, j=col-1;
while(i < row and j >= 0){
    if(a[i][j] == k){
        std::cout<<k<<" found at "<<i<<", "<<j<<"\n";
        return;
    }

    if(a[i][j] > k){
        j--;
    }else{
        i++;
    }
}

std::cout<<"Element not present!"<<"\n";
}
 catch(const std::exception& ex){
    std::cout << "Error in stair_search: " << ex.what() << "\n";
    return -1;
 }
}

int main(){

int a[1000][1000];
int row, col;
std::cin>>row>>col;

// Take input
for (int i=0; i<row; i++){
    for (int j=0; j<col; j++){
        std::cin>>a[i][j];
    }
}

stair_search(a,row,col,16);

return 0;    
}

Also "using namespace std;" is considered very bad practice because you can have overlap of identically named functions. Don't do it. And std::endl should be avoided, it flushes the I/O stream buffer on every call which is pretty slow.

It seems to work for me, but maybe your compiler is different...is

 void stair_search(int a[][1000], int row, int col, int k)

considered undefined behavior, shouldn't it be:

 void stair_search(int a[1000][1000], int row, int col, int k)

or:

 template<size_t sA, size_t sB>
 void stair_search(std::array<std::array<int,sB>sA> a, int row, int col, int k)

Try turning on warnings.

dave_thenerd
  • 448
  • 3
  • 10