Monday 27 March 2017

UNIFORMITY MATRIX

QUESTION DESCRIPTION

Uniformity matrix is a matrix in which all the elements in the matrix are either completely even or completely odd.

Write a C program to find whether a given matrix is a uniformity matrix or not.

Input Format:

The input consists of (n*n+1) integers. The first integer corresponds to the number of rows/columns in the matrix. The remaining integers correspond to the elements in the matrix. The elements are read in rowwise order, first row first, then second row and so on. Assume that the maximum value of m and n is 5.

Output Format:

Print yes if it is a uniformity matrix. Print no if it is not a uniformity matrix.


TEST CASE 1

INPUT

2
4 5
5 4

OUTPUT

no


EXPLANATION : 

The code requires you to check whether a given square matrix is a uniformity matrix or not ( Uniformity matrix is a matrix in which all the elements in the matrix are either completely even or completely odd )

ALGORITHM : 

  • Accept the number of rows and columns from user ( n )
  • Accept the elements of the array using scanf ( arr[][] )
  • Simultaneously calculate if the number being input is even ( arr[i][j]%2==0 )
  • If the number is even increase counter by 1 ( cnt++ )
  • After the loop check if counter is equal to array size ( if all elements are even ) or equal to zero ( if all elements are odd ) ( cnt==n*n||cnt==0 )
  • Print the appropriate result for the two cases

SOLUTION : (Please use this as reference only)


No comments:

Post a Comment