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 the difference between two sets or arrays

When we talk about the difference between two sets or arrays, we are referring to all the elements of the first array that don't appear in the second array. In essence, all the elements in the first array that are not common to the second array are referred to as the difference between the two sets. The difference in sets p and q, for example, will be denoted by p – q.

If array p, for example, has the elements {1, 2, 3, 4}, and array q has the elements {2, 4, 5, 6}, then the difference between the two arrays, p - q, will be  {1,3}.  Let's find out how this is done.

How to do it…

  1. Define two arrays, say p and q, of a certain size and assign elements of your choice to both the arrays.
  2. Define one more array, say r, to be used for storing the elements that represent the difference between the two arrays.
  3. Pick one element from array p and compare it with all the elements of the array q.
  4. If the element of array p exists in array q, discard that element and pick up the next element of array p and repeat from step 3.
  5. If the element of array p does not exist in array q, add that element in array r. Before adding that element to array r, ensure that it does not already exist in array r.
  6. Repeat steps 3 to 5 until all the elements of array p are compared.
  7. Display all the elements in array r, as these are the elements that represent the difference between arrays p and q.

The differencearray.c program to establish the difference between two arrays is as follows:

#include<stdio.h>
#define max 100

int ifexists(int z[], int u, int v)
{
int i;
if (u==0) return 0;
for (i=0; i<=u;i++)
if (z[i]==v) return (1);
return (0);
}

void main()
{
int p[max], q[max], r[max];
int m,n;
int i,j,k;
printf("Enter length of first array:");
scanf("%d",&m);
printf("Enter %d elements of first array\n",m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
printf("\nEnter length of second array:");
scanf("%d",&n);
printf("Enter %d elements of second array\n",n);
for(i=0;i<n;i++ ) scanf("%d",&q[i]);
k=0;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
if (p[i]==q[j])
{ break;
}
}
if(j==n)
{
if(!ifexists(r,k,p[i]))
{
r[k]=p[i];
k++;
}
}
}
printf("\nThe difference of the two array is:\n");
for(i = 0;i<k;i++)
printf("%d\n",r[i]);
}

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

How it works...

We defined two arrays called p and q. We don't want to fix the length of these arrays, so we should define a macro called max of value 100 and set the two arrays, p and q, to the size of max.

Thereafter, you will be prompted to specify the size of the first array and enter the elements in the first array, p. Similarly, you will be asked to specify the length of the second array, q, followed by entering the elements in the second array.

Let's assume you have specified the length of both arrays as 4 and have entered the following elements:

Figure 1.14

We need to pick up one element at a time from the first array and compare it with all the elements of the second array. If an element in array p does not appear in array q, it will be assigned to the third array we created, array r.

Array r will be used for storing the elements that define the difference between two arrays. As shown in Figure 1.15, the first element of array p, in other words, at p[0], is compared with all the elements of array q, in other words, with q[0], q[1], q[2], and q[3].

Because the element at p[0], which is 1, does not appear in array q, it will be added to the array r, indicating the first element representing the difference between the two arrays:

Figure 1.15

Because the element at p[1], which is 2, appears in array q, it is discarded, and the next element in array p, in other wordsp[2], is picked up and compared with all the elements in array q.

As the element at p[2] does not appear in array q, it is added to array r at the next available location, which is r[1] (see Figure 1.16 as follows):

Figure 1.16

Continue the procedure until all the elements of array p are compared with all the elements of array q. Finally, we will have array r, with the elements showing the difference between our two arrays, p and q.

Let's use GCC to compile our program, differencearray.c, as follows:

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

Now, let's run the generated executable file, differencearray, to see the output of the program:

D:\CBook>./differencearray
Enter length of first array:4
Enter 4 elements of first array
1
2
3
4
Enter length of second array:4
Enter 4 elements of second array
2
4
5
6
The difference of the two array is:
1
3

Voilà! We've successfully found the difference between two arrays. Now, let's move on to the next recipe!