-1

I was trying to solve some ds question, all the things were fine algo wise but answer wise (wrong), then to debug, I put cout to check where mistake has been done when I put cout answer is correct , but without it the answer is wrong also, the different online compiler is showing the right answer

without cout and Is 1:7:4 (wrong ans) with it answer is 1:7:3 (correct ans) input is 2 9 1 0 1 1 0 1 1 1 1 0 0 0 1 0 1 0 0 1

 #include <bits/stdc++.h>
 using namespace std; 
 int main()
 {
int n,m;
int a,b;
int ans=0; 
int ansij=INT_MAX;
  cin>>n>>m;
  int arr[n+10][m+10]; 
  for(int i=0;i<n;i++)
  {
  { for(int j=0;j<m;j++) 
    cin>>arr[i][j];
   }
  }
  
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++) 
  { if(i==0&&j==0)
    continue; 
    int score=0;
    if(arr[i][j]==1)
    { //cout<<arr[i][j]<<endl ;
      //1
     if(arr[i+1][j]==1)      
     score++;
     //2
     
     if(arr[i][j+1]==1)
      score++;
      //3
      if(arr[i-1][j]==1)      
     score++;
     //4
     if(arr[i][j-1]==1)
      score++;
      //5
      if(arr[i+1][j+1]==1)      
     score++;
     //6
     if(arr[i+1][j-1]==1)
      score++;
      //7
    if(arr[i-1][j+1]==1)      
     score++;
     //8
     if(arr[i-1][j-1]==1)
      score++;
    }
//cout<<"";
//cout<<"score of i and j is ("<<i+1<<","<<j+1<<") "<<score<<endl;

if(ans<score)
{ ans=score; 
  ansij=i+j;
  a=i;
  b=j;
//cout<<" answer update from gretest"<<endl;
    
}
else if(ans==score)
{ if(ansij>=(i+j))
{ ans=score;
  ansij=i+j;
  a=i;
  b=j;
//cout<<" answer update from qual"<<endl;
}
}
//cout<<" answer in i j is"<<ans<<endl;
}
}  
cout<<a+1<<":"<<b+1<<":"<<ans;   
  
  return 0;
}
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 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 Jul 21 '20 at 10:05
  • 2
    Please indent your code properly. How are we supposed to make any sense out of this jumbled mess? – Paul Sanders Jul 21 '20 at 10:08
  • 2
    Standard C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). Use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. – Some programmer dude Jul 21 '20 at 10:09
  • 2
    `int arr[n+10][m+10];` -- This is not legal C++. This looks like you got this question from one of those "online judge" websites. If so, those sites assume you know the language you're using well enough not to get the basics of the language wrong, or use the language in weird ways. These sites are not designed to teach C++. – PaulMcKenzie Jul 21 '20 at 10:09
  • 1
    For example here: `if(arr[i+1][j]==1)`, you have undefined behavior (UB) if `i == n-1` – Damien Jul 21 '20 at 10:11
  • 2
    As for your problem (besides attempting to use "competition" sites for learning) is that you use uninitialized portions of the arrays. – Some programmer dude Jul 21 '20 at 10:11
  • If just adding a `cout` changes the result of your program, this smells like Undefined Behavior. I would look for out-of-bound accesses to arrays. Though, with `std::vector`, you usually get a range check in debug mode for `operator[]`. – Scheff's Cat Jul 21 '20 at 10:11
  • 2
    Why did you tag "compiler-construction"? I see no BNF, production rules, lexer question, etc. – PaulMcKenzie Jul 21 '20 at 10:18
  • 1
    Too many of these competition websites give the impression that you can learn programming in C++ by answering these (many times, non-trivial) data structures or algorithm questions. That certainly isn't the case -- most of the time, the code is a jumbled mess that would fail a code review in the real world, even if the answer "works". – PaulMcKenzie Jul 21 '20 at 10:19

1 Answers1

2

"cout" is not the problem.

I've initialised a matrix with your given values and i've read the values n=2 and m=9 from the keyboard:

int arr[n+10][m+10]{1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1};

and cout printed 1:7:3 (which you said, is supposed to be the right answer).

This means your problem is at reading the matrix.

Hoppo
  • 1,130
  • 1
  • 13
  • 32