Book Image

Practical C Programming

By : B. M. Harwani
Book Image

Practical C Programming

By: B. M. Harwani

Overview of this book

Used in everything from microcontrollers to operating systems, C is a popular programming language among developers because of its flexibility and versatility. This book helps you get hands-on with various tasks, covering the fundamental as well as complex C programming concepts that are essential for making real-life applications. You’ll start with recipes for arrays, strings, user-defined functions, and pre-processing directives. Once you’re familiar with the basic features, you’ll gradually move on to learning pointers, file handling, concurrency, networking, and inter-process communication (IPC). The book then illustrates how to carry out searching and arrange data using different sorting techniques, before demonstrating the implementation of data structures such as stacks and queues. Later, you’ll learn interesting programming features such as using graphics for drawing and animation, and the application of general-purpose utilities. Finally, the book will take you through advanced concepts such as low-level programming, embedded software, IoT, and security in coding, as well as techniques for improving code performance. By the end of this book, you'll have a clear understanding of C programming, and have the skills you need to develop robust apps.
Table of Contents (20 chapters)

Finding whether a matrix is sparse

A matrix is considered sparse when it has more zero values than non-zero values (and dense when it has more non-zero values). In this recipe, we will learn how to find out whether the specified matrix is sparse.

How to do it…

  1. First, specify the order of the matrix. Then, you will be prompted to enter the elements in the matrix. Let's assume that you specified the order of the matrix as 4 x 4. After entering the elements in the matrix, it might appear like this:

Figure 1.21
  1. Once the elements of the matrix are entered, count the number of zeros in it. A counter for this purpose is initialized to 0. Using nested loops, each of the matrix elements is scanned and, upon finding any zero elements, the value of the counter is incremented by 1.
  1. Thereafter, the following formula is used for establishing whether the matrix is sparse.

If counter > [(the number of rows x the number of columns)/2] =  Sparse Matrix

  1. Depending on the result of the preceding formula, one of the following messages will be displayed on the screen as follows:
The given matrix is a sparse matrix

or

The given matrix is not a sparse matrix

The sparsematrix.c program for establishing whether the matrix is sparse is as follows:

#include <stdio.h>
#define max 100

/*A sparse matrix has more zero elements than nonzero elements */
void main ()
{
static int arr[max][max];
int i,j,r,c;
int ctr=0;
printf("How many rows and columns are in this matrix? ");
scanf("%d %d", &r, &c);
printf("Enter the elements in the matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&arr[i][j]);
if (arr[i][j]==0)
++ctr;
}
}
if (ctr>((r*c)/2))
printf ("The given matrix is a sparse matrix. \n");
else
printf ("The given matrix is not a sparse matrix.\n");
printf ("There are %d number of zeros in the matrix.\n",ctr);
}

 Now, let's go behind the scenes to understand the code better.

How it works...

Because we don't want to fix the size of the matrix, we will define a macro called max of value 100. A matrix, or a two-dimensional array called arr, is defined of the order max x max. You will be prompted to enter the order of the matrix, for which you can again enter any value up to 100.

Let's assume that you’ve specified the order of the matrix as 4 x 4. You will be prompted to enter elements in the matrix. The values entered in the matrix will be in row-major order. After entering the elements, the matrix arr should look like Figure 1.22, as follows:

Figure 1.22

A counter called ctr is created and is initialized to 0. Using nested loops, each element of matrix arr is checked and the value of ctr is incremented if any element is found to be 0. Thereafter, using the if else statement, we will check whether the count of zero values is more than non-zero values. If the count of zero values is more than non-zero values, then the message will be displayed on the screen as follows:

The given matrix is a sparse matrix

However, failing that, the message will be displayed on the screen as follows:

The given matrix is not a sparse matrix

Let's use GCC to compile the sparsematrix.c program as follows:

D:\CBook>gcc sparsematrix.c -o sparsematrix

