Below is a 4 part question of my intro free write review. Please write the follo
ID: 3758510 • Letter: B
Question
Below is a 4 part question of my intro free write review. Please write the following:
Part A)
/**
* Requires: size <= MAX_SIZE and size is a positive even integer.
* Modifies: Nothing.
* Effects : Returns true if and only if no row or column of the board contains
* three or more consecutive red or blue squares.
* Used In : board_is_valid()
*/
bool board_has_no_threes(const int board[MAX_SIZE][MAX_SIZE], int size);
Part B)
/**
* Requires: size <= MAX_SIZE and size is a positive even integer,
* 0 <= row1 && row1 < size, 0 <= row2 && row2 < size.
* Modifies: Nothing.
* Effects : Returns true if either
* * row1 or row2 contains an UNKNOWN, or
* * row1 and row2 have different squares.
*
* Example: rows_are_different should return false for rows 0 and 1:
* XOOX
* XOOX
* ----
* ----
*
* Example: rows_are_different should return true for rows 0 and 1:
* XOOX
* XXOO
* ----
* ----
*
* Example: rows_are_different should return true for rows 0 and 1:
* XO-X
* XOOX
* ----
* ----
*
* Example: rows_are_different should return true for rows 3 and 4:
* XO-X
* XOOX
* ----
* ----
*
* Example: rows_are_different should return true for rows 0 and 1:
* XO-X
* XO-X
* ----
* ----
*
* Used In : board_has_no_duplicates()
*/
bool rows_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size,
int row1,
int row2);
Part C)
/**
* Requires: size <= MAX_SIZE and size is a positive even integer,
* 0 <= col1 && col1 < size, 0 <= col2 && col2 < size.
* Modifies: Nothing.
* Effects : Returns true if either
* * col1 or col2 contains an UNKNOWN, or
* * col1 and col2 have different squares.
*
* Example: columns_are_different should return false for columns 0
* and 1:
* XX--
* OO--
* OO--
* XX--
*
* Example: columns_are_different should return true for columns 0
* and 1:
* XX--
* OX--
* OO--
* XO--
*
* Example: columns_are_different should return true for columns 0
* and 1:
* XX--
* OO--
* -O--
* XX--
*
* Example: columns_are_different should return true for columns 3
* and 4:
* XX--
* OO--
* -O--
* XX--
*
* Example: columns_are_different should return true for columns 0
* and 1:
* XX--
* OO--
* ----
* XX--
*
* Used In : board_has_no_duplicates()
*/
bool cols_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size,
int col1,
int col2);
Part D)
/**
* Requires: size <= MAX_SIZE and size is a positive even integer.
* Modifies: Nothing.
* Effects : Returns true if and only if no two rows or columns of the board
* are identical.
* Used In : board_is_valid()
*/
bool board_has_no_duplicates(const int board[MAX_SIZE][MAX_SIZE], int size);
---------------------------------------------------------------------------------------------------------------------------
The resource below will give more info.
Utility.h
http://pastebin.com/3aYXfPCx
Driver.h
http://pastebin.com/zHXcvnwA
Main.cpp
http://pastebin.com/3jyPuibc
-------------------------------------------------------------------------------------
//This is my incorrect attempt below, Please help, I am getting wrong/mixmatch output when submitting and I do not know what I did wrong.//
bool board_has_no_threes(const int board[MAX_SIZE][MAX_SIZE], int size) {
for(int row = 0; row < size; row++){
for(int col = 0; col < size; col++){
int color = board[row][col];
if(row_has_no_threes_of_color(board, size, row, color)
&& col_has_no_threes_of_color(board, size, col, color)) {
return true;
}
}
}
return false;
}
bool rows_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size,
int row1,
int row2) {
int count = 0;
for(int i = 0; i < size; i++) {
if((board[row1][i] != 1 && board[row1][i] != 2 ) || (board[row2][i] != 1 && board[row2][i] != 2)) {
return true;
}
if(board[row1][i] == board[row2][i]){
count++;
}
}
if(count <= size/2) {
return true;
}
else {
return false;
}
}
bool cols_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size,
int col1,
int col2) {
int count = 0;
for(int i = 0; i < size; i++) {
if((board[i][col1] != 1 && board[i][col1] != 2 ) || (board[i][col2] != 1 && board[i][col2] != 2)) {
return true;
}
if(board[i][col1] == board[i][col2]){
count++;
}
}
if(count <= size/2) {
return true;
}
else {
return false;
}
}
bool board_has_no_duplicates(const int board[MAX_SIZE][MAX_SIZE], int size) {
for(int row1 = 0; row1 < size - 1; row1++){
for(int row2 = row1 + 1; row2 < size; row2++ ){
if(!rows_are_different(board, size,row1, row2)) {
return false;
}
}
}
for(int col1 = 0; col1 < size - 1; col1++){
for(int col2 = col1 + 1; col2 < size; col2++ ){
if(!cols_are_different(board, size, col1, col2)) {
return false;
}
}
}
return true;
}
Explanation / Answer
bool board_has_no_threes(const int board[MAX_SIZE][MAX_SIZE], int size) {
if(size > MAX_SIZE || size < 0){
//invalid size
return false;
}
for(int row = 0; row < size; row++){
if(!row_has_no_threes_of_color(board, size, row, 1) || !row_has_no_threes_of_color(board, size, row, 2)){
return false;
}
}
for(int col = 0; col < size; col++){
if(!col_has_no_threes_of_color(board, size, col, 1) || !col_has_no_threes_of_color(board, size, col, 2)){
return false;
}
}
return true;
}
bool rows_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size,
int row1,
int row2) {
if(size > MAX_SIZE || !((0 <= row1 && row1 < size) && (0 <= row2 && row2 < size))){
//invalid arguments
return false;
}
bool equals = true;
for(int k = 0; k < MAX_SIZE; ++k){
if(board[row1][k] != board[row2][k] || ((board[row1][k] != 1 && board[row1][k] != 2) || (board[row2][k] != 1 && board[row2][k] != 2))){
equals = false;
break;
}
}
if(equals){
return false;
}
else{
return true;
}
}
bool cols_are_different(const int board[MAX_SIZE][MAX_SIZE],
int size,
int col1,
int col2) {
if(size > MAX_SIZE || !((0 <= col1 && col1 < size) && (0 <= col2 && col2 < size))){
//invalid arguments
return false;
}
bool equals = true;
for(int k = 0; k < MAX_SIZE; ++k){
if(board[k][col1] != board[k][col2] || ((board[k][col1] != 1 && board[k][col1] != 2) || (board[k][col2] != 1 && board[k][col2] != 2))){
equals = false;
break;
}
}
if(equals){
return false;
}
else{
return true;
}
}
bool board_has_no_duplicates(const int board[MAX_SIZE][MAX_SIZE], int size) {
if(size > MAX_SIZE || size < 0){
//invalid size
return false;
}
for(int i = 0; i < size; ++i){
for(int j = 0; j < size; ++j){
if(i != j){
if(!cols_are_different(board, size, i, j) || !rows_are_different(board, size, i, j)){
return false;
}
}
}
}
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.