Write the following function: const int MIN_SIZE = 2; const int MAX_SIZE = 8; co
ID: 3758339 • Letter: W
Question
Write the following function:
const int MIN_SIZE = 2;
const int MAX_SIZE = 8;
const int UNKNOWN = 0;
const int RED = 1;
const int BLUE = 2;
const char UNKNOWN_LETTER = '-';
const char RED_LETTER = 'X';
const char BLUE_LETTER = 'O';
const string UNKNOWN_STRING = "unknown";
const string RED_STRING = "X";
const string BLUE_STRING = "O";
/**
* 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);
I tried coding myself and Its not right. Below is my code.
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;
}
}
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
const int MIN_SIZE = 2;
const int MAX_SIZE = 8;
const int UNKNOWN = 0;
const int RED = 1;
const int BLUE = 2;
const char UNKNOWN_LETTER = '-';
const char RED_LETTER = 'X';
const char BLUE_LETTER = 'O';
const string UNKNOWN_STRING = "unknown";
const string RED_STRING = "X";
const string BLUE_STRING = "O";
bool cols_are_different(const int board[MAX_SIZE][MAX_SIZE],int size,int col1,int col2){
if (col1 > MAX_SIZE || col1 < MIN_SIZE || col2 > MAX_SIZE || col2 < MIN_SIZE)
return true;
for (int i = 0; i < size; i++){
if (board[i][col1] == board[i][col2])
return false;
}
return true;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.