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
SOLUTION : (Please use this as reference only)
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 not ( Magic 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)
No comments:
Post a Comment