Monday 27 March 2017

Array Range

QUESTION DESCRIPTION 

Write a program to find the range of the elements in the array.

Range of an array is the difference between the maximum and minimum element in an array,

Input and Output Format:

Input consists of n+1 integers where n corresponds to the number of elements in the array.

The first integer corresponds to n and the next n integers correspond to the elements in the array.

Output consists of an integer which corresponds to the range of the array.

Assume that the maximum number of elements in the array is 20.

Refer sample input and output for formatting specifications.

All text in bold corresponds to input and the rest corresponds to output.


TEST CASE 1

INPUT

5
1 2 4 3 5

OUTPUT

The range of the array is:4


EXPLANATION : 

The code requires you find the range of the elements of the array ( range : max element - min element )

ALGORITHM : 

  • Accept the number of elements from user ( n )
  • Accept the numbers from the user 
  • Check for the largest and smallest number ( arr[i] > max , arr[i] < min )
  • Calculate the range ( max-min )
  • Print the result with appropriate message

SOLUTION : (Please use this as reference only)

CODE


SYMMETRIC MATRIX

QUESTION DESCRIPTION

A symmetric matrix is a square matrix that is equal to its transpose.
Write a C program to find whether a given matrix is a square matrix or not.
Input Format:

The input consists of (m*n+2) integers. The first integer corresponds to m, the number of rows in the matrix and the second integer corresponds to n, the number of 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 10


TEST CASE 1

INPUT

3 3
1 2 3
4 5 6
7 8 9

OUTPUT

Not Symmetric


TEST CASE 2

INPUT

3 3
3 1 1
1 3 1
1 1 5

OUTPUT

Symmetric


EXPLANATION : 

The code requires you to check whether a given square matrix is a symmetric matrix or not Symmetric Matrix )

ALGORITHM : 

  • Accept the number of rows and columns from user ( n,m )
  • Check if array is a square matrix ( n==m )
  • Accept the elements of the array using scanf ( arr[][] )
  • Check if array elements are symmetric ( arr[i][j]==arr[j][i] )
  • If the condition fails , change flag to 0 and jump out of the 2 loops ( goto here )
  • If the array is not a square change flag to 0
  • After the loop check if flag is 0 or 1  ( flag==1 )
  • Print the appropriate result for the two cases

SOLUTION : (Please use this as reference only)

CODE

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)


Magic Square

QUESTION DESCRIPTION

A magic square is an arrangement of numbers (usually integers) in a square grid, where the numbers in each row, and in each column, and the numbers in the forward and backward main diagonals, all add up to the same number

Write a program to find whether a given matrix is a magic square 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 magic square. Print no if it is not a magic square 


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 magic square or notMagic Square )

ALGORITHM : 

  • Accept the number of rows and columns from user ( n )
  • Accept the elements of the array using scanf ( arr[][] )
  • Simultaneously calculate the sum of leading diagonal ( sum+=arr[i][i] , when i ==j
  • Calculate the sum of rows ( sum1+=arr[i][j] )
  • Calculate the sum of columns ( sum2+=arr[j][i] )
  • Calculate the sum of non leading diagonal ( sum+=arr[i][i] , when i+j+1 =n )
  • Check if all the sums are equal ( sum==sum3 and flag==1 )
  • Print the appropriate result

SOLUTION : (Please use this as reference only)