# C and C++ library of functions and procedures for matrix (bidimensional arrays) processing – Read and Write functions

To realize a real computation of matrices it must exist in the first place the matrix or the matrices. Having created an inadequate object for work with it, the results would be unexpected. Therefore we must pay the necessary attention to those functions that are creating a matrix. In this way we won’t have any future troubles.

Matrix processing contains also a sequence of in and out functions. These functions role is:

• to create a matrix from the console when using the library matrix.h or one with all its elements equal with zero when using the class matrix constructor.
• to list a matrix on the computer’ screen

Because the library matrix.h uses a matrix with constant dimensions, and the class matrix uses a dynamic declared matrix, defined with the help of a pointer, both versions of the creating function are described in this chapter.

Creating applications based on the use of matrices it has been observed that beside the wanted matrix, some new matrices are required. These matrices are making the work more easier and some of them are: the unit matrix, the Boolean matrix, the matrix with all its components from a line equal with 1 and in the rest equal with 0 etc. The subprograms that are creating or are showing on screen these matrices are also in and out functions.

Static declared matrix is created from the console using a function from the library matrix.h.

void creation(int *n, int *m, int x)

How it can be seen the function type is void. It receives the following variables as parameters:

• int x– constant integer variable of type pointer ( memory for 30 lines and 30 columns is allocated, fig.1). This is the matrix that will be created from the console.
• int *n– exit parameter, representing matrix’ lines number. Its value is read from the keyboard and a value transfer will take place.
• int *m– exit parameter, representing matrix’ lines number. Its value is read from the keyboard and a value transfer will take place.

Working with matrices declared with constant dimensions it will allocate for each one an array of 30 lines and 30 columns, even it was needed for example only a matrix with 2 lines and 2 columns. (in figure 1 the white squares are representing the matrix with 2 lines and 2 columns created from the keyboard)

Function creation body is:

```void creation(int *n,int *m,int x)
{
int i,j,k;
clrscr();
printf("\n Matrix lines number:");
scanf("%d",n);
valid_l_c(n);
printf("\n Matrix columns number:");
scanf("%d",m);
valid_l_c(m);
printf("\n The matrix is:");
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
{
printf("\n The element x[%d][%d] is:",i,j);
k=scanf("%d",&x[i][j]);
valid_no_integer(&x[i][j],k);
}
}
```

Lines number and columns number of the matrix are read. Its elements will be created after that. As it can be seen every data entrance is followed by a validation. These procedures check if the information given by the user is incorrect (characters instead of integers, float instead of integer, lines or columns number is bigger than 30 or smaller than 1). Two functions are doing the data validation:

void valid_no_integer(int *l, int k);

void valid_l_c(int *l).

void valid_l_c(int *l)– is validating the dimensions of a matrix. The function has one parameter (used for both in and out transfers) represented by an integer int *l, and it transfers that parameter to other functions with the help of its value. This function body is:

```void valid_l_c(int *l)
{
int i,j;
char lines,*endptr;

clrscr();
if((*l<1)||(*l>30))
do
{
printf("\n Error !");
gets(lines);
*l=strtod(lines,&endptr);
clrscr();
}
while ((*l<1)||(*l>30)||(*endptr>9));
}
```

The validation is very strong because don’t allowed that numbers followed by characters to be introduced by user. The number taken from the keyboard is read as a string. With the function int strtod(char, & char*) is made the conversion from string to a number. When a validation error is found, the message Error! will be shown on screen.

void valid_no_integer(int *l, int k)– validates the reading of a integer variable. It is used by the function that creates a static defined matrix for validating the structure elements. Function parameters are two integer variables:

• int k– entrance parameter; the function scanf returns value 0 for an incorrect reading and 1 for a correct reading. One of these two values is received by the function valid_no_integer with the help of its k. If k has value 0 the message Error! is shown and a new data introduction is required.
• int *l– is the read value that is correct from the validation point of view. Its transfer outside the function is made by value.
```void valid_no_integer(int *l,int k)
{
if (k==0)
{
fflush(stdin);
do
{
k=1;
printf("\n Error ! :");
k=scanf("%d",l);
fflush(stdin);
}
while(k==0);
}
}
```

