TECHNOLOGY INSPIRATION
Technology-People-Innovation

Computer Programming - Arrays

Consider a situation, where we need to store 5 integer numbers. If we use programming's simple variable and data type concepts, then we need 5 variables of int data type and program will be something as follows:

  #include <stdio.h>    main()  {     int  number1;     int  number2;     int  number3;     int  number4;     int  number5;          number1 = 10;           number2 = 20;        number3 = 30;        number4 = 40;      number5 = 50;            printf( "number1: %d\n", number1);     printf( "number2: %d\n", number2);     printf( "number3: %d\n", number3);     printf( "number4: %d\n", number4);     printf( "number5: %d\n", number5);  }

It was simple, because we had to store just 5 integer numbers. Now let's assume we have to store 5000 integer numbers, so what is next? Are we going to use 5000 variables?

To handle such situation, almost all the programming languages provide a concept called the array. An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

So instead of declaring individual variables, such as number1, number2, ..., and number99, you just declare one array variablenumber of integer type and use number1[0], number1[1], and ..., number1[99] to represent individual variables. Here, 0, 1, 2, .....99 areindex associated with var variable and they are being used to represent individual elements available in the array.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Arrays in C

Create Arrays

To create an array variable in C, a programmer specifies the type of the elements and the number of elements to be stored in that array. Following is a simple syntax to create an array in C programming:

type arrayName [ arraySize ];

This is called a single-dimensional array. ThearraySize must be an integer constant greater than zero and type can be any valid C data type. For example, now to declare a 10-element array called number of type int , use this statement:

int number[10];

Now, number is a variable array, which is sufficient to hold up to 10 integer numbers.

Initializing Arrays

You can initialize array in C either one by one or using a single statement as follows:

  int number[5] = {10, 20, 30, 40, 50};

The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:

int number[] = {10, 20, 30, 40, 50};

You will create exactly the same array as you did in the previous example. Following is an example to assign a single element of the array:

number[4] = 50;

The above statement assigns element number 5th in the array with a value of 50. All arrays have 0 as the index of their first element which is also called base index and last index of an array will be total size of the array minus 1. Following is the pictorial representation of the same array we discussed above:

Array Presentation

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example:

int var = number[9];

The above statement will take 10th element from the array and assign the value to varvariable. Following is an example, which will use all the above-mentioned three concepts viz. creation, assignment and accessing arrays:

  #include <stdio.h>     int main ()  {     int number[10]; /* number is an array of 10 integers */     int i = 0;        /* Initialize elements of array n to 0 */              while( i < 10 )     {        /* Set element at location i to i + 100 */        number[ i ] = i + 100;        i = i + 1;     }          /* Output each array element's value */     i = 0;     while( i < 10 )     {        printf("number[%d] = %d\n", i, number[i] );        i = i + 1;     }        return 0;  }

When the above code is compiled and executed, it produces the following result:

number[0] = 100  number[1] = 101  number[2] = 102  number[3] = 103  number[4] = 104  number[5] = 105  number[6] = 106  number[7] = 107  number[8] = 108  number[9] = 109  

Arrays in Java

Following is the equivalent program written in Java programming language. Java programming language also supports array, but there is a little difference to create them in different ways using new operator available in Java programming language.

You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.

  public class DemoJava  {         public static void main(String []args)      {        int[] number = new int[10];        int i = 0;                while( i < 10 )        {           number[ i ] = i + 100;           i = i + 1;        }          i = 0;        while( i < 10 )        {           System.out.format( "number[%d] = %d\n", i, number[i] );           i = i + 1;        }     }  }

When above program is executed, it produces the following result:

number[0] = 100  number[1] = 101  number[2] = 102  number[3] = 103  number[4] = 104  number[5] = 105  number[6] = 106  number[7] = 107  number[8] = 108  number[9] = 109  

Arrays (Lists) in Python

Python does not have a concept of Array, instead Python provides another data structure called list, which provides similar functionality as arrays in any other language.

Following is the equivalent program written in Python:

  # Following defines an empty list.  number = []  i = 0    while i < 10:     # Appending elements in the list     number.append(i + 100)     i = i + 1    i = 0  while i < 10:     # Accessing elements from the list     print "number[", i,  "] = ", number[ i ]     i = i + 1  

When above program is executed, it produces the following result:

  number[ 0 ] =  100  number[ 1 ] =  101  number[ 2 ] =  102  number[ 3 ] =  103  number[ 4 ] =  104  number[ 5 ] =  105  number[ 6 ] =  106  number[ 7 ] =  107  number[ 8 ] =  108  number[ 9 ] =  109
Labels:

Post a Comment

[blogger]

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget