Arrays in C++


“An array is a kind of data structure which allows a unique name to be given to a collection of numbered boxes or group of elements of the same type”. An individual element or box of an array is referred by its own unique subscript. Each element or box is ordered by the index or subscript. Array is the basic underlying mechanism for storing multiple values. The main purpose of array is when we need to look at the data more than once. In program examples in other articles at Byte-Notes, each [url=]variable[/url]name has been associated with a single memory cell and data type. It is extremely difficult if a program needs to have, say 20 variables of the same type with a single cin statement.

For Example:

int var1, var, …, var20;


Cin>>var1>>var2>>,…,>>var20; Let us say, in these variables from var1 to var20, we want to find a value 100, we would write a statement like; If(var1 == 100) || (var2 == 100) ||,…,|| (var20 == 100) Cout It is difficult for a programmer and time consuming job to handle a kind of job discussed above. Sometimes the programmer wants to assign more than one values to a single variable name, which can contain a series of values. A technique is known as array, which is part of data structure that allow us to manipulate data more easily and efficiently. In other words, array is a very useful tool, which gives the power to srtore multiple values of the same type in just one variable. Almost every programming language supports some form of array as a data structure. If a programming language offers any data structure, it is most likely offers one-dimensional array. Arrays in c++ are easy to implement and to use with a little basic concept, which is a bit different that other languages. We will catch each and every point as we move forward. “An Array is a group of elements and all elements of the same type and referenced by a common name. An array can be one dimension or of multiple dimensions”.

The method of accessing an element of an array is to use a technique called subscripting. There are two types associated with array, the base-type and the index-type. The first one is the type of the elements that is each element is itself a data structure and the second one is the type of the values used to access individual elements of the array that is any element can be selected by specifying its position in the sequence with a subscript.

For Example:

int item [5]; This specified that a variable item is an array whose index-type is the sub range type from 0 to 4 and whose base-type is int (integer). The base-type may be any valid type as well as index-type.

The Operation:

Shows that item is an array and i its index-type that returns an element of the base-type. This operation is called the retrieval and must be given a subscript. The second basic operation is the storing that accepts an array; an element of its index-type and an element of its base-type. The assignment statement item[i] = val; is called the storing operation. In this case, a value and a subscript must be given. After the execution of the assignment statement, the contents of item [i] are the contents of the variable with the appropriate value.

Array Bounds Checking:

[i][i][i][i]Let us illustrate why bounds checking are important. If there is no check, there is a possibility to exceed the size of array. The reference to an element beyond the declared size will simply be placed in memory outside the array, which will lead to an unpredictable result. The compiler will not signal the user for any diagnostic by crossing the boundaries. So it is a good programming practice to include a condition to warn the user that you are going beyond the array size. In doing so, in some cases we can prevent the system from just hanging. The program execution may or may not hang which varies from system to system. If does not hang, it will store the valuse outside the array or probably on the program itself, in case of no bounds checking.[/i][/i][/i][/i]

  1. //program name: bcheck.cpp
  2. #include
  3. #incolude
  4. #include
  5. const MAX = 10;
  6. int main()
  7. {
  8. int intarr[MAX], i, size;
  9. clrscr();
  10. cout
  11. cin>>size;
  12. cout
  13. if ( size > MAX)
  14. cout
  15. else
  16. {
  17. cout
  18. for (i=0; i
  19. {
  20. intarr = i;
  21.  cout
  22. }
  23. }
  24. return 0;
  25. }

[i]Run OutPut:

Enter the size of Array size = 12 —out of range Enter the size of Array size = 10—within the range 0 1 2 3 4 5 6 7 8 9

Leave a Comment