Unlike the precedent validation function, the last one isn’t so strong because it allows the introduction of numbers followed by characters.

So that was the subprogram that creates a static defined matrix from the keyboard. Next it will be presented how a dynamic defined matrix is created also from the console. The class matrix has implemented this second method. Creating a matrix from the keyboard is done by the class’ implicit constructor and by the >> operator.

The class named matrix has two constructors. One implicit without parameters that creates a matrix from the keyboard and another with two parameters (lines and columns numbers) that creates a matrix with its entire elements equal with 0.

The implicit constructor is:

```matrix::matrix()
{
cout<<"\n Matrix lines number:";
cin>>n;
////clrscr();
cout<<"\n Matrix columns number:";
cin>>m;
a=aloc_mat(n,m);
cout<<"The matrix is:";
for(int i=0;i<n;i++) for(int="" j="0;j<m;j++)" {="" cout<<"\n="" a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
}
```

Defining a matrix means its name, its elements type and its dimensions. By defining it as pointer like int
**x, its dimensions are not required.

Using the second variant, we use exactly the needed memory space, which is allocated in a dynamic way according to the matrix dimensions.

Dynamic memory allocation supposes the use of the memory in a more efficient way. For doing that we have a class member function named int** aloc_mat(int, int). How it can be seen in the constructor heading the necessary space allocation is done after reading the matrix dimensions. It is very important not forgetting this step because the results are depending on it. This class matrix member function has the attribute private.

Its content is:

```int ** matrix::aloc_mat(int n,int m)
{
int **pmat;
pmat=(int **)malloc(n*sizeof(int*));
for (int i=0;i<n;i++) pmat[i]="(int*)malloc(m*sizeof(int));" return="" pmat;="" }="" <="" pre="">```

The entry data for this function is the matrix columns and lines numbers. The function returns a pointer.

The matrix X with the dimensions n=4 and m=3,

, where n is the lines number and m represents the columns number, it will look in memory like

First it is allocated space for an array of pointers that are containing the start addresses of each line of the matrix. This array has n components. Next is allocated space for each line of m components. After the necessary space has been defined the matrix elements are introduced.

Another creating function is obtained by overloading the operator “>>“. It is declared as a class friend function and has the following form:

```istream &operator>>(istream &c,matrix &a)
{
//clrscr();
cout<<"The matrix is:";
for(int i=0;i<a.n;i++) for(int="" j="0;j<a.m;j++)" {="" cout<<"\n="" a["<<i<<"]["<<j<<"]=";
c>>a.a[i][j];
}
return c;
}
```

The “>>” operator only objective is to get the matrix elements, because the creation of a matrix from the keyboard is made by the class constructor that has parameters. This constructor initializes the matrix dimensions with the wanted numbers and its elements with the value 0. Another attribute of the “>>” operator is that it is used by objects created by the constructor with parameters.

The structure of the mentioned constructor is:

```matrix::matrix(int k,int l)
{
n=k;
m=l;
a=aloc_mat(n,m);
for(i=0;i<n;i++) for="" (j="0;j<m;j++)" a[i][j]="0;" }="" <="" pre="">```

In conclusion these have been the functions that create an ordinary matrix defined static or dynamic from the keyboard. The following subprograms are the ones that create the special matrices.

The function void creating_unit(int *n,int *m, int x) from the library matrix.h creates the unit matrix. Its parameters are:

• int *n – exit parameter representing the matrix lines’ number;
• int *m – exit parameter representing the matrix columns’ number;
• int x – exit parameter representing the unit matrix

The cod sequence that creates the unit matrix is:

```void creating_unit(int *m,int *n,int x)
{
int i,j;
printf("\n Matrix is square,so lines number = columns numbers");
printf("\n Lines and columns number:");

scanf("%d",n);
valid_l_c(n);
*m=*n;

for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
if(i==j) x[i][j]=1;
else x[i][j]=0;
}
```

