# Lesson 12

### Summary

Character Arrays
Initialization Of Character Arrays
Arrays Comparison
Sorting Arrays
Searching arrays
Functions And arrays
Example 1
Multidimensional Arrays
Example 2
Tips
Page 117
Character Arrays

While dealing with words and sentences, we actually make use of character arrays. Up to
now, we were dealing with integer arrays and storing integer values. Here we have to see
what needs to be done for storing a name. A simple variable can't be used to store a name
(which is a string of characters) as a variable stores only a single character. We need a
character array to grab a name. A character array is not different from an integer array.
To declare a character array, we will write as under:
char name [100] ;
In this way, we declare a string or character array. There are some special properties of
character arrays. Suppose that we declare an array of 100 characters. We enter a name
with 15-20 characters. These characters in the array occupy 15-20 character spaces. Now
we have to see what has happened to the remaining character spaces in the array.
Similarly, a question arises, will an array displayed on the screen, show 100 characters
with a name in 15-20 spaces and blanks for the remaining. Here C has a character
handling capability i.e. the notion of strings. When we place a string in a character array,
the computer keeps a mark to identify that the array was of this size while the string
stored in it is of the other size. That marker is a special character, called null character.
The ASCII code of null character is all zeros. In C language, we represent the null
character as “\0”. C uses this character to terminate a string. All strings are terminated
with the null character.
Now, we will see how the character arrays are stored in memory. While declaring a
character array, we normally declare its size larger than the required one. By using a
character array, it becomes easy to store a string. We declare a character array as under.
char name [100] ;
Now we can store a string in this array simply by using the cin statement in the following
way.
cin >> name ;
In the above statement, there is an array on right hand side of cin instead of a simple
variable. The cin stream has a built-in intelligence that allows the compiler (program) to
read whole string at a time rather than a single character as in case of simple variable of
type char. The compiler determines that the name is not a simple variable. Rather it is a
string or character array. Thus cin reads a character array until the user presses the enter
key. When enter key is pressed, cin takes the whole input (i.e. string) and stores it into the
array name. The C language, by itself, attaches a null character at the end of the string. In
this way, the total number of spaces occupied in the array by the string is the number of
characters entered by the user plus 1 (this one character is the null character inserted at
the end of the string by C automatically). The null character is used to determine where
the populated area of the array has ended. If we put a string larger than the size of the
array in absence of a null character in it, then it is not possible to determine where a
string is terminated in the memory. This can cause severe logical error. So, one should be
careful while declaring a character array. The size of array should be one more than the
number of characters you want to store.
Page 118
Initialization Of Character Arrays

Now we will look into integer array initialization process that can provide a list of integer
values separated by commas and enclosed in curly braces. Following is the statement
through which we initialize an integer array.
int age [5] = {12, 13, 16, 13, 14};
If we don’t mention the size of the array and assign a list of values to the array, the
compiler itself generates an array of the size according the number of values in the list.
Thus, the statement int age [] = {14, 15, 13}; will allocate a memory to the array of size
3 integers. These things also apply to character arrays as well. We can initialize an array
by giving a list of characters of the string, the way we assign integer values in integer
array. We write the characters of this string one by one in single quotes (as we write a
single character in single quotes), separated by commas and enclosed in curly braces. So
the initialization line will be as under
char name [100] = {‘i’, ‘m’, ‘r’, ‘a’, ‘n’};
we can also write the string on right hand side in double quotes as
char name [100] = “imran” ;
The easy way to initialize a character array is to assign it a string in double quotes. We
can skip the size of the array in the square brackets. We know that the compiler allocates
the memory at the declaration time, which is used during the execution of the program. In
this case, the compiler will allocate the memory to the array of size equal to the number
of characters in the provided string plus 1 (1 is for the null character that is inserted at the
end of string). Thus it is a better to initialize an array in the following way.
char name [] = “Hello World” ;
In the above statement, a memory of 12 characters will be allocated to the array name as
there are 11 characters in double quotes (space character after Hello is also considered
and counted) while the twelfth is the null character inserted automatically at the end of
the string.
example your name) from keyboard and displaying it on the screen. For this purpose, we
can write the following code segment
char name [100] ;
cin >> name ;
In the cin statement, when the user presses the enter key the previous characters entered,
that is a string will be stored in the array name. Now we have a string in the array name.
We can display it with cout statement. To display the string, we have stored in name. We
can write as under
cout << name ;
This will display the string. Alternatively, we can use a loop to display the string. As the
string is an array of characters, we can display these characters one by one in a 'for loop'.
We can write a loop as under
for ( i = 0 ; i < 100 ; i ++ )
cout << name [ i ] ;
Page 119
Thus this loop will display the characters in the array one by one in each iteration. First, it
will display the character at name [0], followed by that at name [1] and so on. Here we
know that the string in the array is terminated by a null character and after this null
character, there are random values that may not be characters (some garbage data) in the
array. We don’t want to display the garbage data that is in the array after this null
character. While using the statement cout << name; the cout stream takes the characters
of the array name up to the null character and the remaining part of the array is ignored.
When we are displaying the characters one by one, it is necessary to stop the displaying
process at the end of a string (which means when null character is reached). For this
purpose, we may put a condition in the loop to terminate the loop when the null character
is reached. So we can use if statement in the loop to check the null character. We can
modify the above for loop so that it could terminate when null character reaches in the
array.
for ( i = 0 ; i < 100 ; i ++ )
{ if (name [ i ] == ‘\0’)
break ;
cout << name [ i ] ;
}
Here a while loop can also be used instead of a 'for loop'.

