English 中文(简体)
Computer Programming - Arrays
  • 时间:2024-12-22

Computer Programming - Arrays


Previous Page Next Page  

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

#include <stdio.h>

int main() {
   int number1;
   int number2;
   int number3;
   int number4;
   int number5;
   
   number1 = 10;      
   number2 = 20;   
   number3 = 30;   
   number4 = 40; 
   number5 = 50;     

   printf( "number1: %d
", number1);
   printf( "number2: %d
", number2);
   printf( "number3: %d
", number3);
   printf( "number4: %d
", number4);
   printf( "number5: %d
", number5);
}

It was simple, because we had to store just five integer numbers. Now let s assume we have to store 5000 integer numbers. Are we going to use 5000 variables?

To handle such situations, almost all the programming languages provide a concept called 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.

Instead of declaring inspanidual variables, such as number1, number2, ..., number99, you just declare one array variable number of integer type and use number1[0], number1[1], and ..., number1[99] to represent inspanidual variables. Here, 0, 1, 2, .....99 are index associated with var variable and they are being used to represent inspanidual 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. Given below is a simple syntax to create an array in C programming −

type arrayName [ arraySize ];

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

int number[10];

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

Initiapzing Arrays

You can initiapze an 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 initiapzation 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 the base index and the last index of an array will be the total size of the array minus 1. The following image shows the pictorial representation of the 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 the 10th element from the array and assign the value to var variable. The following example uses 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;
 
   /* Initiapze 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
", 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. Java supports arrays, but there is a pttle difference in the way they are created in Java using the new operator.

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.

pubpc class DemoJava {
   pubpc 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
", i, number[i] );
         i = i + 1;
      }
   }
}

When the 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 pst, which provides similar functionapty as arrays in any other language.

Following is the equivalent program written in Python −

# Following defines an empty pst.
number = []
i = 0

while i < 10:
   # Appending elements in the pst
   number.append(i + 100)
   i = i + 1

i = 0
while i < 10:
   # Accessing elements from the pst
   print "number[", i,  "] = ", number[ i ]
   i = i + 1

When the 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
Advertisements