C and C++ library of functions and procedures for matrix (bidimensional arrays) – Comparison functions

No comments - This post in romanian

The comparison functions used for matrix processing are needed for finding more information about a matrix or group of matrices from the point of view of: the matrix type, its elements type or the apparition number of a key in the matrix.

The function symmet checks if a matrix is a square or a symmetrical one. It gets as entry parameters the matrix’ lines and columns numbers, which are n and m, and the matrix X. The matrix is defined static. The function returns the address of an integer variable, *er, where it writes 1 if the matrix is symmetrical or 0 if the matrix isn’t symmetrical. Also it verifies if the matrix is a square one and it writes 0 at the integer variable’ address if the condition isn’t fulfilled.

The function is:

void symmet(int n,int m,int x[30][30],int *er)
{
int i,j;
			//checks if the matrix is a square one
if(n!=m) printf("\n The matrix must be a square !");   
else
   	{
	 *er=1;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		if(i<j) if(x[i][j]!=x[j][i]) *er=0;
	if (*er==0) printf("\n The matrix is not symmetrical!");
	    else printf("\n The matrix is symmetrical!");
	}
}

The function check_unit verifies if a matrix is or not the unit matrix. Its parameters are the variables m and n, which are representing the dimensions of the static defined matrix X, and the address of an integer variable, *er. If the matrix isn’t the unit matrix or if isn’t square the variable er takes the value 0. In opposite case the variable takes the value 1.

The function is:

void check_unit(int n,int m,int x[30][30],int *er)
{
int i,j;
			// checks if the matrix is a square one
if(n!=m) printf("\n The matrix must be a square !");
   else
   	{
	 *er=1;
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		if(x[i][j]!=1) *er=0;
	if (*er==0) printf("\n The matrix is not a unit matrix");
	    else printf("\n The matrix is a unit matrix");
	}
}

The function check_no verifies if a user’ number is or not in the matrix. The function’ parameters are the variables n and m, representing the matrix dimensions, and the matrix X, which is defined static. The search result is known by using the integer variable eror, which address is also entry parameter of the function. The variable error has the value 1 if the number is found inside the matrix and the value 0 otherwise. The number to be found is read inside the function.

The function is:

void check_no(int n,int m,int x[30][30],int *eror)
{
int i,j,k,z;
// the number to be searched for is read from the keyboard
printf("\n The searched number is::");
*eror=0;
k=scanf("%d",&z);
//the number’ validation 
valid_no_integer(&z,k);
 
for(i=0;i<n;i++)
   for(j=0;j<m;j++)
       if(x[i][j]==z)
       {
// the coordinates of the number are displayed (the line and the column)
       printf("\n Searched number is on line %d and column %d ",i,j);
       *eror=1;
       }
if(*eror==0) printf("\n The number is not in matrix!");
}

The function check_line_column gets as entry parameters the dimensions of a static defined matrix, the matrix and the address of an integer variable. All those variables are: n for the lines number, m for the columns number, X for the matrix, and int* eror for the address. The function gets from the keyboard an array and checks if that array is representing or not a matrix column. If this is true the variable takes the value 1 and if is false takes the value 0.

When the array’ elements are read, they are validated by the function valid_no_integer.

The function is:

void check_line_column(int n,int m,int x[30][30],int *eror)
{
int i,j,h[30],k;
//the array is created from the keyboard
printf("\n The line to be checked is :");
//its dimension
for(i=0;i<n;i++)
   {
   printf("\n The element h[%d] is:",i);
   k=scanf("%d",&h[i]);
   valid_no_integer(&h[i],k);
   }
//it is verified if the array is column in the matrix
for(j=0;j<m;j++)
   {
   *eror=1;
   for(i=0;i<n;i++)
      if(x[i][j]!=h[i]) *eror=0;
   if(*eror==1)
	{
	printf("\n The line is the column  %d",j);
	k=2;
	}
    }
if(k!=2) printf ("\n The line doesn't represent a column in the matrix");
}

The function check_line_line verifies if an array created from the console is representing a matrix line. This function parameters are: n representing the matrix lines number, m representing the matrix columns number, X representing the static defined matrix and int*eror representing the address of an integer variable. If the array is a line of the matrix then the variable eror takes the value 1. Else it takes the value 0.

When the array’ components are read from the keyboard, they are validated by the function valid_no_integer.

The function is:

void check_line_line(int n,int m,int x[30][30],int *eror)
{
int i,j,h[30],k;
//the array is created 
printf("\n The line to be checked is:");
//the array dimension is m
for(j=0;j<m;j++)
   {
   printf("\n The element h[%d] is:",j);
   k=scanf("%d",&h[j]);
   valid_no_integer(&h[j],k);
   }
//it is verified if the array is representing a matrix line
for(i=0;i<n;i++)
   {
   *eror=1;
   for(j=0;j<m;j++)
      if(x[i][j]!=h[j]) *eror=0;
   if(*eror==1)
	{
	printf("\n The line is the line  %d",i);
	k=2;
	}
    }
if(k!=2) printf ("\n The line doesn't represent a line in the matrix");
}

The following two functions check_columns and check_lines verify if a matrix has or not lines or columns which are equal between them. Both functions have the next parameters: the matrix lines number, n, the matrix columns number, m, the static defined matrix, X, and the address of the integer variable eror, int* eror. The result of this search is given with the help of the variable eror. In case the matrix has equal lines or columns, the variable takes the value 1. Else it takes the value 0.

The basic principle of these two functions is that every line or column is compared with all the others, and when are found two lines or columns equal they are displayed.