### Arrays Comparison

We can use this character-by-character manipulation of the array to compare the
characters of two arrays of the same size. Two arrays can be equal only when first of all
their sizes are equal. Afterwards, we compare the values of the two arrays with one to one
correspondence. If all the values in the first array are equal to the corresponding values of
the second array, then both the arrays will be equal. Suppose, we have two integer arrays

### Example 2

Let’s have a matrix (two-dimensional array) of two rows and three columns. We want to
fill it with values from the user and to display them in two rows and three columns.
Solution

To solve this problem, we use a two-dimensional array of two rows and three columns.
First, we will declare the array by writing
int matrix [2] [3] ;
We declare different variables in our program. To put the values in the array, we use two
nested for loops, which can be written as under.
for ( row = 0 ; row < maxrows ; row ++ )
{
for ( col = 0 ; col < maxcols ; col ++)
{
cout << “Please enter a value for position [“ << row << “, ” << col << ”]” ;
cin >> matrix [row] [col] ;
}
Page 127
}
The inner for loop totals the elements of the array one row at a time. It fills all the
columns of a row. The outer for loop increments the row after each iteration. In the above
code segment, the inner loop executes for each iteration of the outer loop. Thus, when the
outer loop starts with the value of row 0, the inner loop is executed for a number of
iterations equal to the number of columns i.e. 3 in our program. Thus the first row is
completed for the three columns with positions [0,0], [0,1] and [0,2]. Then the outer loop
increments the row variable to 1 and the inner loop is again executed which completes
the second row (i.e. the positions [1,0], [1,1] and [1,2] ). All the values of matrix having
two rows and three columns are found.
Similarly, to display these values one by one, we again use nested loops.
Following is the code of the program.
//This program takes values from user to fill a two-dimensional array (matrix) having two
//rows and three columns. And then displays these values in row column format.
# include <iostream.h>
main ( )
{
int matrix [2] [3], row, col, maxrows = 2, maxcols = 3 ;
// get values for the matrix
for ( row = 0 ; row < maxrows ; row ++)
{
for (col = 0 ; col < maxcols ; col ++)
{
cout << “Please enter a value for position [“ << row << “, ” << col << ”] ” ;
cin >> matrix [row] [col] ;
}
}
// Display the values of matrix
cout << “The values entered for the matrix are “ << endl ;
for ( row = 0 ; row < maxrows ; row ++)
{
for (col = 0 ; col < maxcols ; col ++)
{
cout << “\t” << matrix [row] [col] ;
}
cout << endl ; //to start a new line for the next row
}
}
A sample output of the program is given below.
Page 128
Please enter a value for position [0,0] 1
Please enter a value for position [0,1] 2
Please enter a value for position [0,2] 3
Please enter a value for position [1,0] 4
Please enter a value for position [1,1] 5
Please enter a value for position [1,2] 6
The values entered for the matrix are
1 2 3
4 5 6

### Tips

A character array can be initialized using a string literal
Individual characters in a string stored in an array can be accessed directly
using array subscript
Arrays are passed to functions by reference
To pass an array to a function, the name of the array(without any brackets)
is passed along with its size
To receive an array, the function’s parameter list must specify that an
Including variable names in function prototype is unnecessary. The
compiler ignores these names.