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 unique elements in an array

In this recipe, we will learn how to find the unique elements in an array, such that the repetitive elements in the array will be displayed only once.

How to do it… 

  1. Define two arrays, p and q, of a certain size and assign elements only to array p. We will leave array q blank.
  2. These will be our source and target arrays, respectively. The target array will contain the resulting unique elements of the source array.
  1. After that, each of the elements in the source array will be compared with the existing elements in the target array. 
  2. If the element in the source array exists in the target array, then that element is discarded and the next element in the source array is picked up for comparison.
  3. If the source array element does not exist in the target array, it is copied into the target array.
  4. Let's assume that array p contains the following repetitive elements:

Figure 1.17
  1. We will start by copying the first element of the source array, p, into the target array, q, in other words, p[0] into array q[0], as follows:

Figure 1.18
  1. Next, the second array element of p, in other words, p[1], is compared with all the existing elements of array q. That is, p[1] is compared with array q to check whether it already exists in array q, as follows:

Figure 1.19
  1. Because p[1] does not exist in array q, it is copied at q[1], as shown in Figure 1.20: 

Figure 1.20
  1. This procedure is repeated until all the elements of array p are compared with array q. In the end, we will have array q, which will contain the unique elements of array p.

Here is the uniqueelements.c program for finding the unique elements in the first array:

#include<stdio.h>
#define max 100

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

void main()
{
int p[max], q[max];
int m;
int i,k;
k=0;
printf("Enter length of the array:");
scanf("%d",&m);
printf("Enter %d elements of the array\n",m);
for(i=0;i<m;i++ )
scanf("%d",&p[i]);
q[0]=p[0];
k=1;
for (i=1;i<m;i++)
{
if(!ifexists(q,k,p[i]))
{
q[k]=p[i];
k++;
}
}
printf("\nThe unique elements in the array are:\n");
for(i = 0;i<k;i++)
printf("%d\n",q[i]);
}

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

How it works...

We will define a macro called max of size 100. Two arrays, p and q, are defined of size max. Array p will contain the original elements, and array q will contain the unique elements of array p. You will be prompted to enter the length of the array and, thereafter, using the for loop, the elements of the array will be accepted and assigned to array p.

The following statement will assign the first element of array p to the first index location of our blank array, which we will name array q:

q[0]=p[0]

A for loop is again used to access the rest of the elements of array p, one by one. First, the foremost element of array p, which is at p[0], is copied to array q at q[0].

Next, the second array p element, p[1], is compared with all the existing elements of array q. That is, p[1] is checked against array q to confirm whether it is already present there.

Because there is only a single element in array q,  p[1] is compared with q[0]. Because p[1] does not exist in array q, it is copied at q[1].

This procedure is repeated for all elements in array p. Each of the accessed elements of array p is run through the ifexists() function to check whether any of them already exist in array q

The function returns 1 if an element in array p already exists in array q. In that case, the element in array p is discarded and the next array element is picked up for comparison.

In case the ifexists() function returns 0, confirming that the element in array p does not exist in array q, the array p element is added to array q at the next available index/subscript location.

When all the elements of array p are checked and compared, array q will have only the unique elements of array p.

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

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

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

D:\CBook>./uniqueelements
Enter the length of the array:5
Enter 5 elements in the array
1
2
3
2
1

The unique elements in the array are:
1
2
3

Voilà! We've successfully identified the unique elements in an array. Now, let's move on to the next recipe!