A single-dimensional array is the simplest form of an array that requires only one subscript to access an array element. Like an ordinary variable, an array must have been declared before it is used in the program. The syntax for declaring a single-dimensional array is
data_type array_name [size] ;
For example, an array marks of type int and size five can be declared using this statement.
int marks [5];
Initialization of Single- Dimensional Array: Once an array is declared, the next step is to initialize each array element with a valid and appropriate value. Note that unless an array is initialized, all the array elements contain garbage values. An array can be initialized at the time of its declaration. The syntax for initializing an array at the time of its declaration is
data_type array_name [size] ={value 1,value 2,…………… ,value n};
The values are assigned to the array elements in the order in which they are listed.
That is, value1, value 2 and value n are assigned to the first, second and nth element of the array, respectively.
For example, the array marks can be initialized while declaring using this statement.
int marks[5]={51,62,43,74,55};
If an array is declared and initialized simultaneously, then specifying its size is optional. For example, the statement int marks [] = {51, 62,43,74,55} is also valid.
Accessing Single-Dimensional Array Elements: Once an array is declared and initialized, the values stored in the array can be accessed any time. Each individual array element can be accessed using the name of the array and the subscript value. Every element in an array is associated with a unique subscript value, starting from 0 to size-l (where, size refers to the maximum number of elements that can be stored in the array). The syntax for accessing the values stored in a single dimensional array is
array_name [subscript]
For examp1e, the elements of the array marks can be referred to as marks [0], marks [1],
Marks [2], marks [3] and marks [4], respectively.
Single-dimensional arrays are always allocated contiguous blocks of memory. This implies that all the elements in an array are always stored next to each other. The memory representation of the array marks is shown in Figure. As each element is of the type int (that is, 2 bytes long), the array marks occupies ten contiguous bytes in memory and these bytes are reserved in the memory at the compile-time.
Manipulation of Single-Dimensional Array Elements: Once the array elements are accessed, a number of operations can be performed on them. These operations include finding the sum, average, maximum or minimum, sorting and searching of the array elements, etc.
To understand the concept of single-dimensional arrays, consider this example.
Example: A program to sort the array elements
#include<iostream> #include<iomanip> //for setw() using namespace std; int main () { const int size = 5; //declaring size of array int num[size],temp; //declaring an array cout<<”Enter five elements of array”<<”\n"; for (int i=0; i<size; i++) //reading array elements cin>>num[i]; for(int i=0;i<size-1;i++) //sorting array elements { for(int j=i+l;j<size;j++) { if(num[i]>num[j]) { temp = num[i]; num[i] = num[j]; num[j] = temp; } } } cout<<”Elements of sorted array are\n"; for(int i=0 ; i<size ; i++) cout<<num[i]<<setw(4) ; return 0; }
The output of the program is
Enter five elements of array
67 34 12 98 26
Elements of sorted array are
12 26 34 67 98
Strings-Array of Characters: In addition to integer and floating point elements, an array can have characters as its elements. An array containing a group of characters is known as a character array. For example, an array name of type char is declared and initialized with five characters using this statement.
char name[5]={, a' , ' b' , 'c' , ,d' , 'e' };
An array of characters in which the last character is always a null character (‘ \ 0’) represents a string. Thus, while declaring a character array that holds a string, the size is always declared one more than the number of characters in the string. This type of string representation is also referred to as C-string or null-terminated string. Note that a string not terminated by ‘\ 0’ is treated merely as a collection of characters and not a string. Besides C-strings, C++ also defines a string class, named string to offer object oriented approach to string handling.
Initializing Strings: A string is declared and initialized in the same way as a character array. The syntax for initializing a string at the time of its declaration is
char string_name[size]=”value”;
To understand the concept of string initialization while declaring, consider this statement.
char str[]=”Express”;
In this statement, the string str is initialized with a string constant Express. Note that in this type of initialization, the null character ‘\ 0’ is not specified, the compiler inserts the null character automatically. However, the string str can also be initialized element-wise using this statement.
char str[]={'E', 'x', 'p', 'r', 'e', 's', 's', '\0'};
In this type of initialization, the null character is specified explicitly to denote the end of string. Figure shows memory representation of the string str, where each character occupies one byte in the memory.
Manipulating Strings: In C++, strings can be manipulated in different ways. There is a set of pre-defined functions, which are used to manipulate strings in different manners. These pre-defined functions, which are declared in the string. h (or cstring header in new C++ compilers) header file, are listed in Table.
To understand the concept of strings and string operations, consider this example.
Example : A program to demonstrate string functions
#include<iostream> #include<cstring> // for string functions using namespace std; int main () { const int max=80; // maximum size of string char str1 [max], str2 [max]; cout<<”Enter first string: “; cin>>str1; // reading first string cout<<”Enter second string: “; cin>>str2; // reading second string cout<<”Length of first string: “<<strlen(str1) <<"\n"; cout<<”Length of second string: “<<strlen(str2) <<"\n"; if(!strcmp(str1,str2)) // comparing strings cout<<”Strings are equal “<<"\n"; else cout<<”Strings are not equal”<<”\n”; cout<<” Concatenated string: "<<strcat(str1,str2); return 0; }
The output of the program is
Enter first string : United
Enter second string : Kingdom
Length of first string: 6
Length of second string: 7
Strings are not equal
Concatenated string: United Kingdom
In this example, two strings str1 and str2 are read and different string functions are performed on them. The strlen () determines the lengths of strings, strcmp () compares the strings and strcat () concatenates the strings.
Note that, in the statement cin>>str1 the extraction operator >> considers a space to be a terminating character, thus it is unable to read strings embedded with blanks and multiple lines. For such strings, member functions of the standard input stream (cin) are required.