Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: A Boolean matrix has the parity property when each row and each col

ID: 3555782 • Letter: D

Question

Description:
A Boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here's a 4 x 4 matrix which has the parity property:
The sums of the rows are 2, 0, 4 and 2. The sums of the columns are 2, 2, 2 and 2. In this homework, you are asked to write a program that reads in a matrix to a multiple-subscripted array and checks if it has the parity property.
Input:
The first line of each test case contains one integer n (n<100), representing the size of the matrix. On the next n lines, there will be n integers per line. No other integers than 0 and 1 will occur in the matrix.
Output:
For the matrix in the input, print one line. If the matrix already has the parity property, print "Yes", print "No" otherwise.
Sample Input I:
4
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1
Sample Output I:
Yes
Sample Input II:
4
1 0 1 0
0 1 1 0
1 1 1 1
0 1 0 1
Sample Output II:
No

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int **matrix;
int no_of_rows;
cout <<"enter no of rows :";
cin >> no_of_rows;
cout << endl;
matrix = new int*[no_of_rows];
for(int i=0; i<no_of_rows; i++)
matrix[i] = new int[no_of_rows];
for(int i=0; i<no_of_rows; i++)
for(int j=0; j<no_of_rows; j++)
cin >> matrix[i][j];
bool row_bol = true;
bool col_bol = true;
int col_sum = 0;
int row_sum = 0;
for(int i=0; i<no_of_rows; i++)
{
    row_sum = 0;
    col_sum = 0;
   for(int j=0; j<no_of_rows; j++)
   {
   if(matrix[i][j]!=0 && matrix[i][j]!=1) { row_bol = false; break; }
   row_sum = row_sum + matrix[i][j];
   col_sum = col_sum + matrix[j][i];
   }
   if(!row_bol) break;
   if(row_sum%2!=0) { row_bol = false; break; }
   if(col_sum%2!=0) { row_bol = false; break; }
}
if(row_bol)
cout <<"Yes " << endl;
else
cout <<"No " << endl;
return 0;
}