2D Array Chessboard Pattern | C++

austinleath

February 18th, 2021 03:14:03 PM

					
#include<iostream> using namespace std; const int rows = 8; const int cols = 8; char chessboard[rows][cols]; void setBoard(char chessboard[][cols]); void printBoard(char chessboard[][cols]); void setBoard(char chessboard[][cols]) { for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { if(i % 2 == 0 && j % 2 == 0) { chessboard[i][j] = 'x'; } else { if(i % 2 != 0 && j % 2 == 1) { chessboard[i][j] = 'x'; } else { chessboard[i][j] = '-'; } } } } return; } void printBoard(char chessboard[][cols]) { for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) { cout << chessboard[i][j] << " "; } cout << endl; } return; } int main(int argc, char const *argv[]) { setBoard(chessboard); printBoard(chessboard); return 0; }

Featured Posts