Chapter 1 - Array and Strings
Chapter 1 - Array and Strings
Chapter One
1. Arrays and Strings
1.1 What is An Array?
Properties of arrays:
Arrays in C++ are zero-bounded; that is the index of the first element in the
array is 0 and the last element is N-1, where N is the size of the array.
It is illegal to refer to an element outside of the array bounds, and your
program will crash or have unexpected results, depending on the compiler.
Array can only hold values of one type
Array declaration
Declaring the name and type of an array and setting the number of
elements in an array is called dimensioning the array. The array must be
declared before one uses in like other variables. In the array declaration
one must define:
1. The type of the array (i.e. integer, floating point, char etc.)
2. Name of the array,
3. The total number of memory locations to be allocated or the maximum value
of each subscript. i.e. the number of elements in the array.
Page 1 of 15 MU
Fundamentals of Programming II Chapter Two: Array and Strings
Note: array size cannot be a variable whose value is set while the program is running.
Thus to declare an integer with size of 10 having a name of num is:
That means, we can store 10 values of type int without having to declare
10 different variables each one with a different identifier. Instead of that,
using an array we can store 10 different values of the same type, int for
example, with a unique identifier.
Initializing Arrays
The above declaration would have created an array like the following one:
set 5 different values, one for each element. If we ignore the last initial
value (12071) in the above initialization, 0 will be taken automatically for
the last array element.
Because this can be considered as useless repetition, C++ allows the
possibility of leaving empty the brackets [ ], where the number of items in
the initialization bracket will be counted to set the size of the array.
The compiler will count the number of initialization items which is 6 and set
the size of the array day to 6 (i.e.: day[6])
You can use the initialization form only when defining the array. You cannot
use it later, and cannot assign one array to another once. I.e.
Note: when initializing an array, we can provide fewer values than the array
elements. E.g. int a [10] = {10, 2, 3}; in this case the compiler sets the
remaining elements to zero.
In any point of the program in which the array is visible we can access
individually anyone of its elements for reading or modifying it as if it was a
normal variable. To access individual elements, index or subscript is used.
The format is the following:
name [ index ]
In c++ the first element has an index of 0 and the last element has an
index, which is one less the size of the array (i.e. arraysize-1). Thus, from
the above declaration, day[0] is the first element and day[4] is the last
element.
Following the previous examples where day had 5 elements and each
element is of type int, the name, which we can use to refer to each
element, is the following one:
For example, to store the value 75 in the third element of the array variable
day a suitable sentence would be:
Page 3 of 15 MU
Fundamentals of Programming II Chapter Two: Array and Strings
And, for example, to pass the value of the third element of the array
variable day to the variable a , we could write:
a = day[2];
Therefore, for all the effects, the expression day[2] is like any variable of
type int with the same properties. Thus an array declaration enables us to
create a lot of variables of the same type with a single declaration and we
can use an index to identify individual elements.
Notice that the third element of day is specified day[2] , since first is
day[0] , second day[1] , and therefore, third is day[2] . By this same
reason, its last element is day [4]. Since if we wrote day [5], we would be
acceding to the sixth element of day and therefore exceeding the size of
the array. This might give you either error or unexpected value depending
on the compiler.
In C++ it is perfectly valid to exceed the valid range of indices for an Array,
which can cause certain detectable problems, since they do not cause
compilation errors but they can cause unexpected results or serious errors
during execution. The reason why this is allowed will be seen ahead when
we begin to use pointers.
At this point it is important to be able to clearly distinguish between the two
uses the square brackets [ ] have for arrays.
o One is to set the size of arrays during declaration
o The other is to specify indices for a specific array element when
accessing the elements of the array
We must take care of not confusing these two possible uses of brackets [ ]
with arrays:
Eg: int day[5]; // declaration of a new Array (begins with a type name)
day[2] = 75; // access to an element of the Array.
{
result += day[n];
}
cout << result;
getch();
Multidimensional Arrays
Multidimensional arrays can be described as arrays of arrays. For example, a
bi-dimensional array can be imagined as a bi-dimensional table of a uniform
concrete data type.
Matrix represents a bi-dimensional array of 3 per 5 values of type int . The way
to declare this array would be:
int matrix[3][5];
For example, the way to reference the second element vertically and fourth
horizontally in an expression would be:
matrix[1][3]
You can initialize multidimensional arrays. You assign the list of values to
array elements in order, with the last array subscript changing while each of
the former holds steady. Therefore, if you have an array
Page 5 of 15 MU
Fundamentals of Programming II Chapter Two: Array and Strings
int theArray[5][3]
You initialize this array by writing
int theArray[5][3] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }
For the sake of clarity, you could group the initializations with braces. For
example,
int theArray[5][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}, {13,14,15} };
The compiler ignores the inner braces, which make it easier to understand
how the numbers are distributed. Each value must be separated by a comma,
without regard to the braces. The entire initialization set must be within
braces, and it must end with a semicolon .
Multidimensional arrays are not limited to two indices (two dimensions). They
can contain so many indices as needed, although it is rare to have to represent
more than 3 dimensions
Multidimensional arrays are nothing else than an abstraction, since we can
simply obtain the same results with a simple array by putting a factor between
its indices:
With the only difference that the compiler remembers for us the depth of each
imaginary dimension. Serve as example these two pieces of code, with exactly
the same result, one using bi-dimensional arrays and the other using only
simple arrays:
// multidimensional array
#include <iostream.h>
#define WIDTH 5
#define HEIGHT 3
int matrix [HEIGHT][WIDTH];
int n,m;
int main ()
{
for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++)
{
matrix [n][m]=(n+1)*(m+1);
}
return 0;
}
None of the programs above produce any output on the screen, but both
assign values to the memory block called matrix in the following way:
Page 6 of 15 MU
Fundamentals of Programming II Chapter Two: Array and Strings
In all programs and concepts we have seen so far, we have used only
numerical variables, used to express numbers exclusively. But in addition to
numerical variables there also exist strings of characters that allow us to
represent successive characters, like words, sentences, names, texts, etc. Until
now we have only used them as constants, but we have never considered
variables able to contain them.
In C++ there is no specific elementary variable type to store string of
characters. In order to fulfill this feature we can use arrays of type char, which
are successions of char elements. Remember that this data type (char) is the
one used to store a single character, for that reason arrays of them are
generally used to make strings of single characters.
For example, the following array (or string of characters) can store a string up
to 20 characters long. You may imagine it thus:
name
This maximum size of 20 characters is not required to be always fully used. For
example, name could store at some moment in a program either the string of
characters "Hello" or the string "studying C++”. Therefore, since the array of
characters can store shorter strings than its total length, there has been
reached a convention to end the valid content of a string with a null character,
whose constant can be written as '\0’.
We could represent name (an array of 20 elements of type char) storing the
strings of characters "Hello" and "Studying C++" in the following way:
H e l l 0 \0
S t u d y i n g C + + \0
Page 7 of 15 MU
Fundamentals of Programming II Chapter Two: Array and Strings
Notice how after the valid content it is included a null character ( '\0') in order to
indicate the end of string. The empty cells (elements) represent indeterminate
values.
Initialization of Strings
Because strings of characters are ordinary arrays they fulfill same rules as any
array. For example, if we want to initialize a string of characters with
predetermined values we can do it in a similar way to any other array:
In both cases the Array or string of characters mystring is declared with a size
of 6 characters (elements of type char ): the 5 characters that compose Hello
plus a final null character ( '\0' ) which specifies the end of the string and that,
in the second case, when using double quotes ( " ) it is automatically
appended.
Before going further, you should note that the assignation of multiple constants
like double-quoted constants ( " ) to arrays are only valid when initializing the
array, that is, at the moment when declared.
The following expressions within a code are not valid for arrays
mystring="Hello";
mystring[] = "Hello";
neither would be: mystring = { 'H', 'e', 'l', 'l', 'o', '\0' };
So remember: We can "assign" a multiple constant to an Array only at the
moment of initializing it. The reason will be more comprehensible when you
know a bit more about pointers, since then it will be clarified that an array is
simply a constant pointer pointing to an allocated block of memory. And
because of this constant feature, the array itself cannot be assigned any value,
but we can assign values to each of the elements of the array.
Page 8 of 15 MU
Fundamentals of Programming II Chapter Two: Array and Strings
Page 9 of 15 MU
Fundamentals of Programming II Chapter Two: Array and Strings
Notice how in both calls to cin.getline we used the same string identifier (
mybuffer ). What the program does in the second call is simply step on the
previous content of buffer by the new one that is introduced.
If you remember the section about communication through console, you will
remember that we used the extraction operator (>> ) to receive data directly
from the standard input. This method can also be used instead of cin.getline
with strings of characters. For example, in our program, when we requested an
input from the user we could have written:
This would work, but this method has the following limitations that cin.getline
has not:
It can only receive single words (no complete sentences) since this method uses as
delimiter any occurrence of a blank character, including spaces, tabulators,
newlines and carriage returns.
It is not allowed to specify a size for the buffer. What makes your program unstable
in case that the user input is longer than the array that will host it.
All of these functions admit one parameter and return a value of the requested
type ( int , long or float ). These functions combined with getline method of
cin are a more reliable way to get the user input when requesting a number
than the classic cin>> method:
// cin and ato* functions
#include <iostream.h>
#include <stdlib.h>
int main()
{
char mybuffer[100];
float price;
int quantity;
Page 10 of MU
15
Fundamentals of Programming II Chapter Two: Array and Strings
b) String Concatenation:
Example:
Although we can always write a simple function like the following with the
same purpose like strcpy :
// setting value to string
#include <iostream.h>
void namecopy(char dest[], char source[])
{
int c = 0;
while(source[c] != ‘\0’)
{
dest[c] = source[c];
c++;
}
dest[c] = ‘\0’;
cout<< “\n your name after copying : ”<<dest;
}
void main()
{
char name[10],dest[10];
Page 12 of MU
15
Fundamentals of Programming II Chapter Two: Array and Strings
d) String Compare:
Compares the two string string1 and string2.
Both string compare functions returns three different values:
o Returns 0 is the strings are equal
o Returns negative value if the first is less than the second string
o Returns positive value if the first is greater than the second string
Example
Page 13 of MU
15
Fundamentals of Programming II Chapter Two: Array and Strings
Worksheet No 1:
Page 14 of MU
15
Fundamentals of Programming II Chapter Two: Array and Strings
14.Develop a C++ program that accepts the name of a person and then counts how many vowels the
person’s name have.
15.Modify the question in Q14 in such a way that it should replace vowel characters with * in the
person name.
16.Write a program in C++ which read a three digit number and generate all the possible permutation
of numbers using the above digits. For example n = 123 then the permutations are – 123, 213,
312, 132, 231, 321
17.Write a program which read a set of lines until you enter #.
18.Write a program which read two matrixes and then print a matrix which is addition of these two
matrixes.
19.Write a program which reads two matrix and multiply them if possible
20.Write a program which reads a 3 x 2 matrix and then calculates the sum of each row and store that
in a one dimension array.
Page 15 of MU
15