0

I have done Following code to count X in an array. Here the compliation error which i get.

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

int main() {
    int n;

    char s [n][n]  ;
    cin>>n; 
    char c ; 
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin >> c ;
            if(c=='X')
            {
                s[i][j]='X';
            }
        }

    } 

    int count=0; 

    for(int i=1;i<n-1;i++)
    {
        for(int j=1;j<n-1;j++)
        { 
            if( s[i][j]=='X' && s[i−1][j−1] =='X' && s[i−1][j+1]=='X'&& s[i+1][j−1] =='X' && s[i+1][j+1] =='X')      
                count++;
        }
    }

    cout<<count<<endl;
    return 0;
}

prog.cpp: In function ‘int main()’: prog.cpp:23:34: error: expected ‘:’ before ‘]’ token { if( s[i][j]=='X' && s[i?1][j?1] =='X' && s[i?1][j+1]=='X'&& s[i+1][j?1] =='X' && s[i+1][j+1] =='X') ^

Fureeish
  • 12,533
  • 4
  • 32
  • 62
  • 9
    In `s[i−1]`, that's not the right minus symbol. It should be `s[i-1]`. Same for the other instances of `i−1` in your code. The error message gives you a hint as it displays it as `s[i?1][j?1]`. – Blaze Feb 20 '19 at 12:23
  • 6
    `int n; char s [n][n]; cin>>n; ` does not do what you think it does. – Fureeish Feb 20 '19 at 12:23
  • 2
    Please compile with `-std=c++17 -Wall -Wextra -pedantic-errors` and don't use [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) or [``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), so that you're writing actual C++. – Quentin Feb 20 '19 at 12:26
  • 1
    ... and `char s[n][n];` is not standard C++, it's a gnu extension – Jabberwocky Feb 20 '19 at 12:31
  • did you copy the code from word? – SHR Feb 20 '19 at 12:37

1 Answers1

0

There's nothing wrong with the logical ANDs in the if-statement - well, apart from being unreadable. The problem is everywhere else tbh.

using namespace std;

Try to avoid using namespaces like that. This is so that you prevent ambiguity with name collisions.

char s[n][n];

This is not valid C++. It is known as a VLA. Read more about this in this answer here. Instead, use constexpr or a dynamic array such as std::vector.

int n;
char s[n][n];
cin>>n; 

Due to the fact that n is a local variable, it is defined using garbage values (193446 or -539646 or ...). This means that you may end up with a 2D array of negative spaces??? It's only after that n is being set to a number from the user input. Assuming that VLAs are not a problem, what you should do is the following:

int n = 0;
cin>>n; 
char s[n][n]; //still not valid C++

Furthermore, the 2D array is initialized with garbage values.

This, I have to admit, I do not understand. If the elements of the 2D array are only set when the user input is 'X', then what values will the rest of the array have?

cin >> c ;
if(c=='X')
{
   s[i][j]='X';
}

Did you just want to fill in the array with user input values? If so, then all you need is the following:

for(int i=0; i<n; i++)
    for(int j=0; j<n; j++)
        std::cin >> s[i][j];

And finally the program counts the X patterns where X needs to be at all four corners of another X. Apart from the fact that having long if-statements is a bad practise, the if-statement would return the correct result.

See a running version of your demo here: https://rextester.com/ASQ26945

Constantinos Glynos
  • 2,952
  • 2
  • 14
  • 32