Programming Fundamentals
Arrays & Strings
Arrays
• Variables we declared in our earlier programs can
store a single data item of the specified type
• We can have a variable that stores an integer or a
variable that stores a character
• An array can store several data items of the same
type
• We can have an array of integers, an array of
characters – in fact an array of any type of data
Programming Fundamentals- Arrays & 2
Strings
Arrays
• An Array is a series of elements (variables) of the
same type placed consecutively in memory that can
be individually referenced by adding an index to a
unique name
Programming Fundamentals- Arrays & 3
Strings
Arrays
• For example: We can store 5 values of type int without
having to declare 5 different variables each one with a
different identifier
• Instead of that, using an array we can store 5 different
values of the same type int with one unique identifier
(variable name)
Programming Fundamentals- Arrays & 4
Strings
Arrays
An array containing 5 integer values of type int
called myDataArray
0 1 2 3 4
myDataArray
Each element of type int
Programming Fundamentals- Arrays & 5
Strings
Declaration of Array
• An array must be declared before it is used
• A typical declaration for an array in C++ is:
– type array_name [no_of_elements];
– For example:
int myDataArray[5];
Programming Fundamentals- Arrays & 6
Strings
Declaration of Array
• The elements field within brackets [ ] when declaring
an array must be a constant value
• Since arrays are blocks of static memory of a given
size and the compiler must be able to determine
exactly how much memory must assign to the array
before any instruction is considered.
Programming Fundamentals- Arrays & 7
Strings
Initializing Arrays
• when we declare an Array, we have the
possibility to assign initial values to each one
of its elements using curly brackets { }.
– For example:
int myDataArray[5]={34,21,77,5,120};
Programming Fundamentals- Arrays & 8
Strings
Initializing Arrays
int myDataArray[5]={34,21,77,5,120};
0 1 2 3 4
myDataArray 34 21 77 5 120
Programming Fundamentals- Arrays & 9
Strings
Accessing the values of an Array
• at any point of the program in which the array
is visible we can access individually anyone
of its values for reading or modifying it as if it
was a normal variable
• The format is the following:
– array_name[index];
Programming Fundamentals- Arrays & 10
Strings
Accessing the values of an Array
• Example:
– To store the value 75 in the third element of
myDataArray, statement would be:
myDataArray[2] = 75;
– To pass the value of the third element of
myDataArray to the variable a, we could write:
a = myDataArray[2];
Programming Fundamentals- Arrays & 11
Strings
Setting Array elements to Zero
• To avoid junk values, its easy to initialize a
whole array elements to zero
• For Example,
double junk[size] = {0};
Programming Fundamentals- Arrays & 12
Strings
Defining Array Size with the Initializer
List
• We can omit the size of the array in the
declaration of an array, provided we supply
initializing values
• Number of elements in the array will then be
same as the initializing values
• For example,
int values[] = {2,3,4};
Programming Fundamentals- Arrays & 13
Strings
Finding the Number of Array
Elements
• sizeof() operator can supply the number of bytes that
a variable occupies
• We can use it to find out total number of elements in
the array, for example
• sizeof(values) returns the total number of bytes used
by the array, sizeof(values[0]) returns number of
bytes used by the first element of the array
int values [] = {1,2,3,4,5};
Programming Fundamentals- Arrays & 14
Strings
Arrays of Characters
• An array of characters can have a dual personality
• It can simply be an array of characters where each
element stores one character, or
• It can represent a string – each element in the array
is stored as a separate array element
• The end of the string is indicated by a special string
termination character ‘\0’ called null character
Programming Fundamentals- Arrays & 15
Strings
Declaring and Initializing the
Array of Characters
char vowel[5]={‘a’,’e’,’i’,’o’,’u’};
• Each element in the array is initialized with the
corresponding character from the initializer list
• Like numeric arrays, we can leave it to compiler to
set the size of the array
char vowel[]={‘a’,’e’,’i’,’o’,’u’};
Programming Fundamentals- Arrays & 16
Strings
Declaring and Initializing the Array of
Characters
• We can also declare an array of characters and
initialize it with a string literal
• For example,
char name[12]=“Ali Ahmed”;
• Since we are initializing an array with a string literal,
the null character ‘\0’ will be appended to the end of
the characters in the string
Programming Fundamentals- Arrays & 17
Strings
Declaring and Initializing the Array of
Characters
• Once again, we can leave it to the compiler to set the
size of the array
char name[]=“Ali Ahmed”;
• The array will have ten elements, nine elements to
store the characters and one extra element to store
the string termination character ‘\0’
• To display a string stored in an array, we just write the
name of the array, for example,
cout<<name;
Programming Fundamentals- Arrays & 18
Strings
Declaring and Initializing the Array of
Characters
• We use getline() function to read the string
• By using
cin>> text;
• We could certainly read characters into it but only up
to first space
• Extraction operator(>>) treats a space as a delimiter
between input values and it does not read an entire
string containing spaces
Programming Fundamentals- Arrays & 19
Strings
Declaring and Initializing the Array of
Characters
• The getline() function for the cin stream reads in and
stores a whole line of characters, including spaces
• The input ends when a newline character, ‘\n’, is read
– i.e. when we press enter button
• In our example, we passed two arguments to the
getline() function, i.e.
getline(text, maxLength)
Programming Fundamentals- Arrays & 20
Strings
Declaring and Initializing the Array of
Characters
getline(text, maxLength)
• First argument specifies the location in memory to
store the input
• Second argument is the maximum number of
characters to be stored
• This count also includes the string termination
character ‘\0’ which will be automatically appended to
the end of the string
• In fact getline() function takes three arguments – third
one being the alternative character to ‘\n’, for
example
Programming Fundamentals- Arrays & 21
Strings
Declaring and Initializing the Array of
Characters
getline(text, maxLength, ‘!’)
• When you are done with the input just enter ‘!’
• Of course, the total number of characters are
still limited to maxLength
Programming Fundamentals- Arrays & 22
Strings
Multidimensional Arrays
• Arrays we declared so for have required a single
index value to select an element – that is called one-
dimensional array
• It is so called because varying one index can
reference all the elements
• We can also declare arrays that require two or more
separate index values to access an element
• These arrays are called multidimensional arrays
Programming Fundamentals- Arrays & 23
Strings
Multidimensional Arrays
• An array that requires two index values to
reference an element is called two-dimensional
array, an array that requires three index values to
reference an element is called three-dimensional
array and so on…
Programming Fundamentals- Arrays & 24
Strings
Multidimensional Arrays
• A good example of a two-dimensional array is a chess
board. One dimension represents the eight rows; the other
dimension represents the eight columns
• Therefore, declaration of an array named board would be
int board[8][8];
• You could also represent the same data with a one-
dimensional, 64-square array. For example,
int board[64];
• But this doesn't correspond as closely to the real-world
object as two-dimensional
Programming Fundamentals- Arrays & 25
Strings
Initializing Multidimensional Arrays
int myArray[5][3]={1,2,3,4,5,
6,7,8,9,10,11,12,13,14,15};
• First 3 elements go to first row, next 3 elements go to
second row and so on…
• Two-dimensional array is an array of one-dimensional
array
• The initializing values for a one-dimensional array are
written between braces { } and separated by commas
(,)
Programming Fundamentals- Arrays & 26
Strings
Initializing Multidimensional
Arrays
• For the sake of clarity, we could group the
initializations with braces. For example,
int myArray[5][3] = {
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12},
{13,14,15} };
• The compiler ignores the inner braces, which makes it
easier to understand that how the numbers are distributed
Programming Fundamentals- Arrays & 27
Strings
Multidimensional Character Arrays
• We can declare arrays of two or more dimensions to
hold data of any type
• A two-dimensional array of type char is an array of
strings
• When we initialize a two-dimensional array of type
char with character strings between double quotes,
we don’t need the braces around the string for a row
• For example,
Programming Fundamentals- Arrays & 28
Strings
Multidimensional Character
Arrays
char stars[4][20]= {
“Ahmed Shahzad”,
“Ali Ahmed”,
“Khalid Saleem”,
“Ali Imran” };
Programming Fundamentals- Arrays & 29
Strings