I'm making a program in c++, to print a chessboard. The program was based on asking for an x number of columns and a y rows, with that the program prints a matrix with zeros representing black colors and 1 for whites, it works when I put lines different from columns, but when I put lines = columns it prints wrong.
If possible, could someone help me solve this?
by the way this is the code
obs (apologies for grammatical errors)
#include <iostream>
#include <math.h>
#include <locale.h>
#set size 4
usingnamespace std;
int main()
{
int row = 0;
int column = 0;
int aux = 1;
cout << "\nEnter column \n"<< endl;
cin >>column;
cout << "Enter the number of lines on the board:\n";
cin >>line;
while( line <= 0 || line >= 1000 )
{
cout << "\nVish will give an error, because the line must be greater than 0 and less than 1000 :)\n"<< endl;
cout << "\nenter the row number of the second array: ";
cin >> line;
}
while( column <= 0 || column >= 1000 )
{
cout << "\nVish will give an error, because the column must be greater than 0 and less than 1000 :)\n"<< endl;
cout << "\nenter the column number of the second matrix: ";
cin >> column;
}
int board[row][column];
cout << "NOTE: 1 is for WHITE colors and 0 is for Black"<< endl;
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
board[i][j] = aux;
aux = 1 - aux;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << board[i][j];
}
cout <<"\n";
}
if(board[row-1][column-1] == 0){
cout <<"\nBottom right box of color is = black ";
}
elseif(board[row-1][column-1] == 1){
cout <<"\nBottom right house of color is = white ";
}
}
#include <iostream>
#include <math.h>
#include <locale.h>
#set size 4
usingnamespace std;
int main() {
int row = 0;
int column = 0;
int aux = 1;
cout << "\nEnter column \n" << endl;
cin >> column;
cout << "Enter the number of lines on the board:\n";
cin >> line;
while (line <= 0 || line >= 1000) {
cout << "\nVish will give an error, because the line must be greater than 0 and less than 1000 :)\n" << endl;
cout << "\nenter the row number of the second array: ";
cin >> line;
}
while (column <= 0 || column >= 1000) {
cout << "\nVish will give an error, because the column must be greater than 0 and less than 1000 :)\n" << endl;
cout << "\nenter the column number of the second matrix: ";
cin >> column;
}
int board[row][column];
cout << "NOTE: 1 is for WHITE colors and 0 is for Black" << endl;
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
board[i][j] = aux;
aux = 1 - aux;
}
}
for (int i = 0; i < line; i++) {
for (int j = 0; j < column; j++) {
cout << board[i][j];
}
cout << "\n";
}
if (board[row - 1][column - 1] == 0) {
cout << "\nBottom right box of color is = black ";
} elseif (board[row - 1][column - 1] == 1) {
cout << "\nBottom right house of color is = white ";
}
}
Note that the if on L66 original is not needed as if the condition fails on L61 then the colour must be white.
line 44:
array index must be a constant:
int x = 10;
double foo[x]; //error: x is not a compile time constant.
some compilers support the above. That is an extension to the c++ language, though.
I think I still don't understand what you're talking about, as I'm a beginner in c++, but I think I found a way to solve this, I was watching here and there is a constant when column = row it will always be white. I'm thinking about putting one
if (row=column)cout << "is white"...,
but how can i do to print the array?