ARTICLE

Arrays in C

Posted by Dhananjay Kumar Articles | Visual Studio 2010 February 09, 2012
Arrays are a linear data structure that stores the same type of data in contiguous memory locations. Arrays are best used to store data in contiguous memory locations.
Reader Level:

Arrays are a linear data structure that stores the same types of data in contiguous memory locations. Arrays are best used to store data in contiguous memory locations. However an array can have a definite size of data. The maximum size of an array must be set when declaring the array.

An array can be defined as below:

ArrayC1.gif

Diagrammatically myarray can be shown as below:

ArrayC2.gif

Some information about the above array is:

  1. The Name of the array is myarray.
  2. This is an integer array
  3. The size of the array is 5
  4. The Base address of the array is 2000H
  5. One element takes 2 bytes in memory. I am assuming we are using the C language, so an integer takes 2 bytes in memory.
  6. 0,1,2,3,4 are the indexes of the array
  7. 99, 13,77,567,9 are elements of the array.
  8. You can say 77 is an array element at the 2nd position of the array.
  9. Notation to fetch array elements is as below:

    myarray[2]=77

If you need to find the memory location of the 20th element in the above array then you can get that using the following formula:

Base Memory location + (Memory size of elements * (Element Position -1))

In the above case:

  1. Base memory location = 2000H
  2. Size of elements = 16 Bits
  3. Element position = 20

    Memory location of 20th element = 2000 + (16 * (20-1)) = 2000 + 16 *19 = 2304H

On an array, usually the following native operations can be performed:

  1. Traversing an array
  2. Search an element in array
  3. Insert an element at given position in array
  4. Delete and element from a given position in array
  5. Reverse an array

Array in C

In C we can create an array as below:

ArrayC3.gif

We created three different types of arrays. They are as below:

ArrayC4.gif

You can store values in an array as below:

ArrayC5.gif

And you can print them as below:

ArrayC6.gif

Traversing an Array

Traversing an array means iterate through each element of the array once and perform some operations on each element.

ArrayC7.gif

In the above pseudo code K is a variable, A is an array and N is an upper limit of the array. In C a function to traverse an array and print all the elements can be as below:

int  traverse(int *myarray)

         int i;
         for(i=0;i<5;i++)
         {
                 printf("%d\n",myarray[i]);
         }
         return 0;
}


And in the main function an array is declared as below:

#include<stdio.h>
#include<conio.h>
void main()
{
  int myarray[5];
  myarray[0]=9; 
  myarray[4]=99;
  traverse(myarray);
  getch();
}

You should get output as below:

ArrayC8.gif

You can see in the output that for the 2nd, 3rd and 4th position the elements have some garbage value because we did not assign any values to these positions in the array.

Insert in Array

You can insert an element to a given position in array using the following algorithm. Let us say:

  1. K: position to insert element
  2. Element: element to insert
  3. N : Size of the array
  4. A : Name of the array

    ArrayC9.gif

In C the function to insert an element in a given position in array can be written as below:

int insertelement(int *myarray, int position, int element)
{
         int i;
         for(i=4;i>=position;i--)
         {
                 myarray[i]=myarray[i-1];
 
         }
         myarray[i]=element;
         return 0;
}


And in the main function you can call this function as below:

#include<stdio.h>
#include<conio.h>
#define MAX 5
void main()
{
  int myarray[5];
  myarray[0]=9; 
  myarray[4]=99;
  traverse(myarray);
  insertelement(myarray,2,99);
  traverse(myarray);
 
getch();
}

When you run the above program you will find 99 has been inserted at the 2nd position as below:

ArrayC10.gif

You will notice all the elements after the 2nd position have been shifted by one place.

Delete in Array

You can delete an element at a given position in an array by using the following algorithm. Let us say:

  1. K: position to delete element
  2. N : Size of the array
  3. A : Name of the array

    ArrayC11.gif

In C you can write a function to delete an element from a specified position as below:
   
int deleteelement(int *myarray,int position)
{
         int i;
         for(i=position; i<5; i++)
         {
                 myarray[i-1]=myarray[i];
         }
         myarray[i-1]=0;
         return 0; 
}

Search in Array

There are two types of searches you can perform on an array:

  1. Linear Search
  2. Binary Search

The binary search works on a divide and conquer algorithm and I will take this topic separate in another article. A Linear search can be performed as below:

int search(int *myarray, int element)
{
         int i;
         int flag=0;
         for(i=0;i<5;i++)
         {
                 if(myarray[i]==element)
                 {
                          flag=1;
                          return i+1;
                          break;
                 }
         }
         if(flag==0)
         {
                 return 0;
         }
}


In this way you can start working with arrays in C. In another article I will show you how to work with pointers and arrays together. Thanks for reading.
 

COMMENT USING
Employers - Post Free Jobs