The unit matrix is a square matrix in the first place and that’s why the lines number is the same with the columns number.

Using the class matrix a unit matrix can be created with the member function void creating_unit(). Although we use a dynamic defined matrix, we don’t need to allocate space for it, because this has been done by the class constructor when the object has been created. Not to be forgotten is that all of these functions which are creating special matrices are associated to objects made by the constructor with parameters.

The code sequence used for creating a unit matrix is:

```void matrix::creating_unit()
{
////clrscr();
for (i=0;i<n;i++) for(j="0;j<=i;j++)" if="" (i="=j)" a[i][j]="1;" else="" }="" <="" pre="">```

So the only difference between the function creating_unit from matrix.h and the function creating_unit from the class matrix is that in the last one the matrix lines and columns number is no more required. These dimensions are known to the function because they are class members and they are initialized by the class constructor.

The function void creating_column_1(int *n,int *m, int x) creates a matrix with a column that has all components equal with 1. The column is chosen by the user and all the other matrix’ elements are 0. The function has the following parameters:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix

The code sequence that creates a matrix with the value 1 on one column is:

```void creating_column_1(int *m,int *n,int x)
{
int i,j,c,k;

printf("\n The matrix lines number:");
scanf("%d",n);
valid_l_c(n);

printf("\n The matrix columns number:");
scanf("%d",m);
valid_l_c(m);

printf("\n The column with elements one is:");
k=scanf("%d",&c);

if ((c<1)|(k==0)|(c>*m))
{
fflush(stdin);
do
{
k=1;
printf("\n Eror, retype the column number(between 1 and %d):");
k=scanf("%d",&c);
if((c<1)|(c>*m)) k=0;
fflush(stdin);
}
while(k==0);
}
c--;
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
{
if(j==c) x[i][c]=1;
else x[i][j]=0;
}
}
```

Using the class matrix, a matrix with value 1 on an entire column is obtained with the help of the member function void creating_column_1(int c). This function parameter is the integer variable c, which represents that column.

The code sequence is:

```void matrix::creating_column_1(int c)
{
if (c<m) for="" (i="0;i<n;i++)" (j="0;j<m;j++)" if="" a[i][c]="1;" else="" a[i][j]="0;" cout<<"column="" doesn't="" exist!";="" }="" <="" pre="">```

In the class member function, the column chosen to have its entire components equal with 1 is a parameter and in the library function the column is read from the keyboard inside the function.

The function void creating_line_1(int *n,int *m, int x) creates a matrix with a line that has all components equal with 1. The line is chosen by the user and all the other matrix’ elements are 0. The function has the following parameters:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix

The code sequence that creates a matrix with the value 1 on one line is:

```void creating_line_1(int *m,int *n,int x)
{
int i,j,k,l;

printf("\n The matrix lines number:");
scanf("%d",n);
valid_l_c(n);

printf("\n The matrix columns number:");
scanf("%d",m);
valid_l_c(m);

printf("\n The line with elements one is:");
k=scanf("%d",&l);

if ((k==0)|(l<1)|(l>*n))
{
fflush(stdin);
do
{
k=1;
printf("\n Eror, retype the line number(between 1 and %d):");
k=scanf("%d",&l);
if((l<1)|(l>*n)) k=0;
fflush(stdin);
}
while(k==0);
}
l--;
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
{
if(i==l) x[i][j]=1;
else x[i][j]=0;
}
}
```

Using the class matrix, a matrix with value 1 on an entire line is obtained with the help of the member function void creating_line_1(int l). This function parameter is the integer variable l, which represents that line.

The code sequence is:

```void matrix::creating_line_1(int l)
{
if (l<n) for="" (i="0;i<n;i++)" (j="0;j<m;j++)" if="" a[l][j]="1;" else="" a[i][j]="0;" cout<<"line="" doesn't="" exist!";="" }="" <="" pre="">```

