Arrays

Topics: Array, String theory, Dimension Pages: 5 (1177 words) Published: October 19, 2013
ARRAYS IN C, C++, JAVA

ARRAYS IN C PROGRAMMING
An array in C programming language can be defined as number of memory locations, each of which can store the same data type and which can be referenced through the same variable name.
An array is a collective name given to a group of similar quantities. Eg: 1. Percentage marks of 100 students
2. Age of 25 people

Thus, an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. Usually, the array of characters is called a 'string', whereas an array of integers or floats is simply called an array. All elements of any given array must be of the same type.

DECLARATION OF AN ARRAY:
Arrays must be declared before they can be used in the program. Standard array declaration is as
type variable_name[length of array];
Here type specifies the variable type of the element which is going to be stored in the array. In C Language, arrays starts at position 0. The elements of the array occupy adjacent location in memory.

INITIALIZING ARRAYS:
Initializing of array is very simple in C Programming. The initializing values are enclosed within curly braces in the declaration and placed following an equal sign after the array name. Eg: int array[5]={1,2,3,4,5};//declare and initialize the array

SINGLE DIMENSIONAL ARRAY:
Single dimensional arrays are essentially list of information of one type. Eg: If we wish to declare an array s for a 10 character string, we would write char s[10];

TWO DIMENSIONAL ARRAY:
C allows multidimensional array. Two dimensional array is the simplest form of multidimensional array. To declare a two dimensional integer array 'd' of size 10,20 we would write int d[10][20]; Two dimensional arrays are stored in a row-column matrix where the first index indicates the row and second index indicates the column. This means the rightmost index changes faster than the leftmost sequencing through the array.

MULTIDIMENSIONAL ARRAYS:
Arrays of three or more dimensions are often used because of the amount of memory required to hold them. For eg: A four dimensional character array with dimensions 10,6,9,4 would require 10x6x9x4 or 2160 bytes. If the array were integer, 4320 bytes would be allocated. The required storage increases exponentially with the number of dimensions.

ARRAYS IN C++
C++ provides a data structure, the array, which stores a fixed size sequential collection of elements of the same 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 individual variables, such as n1, n2,....n99, we can declare one array variable such as numbers and use n[1], n[2],...n[99] to represent individual variables.
All arrays consists of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

DECLARING ARRAYS:
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:
type array_name[array size];
This is called a single dimensional array. The array size must be an integer constant greater than zero and type can be any valid C++ data type. For eg: To declare a 10 element array called balance of type double, we will use

double balance[10];

INTIALIZING ARRAYS:
We can initialize C++ array elements either one by one or by using a single statement as follows:
double balance[5]={100.0,2.0,3.4,17.0,50.5};
The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

MULTIDIMENSIONAL ARRAYS:
C++ allows multidimensional arrays. General form of multidimensional array declaration:
type name[size][size]....[size N];
The simplest form of multidimensional array is the two dimensional array. A...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay on Example of Array for It Students
  • A Tutorial on Pointers and Arrays in C Essay
  • Array Sample Program Essay
  • 12 computer science notes ch4D Arrays a Essay
  • Lesson 1c Multidimensional Arrays Essay
  • Essay on 7 and Array
  • Essay about It/218 Week 4 Day 5 Arrays vs Pointers Checkpoint
  • Essay about 9. Get Two Strings from User and Print the Both Strings in Such a Format That It Displays 0 Index Value of First Array Then...

Become a StudyMode Member

Sign Up - It's Free