Tuesday, 7 August 2018

Data Structures and Algorithms- Assignment :A7



Title:

A magic square is an n * n matrix of the integers 1 to n2 such that the sum of each row, column, and diagonal is the same. The figure given below is an example of magic square for case n=5. In this example, the common sum is 65. Write C/C++ Program for magic square

Program:



#include<iostream>
using namespace std;
class magic_square
{
            int a[10][10];
            int n;
public:
            magic_square()
            {
                        cout<<"\nEnter n : ";
                        cin>>n;
            }
            void get()
            {
                        cout<<"\nEnter the elements : ";
                        for(int i=0 ; i<n ; i++)
                        {
                                    for(int j=0 ; j<n ; j++)
                                    {
                                                cin>>a[i][j];
                                    }
                        }
            }
            bool row()
            {
                        int sum[10];
                        for(int i=0 ; i<n ; i++)
                        {
                                    sum[i]=0;
                        }
                        for(int i=0 ; i<n ; i++)
                        {
                                    for(int j=0 ; j<n ; j++)
                                    {
                                                sum[i]=sum[i]+a[i][j];
                                    }
                        }
                        for(int i=0 ; i<n ; i++)
                        {
                                    if(sum[0]!=sum[i])
                                                return false;
                        }
                        return true;
            }

            bool column()
            {
                        int sum[10];
                        for(int i=0 ; i<n ; i++)
                        {
                                    sum[i]=0;
                        }
                        for(int i=0 ; i<n ; i++)
                        {
                                    for(int j=0 ; j<n ; j++)
                                    {
                                                sum[i]=sum[i]+a[j][i];
                                    }
                        }
                        for(int i=0 ; i<n ; i++)
                        {
                                    if(sum[0]!=sum[i])
                                                return false;

                        }
                        return true;
            }



            bool diagnol()
            {
                        int d1,d2;
                        d1=d2=0;
                        for(int i=0 ; i<n ; i++)
                        {
                                    for(int j=0 ; j<n ; j++)
                                    {
                                                if(i==j)
                                                            d1=d1+a[i][j];
                                                if(i+j==n-1)
                                                            d2=d2+a[i][j];
                                    }
                        }

                        if(d1!=d2)
                                    return false;
                        return true;
            }
};
int main()
{
            magic_square m;
            m.get();
            if(m.diagnol() && m.row() && m.column())
                        cout<<"\nIt is a magic matrix !!";
            else
                        cout<<"\nSoory !! It is not a magic matrix";
            return 0;
}



Output:



Enter n : 5

Enter the elements : 15
8
1
24
17
16
14
7
5
23
22
20
13
6
4
3
21
19
12
10
9
2
25
18
11

It is a magic matrix !!


No comments:

Post a Comment

Stack and Queue as ADT in C++

Stack as ADT The functions associated with stack are: empty()  – Returns whether the stack is empty size()  – Returns the size o...