You are on page 1of 20

CoSc 1012

Computer
Programming

Instructor: Welde Janfa


Outline

● Array Definition
● Array referencing
Chapter 5 ● One dimensional and
Arrays and multidimensional
String arrays
● Strings: Definition,
Manipulation accessing Strings
● An array is a series of elements of the same
type placed in contiguous memory locations
● Individual elements can be referenced by an
index to a unique identifier.
● For example, five values of type int can be
declared as an array without having to declare
Array Definition 5 different variables (each with its own
identifier).
● Using an array, the five int values are stored in
contiguous memory locations, and all five can
be accessed using the same identifier, with the
proper index.
● Like a regular variable, an array must be
declared before it is used.
● A typical declaration for an array in C++ is:

type name [elements];


3
● Syntax :-
dataType arrayName[arraySize];
● For example :-
int age [5];
0 1 2 3 4
Array Declaration
and Initialization int age

Here,
● int - type of element to be stored
● age - name of the array
● 5 - size of the array

4
Few Things to Remember:

int x[6]

● The array indices start with 0. Meaning x[0] is the

first element stored at index 0.

Array Declaration ● If the size of an array is n, the last element is stored

at index (n-1). In this example, x[5] is the last


and Initialization element.

● Elements of an array have consecutive addresses.

For example, suppose the starting address of x[0]

is 2120d. Then, the address of the next element

x[1] will be 2124d, the address of x[2] will be

2128d and so on.

● Here, the size of each element is increased by 4.

This is because the size of int is 4 bytes. 5


In C++, it's possible to initialize an array

● During declaration. For example,

int x[6] = {19, 10, 8, 17, 9, 15};


C++ Array
Another method to initialize array
Initialization during declaration:

int x[] = {19, 10, 8, 17, 9, 15};

Here, we have not mentioned


the size of the array. In such
cases, the compiler
automatically computes the
size.

6
C++ Array With Empty Members

● In C++, if an array has a size n, we


can store upto n number of elements
C++ Array in the array. However, what will
Initialization happen if we store less than n
number of elements.
● For example, store only 3 elements in
the array

int x[6] = {19, 10, 8};

7
● The values of any of the elements in an array
can be accessed just like the value of a regular
variable of the same type.
● The syntax is:
name[index]
Accessing the ● Following the previous examples in which age
had 5 elements and each of those elements
value of an Array was of type int, we can access the value as the
following example :-
○ For example, the following statement
stores the value 75 in the third element of
age:
age [2] = 75;
○ and, for example, the following copies the
value of the third element of age to a
variable called x:
x = age[2];
8
● The base address of an array is the address
(that is, the memory location) of the first
array component.
● For example, if list is a one-dimensional
array, then the base address of
Memory Address of an Array list is the address of the component list[0].
Consider the following statements:
int myList[5];

C++ Array Out of Bounds

● If we declare an array of size 5, then the


array will contain elements from index 0 to
4.
● However, if we try to access the element at
index 5 or more than 5, it will result in
Undefined Behaviour.
9
● Multidimensional arrays can be described
as "arrays of arrays".
● For example, a bi-dimensional array can be
imagined as a two-dimensional table made
of elements, all of them of a same uniform
Multidimensional data type
1 2 3 4
arrays 1

● A bi-dimensional array of 3 per 5 elements


of type int.
10
● The C++ syntax for this is:
datatype name [size1 ][size2];
○ dataType: type to which the
multidimensional array belongs
○ Name: name of the multidimensional
Multidimensional array
○ Size1…size2: size of each dimension
arrays ● For example
○ int period [4][5]
○int b [5][6]

11
● For example, the way to reference the
second element vertically and fourth
horizontally in an expression would be:
○ Period [1][2]
Multidimensional ● Note:-
(remember that array indices always
arrays begin with zero).
● Multidimensional arrays are not limited
to two indices (i.e., two dimensions).
● They can contain as many indices as
needed.
● For example:
char century [100][365][24][60][60];

12
⚫ At some point, we may need to pass an
array to a function as a parameter.
⚫ In C++, it is not possible to pass the entire
block of memory represented by an array
to a function directly as an argument. But
what can be passed instead is its address. In
Arrays as practice, this has almost the same effect,
and it is a much faster and more efficient
parameters operation.
⚫ To accept an array as parameter for a
function, the parameters can be declared as
the array type, but with empty brackets,
omitting the actual size of the array. For
example:
void procedure (int arg[])
This function accepts a parameter of type
"array of int" called arg. 13
Strings
● is a collection of characters.
For example

Strings: Definition, “Abcdefjh”,


“Abera kebede”
accessing Strings “My C++ class
● There are two types of strings commonly

used in C++ programming language:


○ C-strings (C-style Strings- character
sequence)
○ Strings that are objects of string class
(The Standard C++ Library string class)
14
● Character sequences
○ C++ has a powerful string class to
handle and manipulate strings of
characters.
○ However, because strings are, in fact,
Strings: Definition, sequences of characters, we can
represent them also as plain arrays
accessing Strings of elements of a character type.
○ For example, the following array:
char name [20];
○ is an array that can store up to 20
elements of type char.
○ Therefore, this array has a capacity
to store sequences of up to 20
characters. But this capacity does
not need to be fully exhausted
15
⚫ C-type string (Character sequences)
◦ In C programming, the collection of
characters is stored in the form of arrays,
this is also supported in C++
programming. Hence it's called C-strings.
◦ C-strings are arrays of type char
terminated with null character, that is, \0
(ASCII value of null character is 0).
Strings: Definition,
◦ Therefore, the array of char elements
accessing Strings called myword can be initialized as
char myword[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword[ ] = "Hello";
◦ In both cases, the array of characters
myword is declared with a size of 6
elements of type char: the 5 characters
that compose the word "Hello", plus a final
null character ('\0'), which specifies the end of
the sequence

16
● Character sequences
○ char name [20];
○ For example, either the sequence "Hello" or
the sequence "Merry Christmas" can be
stored in name, since both would fit in a
sequence with a capacity for 20 characters.
Strings: Definition, ○ By convention, the end of strings represented
in character sequences is signaled by a special
accessing Strings character: the null character, whose literal
value can be written as '\0' (backslash, zero).
○ Because arrays of characters are ordinary
arrays, they follow the same rules as these.
○ For example, to initialize an array of
characters with some predetermined
sequence of characters, we can do it just like
any other array:
char myword[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };

17
⚫ Character sequences
◦ In fact, because string literals are regular
arrays, they have the same restrictions
as these, and cannot be assigned values.
◦ Expressions (once myword has already
been declared as above), such as:
Strings: Definition, myword = "Bye";
myword[ ] = "Bye";

accessing Strings ◦ would not be valid, like neither would


be:
myword = { 'B', 'y', 'e', '\0' };
◦ This is because arrays cannot be
assigned values. Note, though, that each
of its elements can be assigned a value
individually.
◦ For example, this would be correct:
● myword[0] = 'B';
● myword[1] = 'y';
● myword[2] = 'e';
● myword[3] = '\0';
18
● C++ Strings
○ standard C++ library provides a string class type
that supports all the operations mentioned above.
○ The library included as
Include <string>
Strings: Definition, #include <iostream>
#include <string>
using namespace std;

accessing Strings int main () {


string str1 = "Hello";
string str2 = "World";
string str3;
int len ;
// copy str1 into str3
str3 = str1;
cout << "str3 : " << str3 << endl;

// concatenates str1 and str2


str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;

// total length of str3 after concatenation


len = str3.size();
cout << "str3.size() : " << len << endl;
return 0;
}
19
End of Chapter
Five

You might also like