The functions are:

void check_columns(int n,int m,int x[30][30],int *eror)
{
int i,j,k,l;
*eror=1;
l=0;
// every column is selected
for(j=0;j<m-1;j++)
   {
   *eror=1;
//the columns is compared with the rest
   for(k=j+1;k<m;k++)
       {
       *eror=1;
       for(i=0;i<n;i++)
	     if(x[i][j]!=x[i][k]) *eror=0;
       if(*eror==1)
		{
		l=1;
//equal columns are displayed
		printf("\n Column %d is equal with column  %d ",j,k);
		}
       }
    }
if(l==0) printf("\n The matrix has no equal columns");
}

And the second function is:

void check_lines(int n,int m,int x[30][30],int *eror)
{
int i,j,k,l;
*eror=1;
l=0;
//each line is selected
for(i=0;i<n-1;i++)
   {
   *eror=1;
// the line is compared with the rest
   for(k=i+1;k<n;k++)
       {
       *eror=1;
       for(j=0;j<m;j++)
	     if(x[i][j]!=x[k][j]) *eror=0;
       if(*eror==1)
		{
		l=1;
// equal lines are displayed
		printf("\n Line  %d is equal with line  %d ",i,k);
		}
       }
    }
if(l==0) printf("\n The matrix has no equal lines");
}

The function key_appar_no has the same parameters as the previous described functions. It gets a number from the keyboard and it checks if that number is in the matrix X. Also it counts that number apparitions and it writes the final value at the integer variable’ address.

Using the function valid_no_integer validates the number read from the console.

The function is:

void key_appar_no(int n,int m,int x[30][30],int *v)
{
int i,j,k,c,l;
clrscr();
//the number is read and validated
printf("\n Type the key:");
l=scanf("%d",&c);
valid_no_integer(&c,l);
 
k=0;
//its apparitions in the matrix are counted
for (i=0;(i<n);i++)
  for (j=0;j<m;j++)
	     if (x[i][j]==c)  k++;
if (k==0)  printf ("\nThe key does not appear in the matrix!");
	 else
	 {
	 *v=k;//the repetition number is written 
	 printf("\n The key appears in the matrix for %d times",*v);
	 }
getch();
}

The next two functions key_appar_no_line and key_appar_no_column check how many times a number given by the user appears in a line of the matrix or in a column, also indicated by the user. These functions parameters are: the matrix lines number, n, the matrix columns number, m, the static defined matrix, X, and the address of the integer variable v, int* v. The result of this search is given with the help of the variable v.

The number and the column or the line in which the search took place, are read by the function from the keyboard. If the number isn’t in the line or the column the variable v takes the value 0.

The functions are:

void key_appar_no_line(int n,int m,int a[30][30],int *v)
{
int i,j,k,c,q,l;
clrscr();
//the number is read and validated
printf("\n Type the key:");
l=scanf("%d",&c);
valid_no_integer(&c,l);
 
// the line in which the number is searched
printf("\n Type the line :\t");
scanf("%d",&q);
k=0;
//the number’ apparitions are counted 
for (j=0;j<m;j++)
	      if (a[q][j]==c)  k++;
if (k==0)
	{
	printf ("The key %d does not appear in the line %d!",c,q);
	*v=k;//apparition number is saved
	}
    else
	{
	*v=k; // apparition number is saved
	printf ("The key %d does appear in the line %d for %d times",c,q,*v);
	}
}

and the second one:

void key_appar_no_column(int n,int m,int a[30][30],int *v)
{
int i,j,k,c,q,l;
clrscr();
 
// the number is read and validated
printf("Type the key:");
scanf("%d",&c);
valid_no_integer(&c,l);
 
 
// the column in which the number is searched, is indicated
printf("\n Type the column:\t");
scanf("%d",&q);
k=0;
// the number’ apparitions are counted
for (i=0;i<n;i++)
	      if (a[i][q]==c)  k++;
if (k==0)
   {
   printf ("The key %d does not appear in the column %d!",c,q);
   *v=k; // apparition number is saved
   }
  else
   {
   *v=k; // apparition number is saved
   printf ("The key %d does appear in the column %d for %d times",c,q,*v);
   }
}

Besides these functions from the library matrix.h, inside the class matrix have been overloaded the following operators: % and = =, for comparing objects of type matrix. The operator %
does the same thing as the function symmet, which is checking if a matrix is a symmetrical one.

The operator = = sees if two objects of type matrix are equal, that is, if two matrices are equal. Its parameter is the address of the comparing object and it returns 0 if the objects are not equal or 1 if they are equal.

This function code sequence is:

int matrix::operator ==(matrix &b)
{
int k=0;
//the objects dimensions are verified first
if ((n==b.n)&&(m==b.m))
			   {
		//if they have the same dimensions, their  elements are compared
			for(i=0;i<n;i++)
			     for(j=0;j<m;j++) if (a[i][j]!=b.a[i][j]) k=1;
			 if (k==1) return 0;//the objects are different
			      else  return 1;//the objects are equal
			    }
	  else return 0;
}

The functions from this chapter, which are from the library matrice.h, are present in the class matrix too, but they don’t have any parameters and they return integer values.

Their headlines are:

int check_line_column();

int check_line_line();

int check_columns();

int check_lines();

int check_unit();

int key_appar_no(int);

int key_appar_no_line(int,int);

int key_appar_no_column(int,int);

These are the only differences between the two versions of each function, because their algorithms are the same in both cases

Other posts on this topic:

,


  1. No comments yet.
(will not be published)

  1. No trackbacks yet.