In the class matrix the line to be modified is a parameter of the function. But in the library matrix.h the line is read inside the function from the keyboard.

The function void creating_sec_diag(int *n, int *m, int x) creates a matrix with all the elements from the secondary diagonal equal with 1 and the others elements equal with 0. The function parameters are:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix with value 1 on the secondary diagonal

This function’ body is:

```void creating_sec_diag(int *n,int *m,int a)
{
int i,j,k;
clrscr();

printf("\n Matrix lines number:");
scanf("%d",n);
valid_l_c(n);
clrscr();

printf("\n Matrix columns number:");
scanf("%d",m);
valid_square(m,*n);

for (i=0;i<*n;i++)
for(j=0;j<*n;j++)
if (j==((*n)-i-1)) a[i][j]=1;
else a[i][j]=0;
}
```

The matrix with all elements from the secondary diagonal having the value 1 must be in the first place a square matrix. For this reason it is used the function valid_square(m,*n) that checks if the lines number is equal with the columns number. If the matrix is not a square one, the message Error is shown on screen and new data is required.

A same type of matrix is created using the class matrix member function void creating_sec_diag(). The code sequence
representative for this function is:

```void matrix::creating_sec_diag()
{
////clrscr();
for (i=0;i<n;i++) for(j="0;j<n;j++)" if="" (j="=((n)-i-1))" a[i][j]="1;" else="" }="" <="" pre="">```

Lines and columns numbers, n, m are class members being initialized by the constructor.

The function void creating_0_up_main_diag(int *n, int *m, int x) creates a matrix with its entire elements above the main diagonal equal with 0. The others elements of the matrix are given by the user. This function parameters are:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix with value 0 above the main diagonal

The code sequence that creates a matrix of this type is:

```void creating_0_up_main_diag(int *n,int *m,int a)
{
int i,j,k,l;
clrscr();

printf("\n Matrix lines number :");
scanf("%d",n);
valid_l_c(n);
clrscr();

printf("\n Matrix column number :");
scanf("%d",m);
valid_square(m,*n);
clrscr();

printf("\n The matrix is:\n");
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
if	 (i>=j)
{
printf("\n a[%d][%d]=",i,j);
l=scanf("%d",&a[i][j]);
valid_no_integer(&a[i][j],l);
}
else a[i][j]=0;
}
```

and the class method

```void matrix::creating_0_up_main_diag()
{
////clrscr();
cout<<"\nThe matrix is :\n";
if (n==m)
for(i=0;i<n;i++) for(j="0;j<m;j++)" if="" (i="">=j){
cout<<"\n a["<<i<<"]["<<j<<"]="; cin="">>a[i][j];
}
else a[i][j]=0;
else { cout<<"The matrix must be a square!";
return;}
}
</i<<"]["<<j<<"]=";></n;i++)>```

Using the dynamic version, the function which creates a matrix previous described is creating_0_up_main_diag(). This function is from the class matrix and its heading is the same with the one of the library’ function. Because we use in this case objects we don’t need to read inside the function the matrix dimensions.

The function void creating_0_main_diag(int *n, int *m, int x) creates a matrix with the value 0 on the main diagonal. All the others elements of the matrix are given by the user. The function has the following parameters:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix with 0 on the main diagonal

The function content is:

```void creating_0_main_diag(int *n,int *m,int a)
{
int i,j,k,l;
clrscr();

printf("\n Matrix lines number :");
scanf("%d",n);
valid_l_c(n);
clrscr();

printf("\n Matrix column number:");
scanf("%d",m);
valid_square(m,*n);
clrscr();

printf("\n The matrix is:\n");
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
if	 (i!=j)
{
printf("\n a[%d][%d]=",i,j);
l=scanf("%d",&a[i][j]);
valid_no_integer(&a[i][j],l);
}
else a[i][j]=0;
}
```

The function from the class matrix that does the same thing is creating_0_main_diag(). It is a class member function without type. The difference between these two functions is that in the code sequence of the last one we don’t read the matrix dimensions because we have them from the class.

The function void creating_0_under_main_diag(int *n, int *m, int x) creates a matrix with its entire elements under the main diagonal equal with 0. The others elements of the matrix are given by the user. This function parameters are:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix with value 0 under the main diagonal

The code sequence that creates a matrix of this type is:

```void creating_0_under_main_diag(int *n,int *m,int a)
{
int i,j,k,l;
clrscr();

printf("\n Matrix lines number :");
scanf("%d",n);
valid_l_c(n);
clrscr();

printf("\n Matrix columns number :");
scanf("%d",m);
valid_square(m,*n);
clrscr();

printf("\n The matrix is:\n");
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
if	 (i<=j)
{
printf("\n a[%d][%d]=",i,j);
l=scanf("%d",&a[i][j]);
valid_no_integer(&a[i][j],l);
}
else a[i][j]=0;
}
```

The function from the class matrix that does the same thing as the library function is creating_0_main_diag(). It is a class member function with the void type. The only difference between these two functions is that in the code sequence of the last one we don’t read the matrix dimensions because we have them from the class. The matrix dimensions have been initialized when the object was created by the class’ constructor

The function void creating_0_sec_diag(int *n, int *m, int x) creates a matrix with the value 0 on the second main diagonal. All the others elements of the matrix are given by the user. The function has the following parameters:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix with 0 on the second diagonal

The function content is:

```void creating_0_sec_diag(int *n,int *m,int a)
{
int i,j,k,l;
clrscr();

printf("\n Matrix lines number :");
scanf("%d",n);
valid_l_c(n);
clrscr();

printf("\n Matrix columns number :");
scanf("%d",m);
valid_square(m,*n);
clrscr();

printf("\n The matrix is:\n");
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
if	 (j!=*n-i-1)
{
printf("\n a[%d][%d]=",i,j);
l=scanf("%d",&a[i][j]);
valid_no_integer(&a[i][j],l);
}
else a[i][j]=0;
}
```

In the class matrix there is a function creating_0_sec_diag() that has the same result. This function is a member of the class and its type is void. It is different from the library’ function because it don’t ask for the matrix dimensions to be read. It has this numbers from the class constructor.

The function void creating_0_up_sec_diag(int *n, int *m, int x) creates a matrix with its entire elements above the secondary diagonal equal with 0. The others elements of the matrix are given by the user. This function parameters are:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix with value 0 above the secondary diagonal

This function body is:

```void creating_0_up_sec_diag(int *n,int *m,int a)
{
int i,j,k,l;
clrscr();

printf("\n Matrix lines number :");
scanf("%d",n);
valid_l_c(n);
clrscr();

printf("\n Matrix columns number :");
scanf("%d",m);
valid_square(m,*n);
clrscr();

printf("\n The matrix is:\n");
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
if	 (i+j>=*n-1)
{
printf("\n a[%d][%d]=",i,j);
l=scanf("%d",&a[i][j]);
valid_no_integer(&a[i][j],l);
}
else a[i][j]=0;
}
```

Doing that dynamic, that is using the class matrix member functions. The class function that we need is creating_0_up_sec_diag(). Because we are working with objects, the class constructor already initializes the matrix dimensions, so we don’t read them inside the function.

The function void creating_0_under_sec_diag(int *n, int *m, int x) creates a matrix with its entire elements under the secondary diagonal equal with 0. The others elements of the matrix are given by the user. This function’ parameters are:

• int *n – exit parameter representing the lines’ number
• int *m – exit parameter representing the columns’ number
• int x – exit parameter representing the resulted matrix with value 0 under the secondary diagonal

This function body is:

```void creating_0_under_sec_diag(int *n,int *m,int a)
{
int i,j,k,l;
clrscr();

printf("\n Matrix lines number :");
scanf("%d",n);
valid_l_c(n);
clrscr();

printf("\n Matrix columns number :");
scanf("%d",m);
valid_square(m,*n);
clrscr();

printf("\n The matrix is:\n");
for(i=0;i<*n;i++)
for(j=0;j<*m;j++)
if	 (i+j<=*n-1)
{
printf("\n a[%d][%d]=",i,j);
l=scanf("%d",&a[i][j]);
valid_no_integer(&a[i][j],l);
}
else a[i][j]=0;
}
```

