answersLogoWhite

0


Best Answer

int sym_test (const int **a, int n) {
int i, j, sym;

i=1, j=0, sym=1;
while (sym && iif (a[i][j] != a[j][i]) sym= 0;
else if (jelse ++i, j=0;

}
return sym;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

#include

#include

//write matrix

void write_mat(int a[][10],int n)

{

int i,j;

{

for(i=0;i

for(j=0;j

printf("%10d",a[i][j]);

printf("\n");

}

}

//read matrix

void read_mat(int a[][10],int n)

{

int i,j;

printf("Enter %d X %d matrix below:\n",n,n);

for(i=0;i

for(j=0;j

scanf("%d",&a[i][j]);

}

//main function

void main()

{

int a[10][10],i,j,n;

//accept matrix

printf("Enter size of square matrix");

scanf("%d",&n);

read_mat(a,n);

//check if the matrix is symmetric or not

for(i=0;i

for(j=i+1;j

if(a[i][j]!=a[j][i])

{

printf("Not symmetric");

return;

}

printf("Symmetric");getch();}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

There are four ways to test for matrix symmetry: across the horizontal axis; across the vertical axis; across the main diagonal; and across the anti-diagonal.

Symmetry across the horizontal axis means the top half of the matrix must mirror the bottom half, just as if we'd placed a mirror on the middle row (rows being horizontal in a matrix).

Symmetry across the vertical axis means the left half of the matrix must mirror the right half. Again, just as if we'd placed a mirror on the centre column (columns being vertical in a matrix).

Symmetry across the main diagonal means the lower left portion of the matrix must mirror the upper right portion, just as if we'd placed a mirror along the main diagonal (running from top-left to bottom-right).

Symmetry across the anti diagonal means the upper left portion of the matrix must mirror the lower right portion, just as if we'd placed a mirror along the anti-diagonal (running from bottom-left to top-right).

In order to test for all four we need four functions, each of which returns true or false for a given matrix.

Full symmetry is achieved when both vertical and horizontal symmetry functions return true. There is no need to test for diagonal symmetries when horizontal and vertical symmetry are both true since it can be implied all four must be true.

Partial symmetry is achieved when any one (and only one) of the four functions returns true.

Non-symmetry is achieved when all four return false.

The following program demonstrates how to implement the four functions, testing the results with a fully-symmetric matrix and then modifying it to test for each individual case as well as non-symmetry. The program asserts that all four functions operate correctly.

A fifth function is also included to ensure the matrix is perfectly square. This is only necessary because the matrix is implemented as a vector of vectors, all of which are variable length. You could eliminate the need for this test by implementing your matrix as a class using an embedded array of arrays (std::array) such that the container is guaranteed to be a perfect square. The 4 functions could then be implemented as member functions.

#include<iostream>

#include<vector>

#include<assert.h>

// Forward declarations:

bool is_square (const std::vector<std::vector<int>>&);

bool is_horizontally_symmetric (const std::vector<std::vector<int>>&);

bool is_vertically_symmetric (const std::vector<std::vector<int>>&);

bool is_main_diagonally_symmetric (const std::vector<std::vector<int>>&);

bool is_anti_diagonally_symmetric (const std::vector<std::vector<int>>&);

int main()

{

std::vector<std::vector<int>> m1;

// Create fully symmetric matrix:

m1.push_back (std::vector<int>{1,2,3,2,1});

m1.push_back (std::vector<int>{2,3,4,3,2});

m1.push_back (std::vector<int>{3,4,5,4,3});

m1.push_back (std::vector<int>{2,3,4,3,2});

m1.push_back (std::vector<int>{1,2,3,2,1});

// Assert validity:

assert (is_horizontally_symmetric (m1));

assert (is_vertically_symmetric (m1));

assert (is_main_diagonally_symmetric (m1)); // non-essential test

assert (is_anti_diagonally_symmetric (m1)); // non-essential test

// Convert to horizontally symmetric and validate:

m1[1][0] = 5;

m1[3][0] = 5;

assert (is_horizontally_symmetric (m1));

assert (!is_vertically_symmetric (m1));

assert (!is_main_diagonally_symmetric (m1));

assert (!is_anti_diagonally_symmetric (m1));

// Restore:

m1[1][0] = 2;

m1[3][0] = 2;

// Convert to vertically symmetric and validate:

m1[0][1] = 5;

m1[0][3] = 5;

assert (!is_horizontally_symmetric (m1));

assert (is_vertically_symmetric (m1));

assert (!is_main_diagonally_symmetric (m1));

assert (!is_anti_diagonally_symmetric (m1));

// Restore:

m1[0][1] = 2;

m1[0][3] = 2;

// Convert to main diagonally symmetric and validate:

m1[1][4] = 5;

m1[4][1] = 5;

assert (!is_horizontally_symmetric (m1));

assert (!is_vertically_symmetric (m1));

assert (is_main_diagonally_symmetric (m1));

assert (!is_anti_diagonally_symmetric (m1));

// Restore:

m1[1][4] = 2;

m1[4][1] = 2;

// Convert to anti diagonally symmetric and validate:

m1[0][1] = 5;

m1[3][4] = 5;

assert (!is_horizontally_symmetric (m1));

assert (!is_vertically_symmetric (m1));

assert (!is_main_diagonally_symmetric (m1));

assert (is_anti_diagonally_symmetric (m1));

// Restore:

m1[0][1] = 2;

m1[3][4] = 2;

// Convert to non-symmetric:

m1[0][3] = 4;

m1[0][4] = 5;

assert (!is_horizontally_symmetric (m1));

assert (!is_vertically_symmetric (m1));

assert (!is_main_diagonally_symmetric (m1));

assert (!is_anti_diagonally_symmetric (m1));

}

// Returns true if the given matrix is square (every row has

// the same number of columns as there are rows). Returns

// false if the matrix is empty or only has one row.

bool is_square (const std::vector<std::vector<int>>& matrix)

{

if (!matrix.size() matrix.size() == 1)

return false;

for (size_t r=0; r<matrix.size(); ++r)

if (matrix[r].size() != matrix.size())

return false;

return true;

}

// Returns true if every column is symmetric.

bool is_horizontally_symmetric (const std::vector<std::vector<int>>& matrix)

{

if (!is_square(matrix))

return false;

for (size_t c=0; c<matrix.size(); ++c)

for (size_t r=0; r<matrix.size() / 2 + 1; ++r)

if (matrix[r][c]!=matrix[matrix.size()-(r+1)][c])

return false;

return true;

}

// Returns true if every row is symmetric.

bool is_vertically_symmetric (const std::vector<std::vector<int>>& matrix)

{

if (!is_square(matrix))

return false;

for (size_t r=0; r<matrix.size(); ++r)

for (size_t c=0; c<matrix.size() / 2 + 1; ++c)

if (matrix[r][c]!=matrix[r][matrix.size()-(c+1)])

return false;

return true;

}

// Returns true if the matrix is symmetrical across the main diagonal.

bool is_main_diagonally_symmetric (const std::vector<std::vector<int>>& matrix)

{

if (!is_square(matrix))

return false;

for (size_t x=0; x<matrix.size(); ++x)

for (size_t y=0; y<x; ++y)

if (matrix[x][y]!=matrix[y][x])

return false;

return true;

}

// Returns true if the matrix is symmetrical across the anti-diagonal.

bool is_anti_diagonally_symmetric (const std::vector<std::vector<int>>& matrix)

{

if (!is_square(matrix))

return false;

for (size_t i=0, m=matrix.size()-1; i<matrix.size(); ++i, --m)

for (size_t j=0, n=matrix.size()-1; j<matrix.size(); ++j, --n)

if (matrix[i][n]!=matrix[j][m])

return false;

return true;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program using c plus plus to whether the given square matrix is symmetric or not?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is a skew symmetric matrix?

A skew symmetric matrix is a square matrix which satisfy, Aij=-Aji or A=-At


What is a symmetric matrix?

a square matrix that is equal to its transpose


What is the definition of a symmetric matrix?

Symmetric Matrix:Given a square matrix A such that A'=A, where A' is the transpose of A, then A is a symmetric matrix.note: No need to think about diagonal elements, they can be anything.


Is null square matrix a skew symmetric?

yes, it is both symmetric as well as skew symmetric


What is the definition of a matrix?

Symmetric Matrix:Given a square matrix A such that A'=A, where A' is the transpose of A, then A is a symmetric matrix.note: No need to think about diagonal elements, they can be anything.


what is the definition of The Matrix?

Symmetric Matrix:Given a square matrix A such that A'=A, where A' is the transpose of A, then A is a symmetric matrix.note: No need to think about diagonal elements, they can be anything.


What is the symbol for skew?

In linear algebra, a skew-symmetric matrix is a square matrix .....'A'


How do you write a program in c plus plus to check if an array is symmetric?

To determine if an array is symmetric, the array must be square. If so, check each element against its transpose. If all elements are equal, the array is symmetric.For a two-dimensional array (a matrix) of order n, the following code will determine if it is symmetric or not:templatebool symmetric(const std::array& matrix){for (size_t r=0 ; r


Write a program using c plus plus to check whether the given square matrix is symmetric or not?

means whether the matrix is same or not program for symmetric matrix : include&lt;stdio.h&gt; #include&lt;conio.h&gt; main() { int a[10][10],at[10][10],k,i,j,m,n; clrscr(); printf("enter the order of matrix"); scanf("%d %d",&amp;m,&amp;n); printf("enter the matrix"); for(i=0;i&lt;m;i++) { for(j=0;j&lt;n;j++) scanf("%d",&amp;a[i][j]); } for(i=0;i&lt;m;i++) { for(j=0;j&lt;n;j++) at[i][j]=a[j][i]; } for(i=0;i&lt;m;i++) { for(j=0;j&lt;n;j++) { if(at[i][j]!=a[i][j]) k=1; } } if(k==1) printf("not symmetric"); else printf("symmetric"); getch(); }


What type of matrix is a vector?

Vector matrix has both size and direction. There are different types of matrix namely the scalar matrix, the symmetric matrix, the square matrix and the column matrix.


Can a zero matrix be a skew symmetric matrix?

My knowledge limits to square matrices. The answer is yes, because 0 = -0


What is the definition of an anti-symmetric matrix?

The Definition of an Anti-Symmetric Matrix:If a square matrix, A, is equal to its negative transpose, -A', then A is an anti-symmetric matrix.Notes:1. All diagonal elements of A must be zero.2. The cross elements of A must have the same magnitude, but opposite sign.