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)

Multiplying two matrices

A prerequisite for multiplying two matrices is that the number of columns in the first matrix must be equal to the number of rows in the second matrix.

How to do it…

  1. Create two matrices of orders 2 x 3 and 3 x 4 each. 
  2. Before we make the matrix multiplication program, we need to understand how matrix multiplication is performed manually. To do so, let's assume that the two matrices to be multiplied have the following elements:

Figure 1.5
  1. The resultant matrix will be of the order 2 x 4, that is, the resultant matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix:

Figure 1.6

Essentially, the resultant matrix of the order 2 x 4 will have the following elements:

Figure 1.7
  1. The element first row, first column in the resultant matrix is computed using the following formula:

SUM(first element of the first row of the first matrix × first element of the first column of the second matrix), (second element of the first row... × second element of the first column...), (and so on...)

For example, let's assume the elements of the two matrices are as shown in Figure 1.5.  The elements in the first row and the first column of the resultant matrix will be computed as follows:

Figure 1.8
  1. Hence, the element in first row, first column in the resultant matrix will be as follows:

(3×6)+(9×3)+(7×5)
=18 + 27 + 35
=80

Figure 1.9 explains how the rest of the elements are computed in the resultant matrix:

Figure 1.9

The matrixmulti.c program for multiplying the two matrices is as follows:

#include  <stdio.h>
int main()
{
int matA[2][3], matB[3][4], matR[2][4];
int i,j,k;
printf("Enter elements of the first matrix of order 2 x 3 \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&matA[i][j]);
}
}
printf("Enter elements of the second matrix of order 3 x 4 \n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&matB[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
matR[i][j]=0;
for(k=0;k<3;k++)
{
matR[i][j]=matR[i][j]+matA[i][k]*matB[k][j];
}
}
}
printf("\nFirst Matrix is \n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matA[i][j]);
}
printf("\n");
}
printf("\nSecond Matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",matB[i][j]);
}
printf("\n");
}
printf("\nMatrix multiplication is \n");
for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
printf("%d\t",matR[i][j]);
}
printf("\n");
}
return 0;
}

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

How it works...

The two matrices are defined matA and matB of the orders 2 x 3 and 3 x 4, respectively, using the following statement:

int matA[2][3], matB[3][4]

You will be asked to enter the elements of the two matrices using the nested for loops. The elements in the matrix are entered in row-major order, in other words, all the elements of the first row are entered first, followed by all the elements of the second row, and so on.

In the nested loops, for i and for j, the outer loop, for i, represents the row and the inner loop, and for j represents the column.

While entering the elements of matrices matA and matB, the values entered in the two matrices will be assigned to the respective index locations of the two-dimensional arrays as follows:

Figure 1.10

The nested loops that actually compute the matrix multiplication are as follows:

  for(i=0;i<2;i++)
{
for(j=0;j<4;j++)
{
matR[i][j]=0;
for(k=0;k<3;k++)
{
matR[i][j]=matR[i][j]+matA[i][k]*matB[k][j];
}
}
}

The variable i represents the row of the resultant matrix, j represents the column of the resultant matrix, and k represents the common factor. The common factor here means the column of the first matrix and the row of the second matrix.

Recall that the prerequisite for matrix multiplication is that the column of the first matrix should have the same number of rows as the second matrix. Because the respective elements have to be added after multiplication, the element has to be initialized to 0 before addition.

The following statement initializes the elements of the resultant matrix:

      matR[i][j]=0;

The for k loop inside the nested loops helps in selecting the elements in the rows of the first matrix and multiplying them by elements of the column of the second matrix:

matR[i][j]=matR[i][j]+matA[i][k]*matB[k][j];

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

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

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

D:\CBook\Chapters\1Arrays>./matrixmulti

Enter elements of the first matrix of order 2 x 3
3
9
7
1
5
4

Enter elements of the second matrix of order 3 x 4
6 2 8 1

3 9 4 0
5 3 1 3

First Matrix is
3 9 7
1 5 4

Second Matrix is
6 2 8 1
3 9 4 0
5 3 1 3

Matrix multiplication is
80 108 67 24
41 59 32 13

Voilà! We've successfully multiplied two matrices.

There’s more…

One thing that you might notice while entering the elements of the matrix is that there are two ways of doing it.

  1. The first method is that you press Enter after inputting each element as follows:
3
9
7
1
5
4

The values will be automatically assigned to the matrix in row-major order, in other words, 3 will be assigned to matA[0][0], 9 will be assigned to matA[0][1], and so on.

  1. The second method of entering elements in the matrix is as follows:
6 2 8 1
3 9 4 0
5 3 1 3

Here, 6 will be assigned to matB[0][0], 2 will be assigned to matB[0][1], and so on.

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