There is a matrix, then I have to make a function to know that the value on the left diagonal and the right diagonal is the same.
const int n = 100;
int main(){
int n;
char a[n][n];
int dimL = 0;
int dimR = 0;
cout << "Input the dimension of the matrix: ";
cin >> n;
for( int i = 0; i < n; i++ )
for( int j = 0; j < n; j++ ){
cin >> a[i][j];
}
cout << "Matrix A is: " << endl;
for( int i = 0; i < n; i++ )
for( int j = 0; j < n; j++ ){
cout << a[i][j] << " ";
if( j == n - 1 )
cout << endl;
}
check(a,n,dimL,dimR);
if(check(a,n,dimL,dimR)){
cout << "YES";
}
else {
cout << "NO";
}
}
In the main is just the simple code to input array.
bool check(char a[n][n], int n, int &dimL, int &dimR ){
char left[n][n], right[n][n];
for( int i = 0; i < n; i++ )
for( int j = 0; j < n; j++ ){
if( i == j ){
left[dimL++] = a[i][j]; // left diagonal
}
if( i + j == n - 1 ){
right[dimR++] = a[i][j]; // right diagonal
}
}
for( int i = 0; i < n; i++ )
if( left[i] != right[i] )
return false;
return true;
}
and this is the function. Of course isn't work. it said expression must be modifiable lvalue. Because its 1D to 2D. can I convert 2D array to 1D array ?
some code I realize that are wrong. so I changed.
const int x = 100;
bool check(char a[x][x], int n, int &dimL, int &dimR ){
char left[x], right[x];
for( int i = 0; i < n; i++ )
for( int j = 0; j < n; j++ ){
if( i == j ){
left[dimL++] = a[i][j];
}
if( i + j == n - 1 ){
right[dimR++] = a[i][j];
}
}
for( int i = 0; i < n; i++ )
if( left[i] != right[i] )
return false;
return true;
}
It works. I tried to input array a a a, b b b, c c c, and the output is yes. just a little simple problem isn't solved, I can't see the left or right array.
if i try to put some code in my main.
if(int i = 0; i < n; i++ ){
cout << left[i];
}
It same shown that I need the expression that modifiable lvalue..
Thank you