Let's run the generated executable file, sparsematrix.exe, to see the output of the program:

D:\CBook>./sparsematrix
How many rows and columns are in this matrix? 4 4
Enter the elements in the matrix :
0 1 0 0
5 0 0 9
0 0 3 0
2 0 4 0
The given matrix is a sparse matrix.
There are 10 zeros in the matrix.

Okay. Let's run the program again to see the output when the count of non-zero values is higher:

D:\CBook>./sparsematrix
How many rows and columns are in this matrix? 4 4
Enter the elements in the matrix:
1 0 3 4
0 0 2 9
8 6 5 1
0 7 0 4
The given matrix is not a sparse matrix.
There are 5 zeros in the matrix.

Voilà! We've successfully identified a sparse and a non-sparse matrix.

There's more...

How about finding an identity matrix, in other words, finding out whether the matrix entered by the user is an identity matrix or not. Let me tell you—a matrix is said to be an identity matrix if it is a square matrix and all the elements of the principal diagonal are ones and all other elements are zeros. An identity matrix of the order 3 x 3 may appear as follows:

Figure 1.23

In the preceding diagram, you can see that the principal diagonal elements of the matrix are 1's and the rest of them are 0's. The index or subscript location of the principal diagonal elements will be arr[0][0], arr[1][1], and arr[2][2], so the following procedure is followed to find out whether the matrix is an identity matrix or not:

  • Checks that if the index location of the row and column is the same, in other words, if the row number is 0 and the column number, too, is 0, then at that index location, [0][0], the matrix element must be 1. Similarly, if the row number is 1 and the column number, too, is 1, that is, at the [1][1] index location, the matrix element must be 1.
  • Verify that the matrix element is 0 at all the other index locations.

If both the preceding conditions are met, then the matrix is an identity matrix, or else it is not.

The identitymatrix.c program to establish whether the entered matrix is an identity matrix or not is given as follows:

    #include <stdio.h>
#define max 100
/* All the elements of the principal diagonal of the Identity matrix are ones and rest all are zero elements */
void main ()
{
static int arr[max][max];
int i,j,r,c, bool;
printf("How many rows and columns are in this matrix ? ");
scanf("%d %d", &r, &c);
if (r !=c)
{
printf("An identity matrix is a square matrix\n");
printf("Because this matrix is not a square matrix, so it is not an
identity matrix\n");
}
else
{
printf("Enter elements in the matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nThe entered matrix is \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
bool=1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
{
if(arr[i][j] !=1)
{
bool=0;
break;
}
}
else
{
if(arr[i][j] !=0)
{
bool=0;
break;
}
}
}
}
if(bool)
printf("\nMatrix is an identity matrix\n");
else
printf("\nMatrix is not an identity matrix\n");
}
}

Let's use GCC to compile the identitymatrix.c program as follows:

D:\CBook>gcc identitymatrix.c -o identitymatrix

No error is generated. This means the program is compiled perfectly and an executable file is generated. Let's run the generated executable file. First, we will enter a non-square matrix:

D:\CBook>./identitymatrix
How many rows and columns are in this matrix ? 3 4
An identity matrix is a square matrix
Because this matrix is not a square matrix, so it is not an identity matrix

Now, let's run the program again; this time, we will enter a square matrix

D:\CBook>./identitymatrix 
How many rows and columns are in this matrix ? 3 3
Enter elements in the matrix :
1 0 1
1 1 0
0 0 1

The entered matrix is
1 0 1
1 1 0
0 0 1

Matrix is not an identity matrix

Because a non-diagonal element in the preceding matrix is 1, it is not an identity matrix. Let's run the program again:

D:\CBook>./identitymatrix 
How many rows and columns are in this matrix ? 3 3
Enter elements in the matrix :
1 0 0
0 1 0
0 0 1
The entered matrix is
1 0 0
0 1 0
0 0 1
Matrix is an identity matrix

Now, let's move on to the next recipe!