The function creating_0_under_sec_diag() from the class matrix does the same thing as the function described above. It is a class member function and its type is void. The difference between the two functions is that in the class function the matrix dimensions are known from the class constructor.

The function void creating_bool_matrix(int n, int m, int x, int b) creates the Boolean matrix. It is the matrix whose elements could have only the values 0 and 1. The Boolean matrix is associated with another matrix that has the values.

This function has the following parameters:

• int n – entry parameter representing the matrix X lines number ;
• int m – entry parameter representing the matrix X columns number;
• int x – entry parameter representing the associated matrix;
• int b[30 ]– exit parameter representing the Boolean matrix obtained from the matrix X;

The sequence that creates the Boolean matrix is:

```void creating_bool_matrix(int n,int m,int a,int b)
{
int i,j;
for (i=0;i<n;i++) for(j="0;j<m;j++)" if="" (a[i][j]="=0)" b[i][j]="0;" else="" }="" <="" pre="">```

In the class matrix the Boolean matrix is obtained by using the member function creating_bool_matrix(matrix &). This function only parameter is an matrix object received by the function with the help of its address. The function is:

```void matrix::creating_bool_matrix(matrix &b)
{
for (i=0;i<n;i++) for="" (j="0;j<m;j++)" if="" (a[i][j]="=0)" b.a[i][j]="0;" else="" }="" <="" pre="">```

All these functions that create structures or objects are very important, but the functions that are displaying the results have the same importance too. These functions are the out functions. In the case of this project are used only out functions that use the screen, however can be others functions which can use a printer, a file or more files.

Listing on screen is obtained in the static way of working with the help of the function void show_matrix(int n,int m,int x). Inside the class the operator “<<“, that has been overloaded, gives the same result.

The function show_matrix(n,m,x) displays the matrix x. Its entry parameters are:

• int n– lines number;
• int m– columns number;
• int x-the matrix.

The function is:

```void show_matrix(int n,int m,int x)
{
int i,j;
printf("\n The matrix is:\n");
for (i=0;i<n;i++) {for="" (j="0;j<m;j++)" printf("="" %3d="" ",x[i][j]);="" printf("\n");}="" }="" <="" pre="">```

For example, if we have the 3 lines and 3 columns matrix with the components

1,2,3,4,5,6,7,8,9, on the screen is displayed:

```The matrix is:
1   2   3
4   5   6
7   8   9
```

The operator “<<” is defined as a friend function în thematrix class.

```ostream &operator<<(ostream &c,matrix &a)
{
int i,j;
if((a.n!=0)&&(a.m!=0))
{
cout<<"\n The matrix is : \n";
for (i=0;i<a.n;i++) {="" cout<<"\n\t";="" for="" (j="0;j<a.m;j++)" c<<"\t"<<a.a[i][j];="" }="" else="" cout<<"\nnothing="" to="" list="" !!!";="" return="" c;="" <="" pre="">```

For later using, the library matrix.h contains also the functions creation_float(&n,&m,x), creating_unit_float(n,m,x) şi show_matrix_float(n,m,x). The only difference between these functions and the ones described in this chapter is the type of the matrix’ elements. In this case the matrix is declared like:

float x.

Therefore the comments regarding these functions are the same with the ones which are using a two-dimensional array with elements of type integer.

Matrix processing supposes working with matrices. Their existence is assured by the in functions. The correctness of the applications is verified with the help of the out functions. These are only two reasons to take seriously those functions without which we can’t work with matrices.

`</a.n;i++)>`
`</n;i++)>`
`</n;i++)>`
`</n;i++)>`
`</n;i++)>`
`</n)>`
`</m)>`
`</n;i++)>`
`</n;i++)>`

</a.n;i++)>

</n;i++)></n;i++)>