Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Arrays

What is an
Array
An array is a data
structure used in
programming to store a
collection of elements,
where each element can
be accessed using an
index or a key. Arrays are
commonly used to
organize and manipulate
large amounts of data in a
structured manner.
Characteristics of Arrays
1. Ordered Collection:
Elements in an array are stored in a specific order, usually starting from index 0 and
increasing sequentially.
2.Fixed Size:
The size of an array is determined when it is created, and it typically cannot be
changed during runtime. Some languages provide dynamic arrays or resizable arrays
that can be extended or shrunk, but the basic idea remains the same.
3.Homogeneous Elements:
Elements within an array are of the same data type. For example, you might have an
array of integers, strings, or other specific data types.
4.Indexed Access:
Elements in an array can be accessed using their index. The index indicates the
position of the element within the array. Indexing often starts from 0 (zero-based
indexing), but some languages might use 1 (one-based indexing) or other
conventions.
Declaring arrays
You typically declare an array the same way you declare any variable but
with[ ]:
variable_type variable name[array_size] = { array, elements } ;

variable_type = define the type of variable you want to use


variable_name = what do you want to name your variable
array_size = define how big you want your array to be
array elements = place your array elements

int numbers[5] = { 3 , 6 , 8 , 4 , 9 } ;
OR
variable_type variable name[ ] = { array, elements } ;

variable_type = define the type of variable you want to use


variable_name = what do you want to name your variable
array elements = place your array elements

int numbers[ ] = { 3 , 6 , 8 , 4 , 9 } ;

!!!NOTE that here we did not specify the size just the elements
OR
variable_type variable name[array_size];

variable_type = define the type of variable you want to use


variable_name = what do you want to name your variable
array_size = define how big you want your array to be

int numbers[7];

!!!NOTE that here we did specify the size, but not the elements. We can give the
elements later in the code.
Array positioning

int numbers[5] = { 3 , 6 , 8 , 4 , 9 };

numbers
0 1 2 3 4 Position

3 6 8 4 9 Element

This means that: numbers array @ position 0 has 3 stored


numbers array @ position 2 has 8 stored
numbers array @ position 3 has 4 stored
the size of the array is 5 positions
Accessing Array elements (Read)
int numbers[5] = { 3 , 6 , 8 , 4 , 9 };
Int grade = numbers[0]; //grade is equal to 3
Int count = numbers[3]; //count is equal to 4
Int beers = numbers[2]; //beers is equal to 8

Int goals = numbers[4]; //what is the value of goals?

?
Accessing Array elements (Write)
int numbers[5] = { 3 , 6 , 8 , 4 , 9 };
numbers[0] = 5; //change position 0 to contain 5
numbers[4] = 7; //change position 4 to contain 7
numbers[3] = 8; //change position 3 to contain 8
numbers[1] = 6; //change position 1 to contain 6
numbers[3] = 5; //position 3 will now contain a 5

0 1 2 3 4

5 6 8 5 7
!!!NOTICE THAT POSITION 2 REMAINS UNCHAINGED!!!
Strings
A string is a sequence of characters, typically used to represent textual
data in programming. Characters can include letters, digits, punctuation
marks, whitespace, and special symbols. Strings are one of the most
common and fundamental data types in programming languages, as
they are essential for dealing with text-based information and
communication.
Strings are a data type, but can also be characterized as an array
(character array).
To declare a string:
String variable_name = “value”;
e.g.
String _lion = “Simba”;
String positioning

Just like arrays strings can be used and accessed the same way we
would an array.
String _lion = “Simba”;
_lion
0 1 2 3 4

S i m b a
String Manipulation
Indexing and Slicing:
You can access individual characters within a string by using their
index (position) in the string. You can also extract substrings using
slicing.

Concatenation:
Strings can be concatenated, or joined together, to create new strings.

Length:
You can find the length of a string, which represents the number of
characters it contains.

String Manipulation:
Programming languages provide various built-in functions and
methods for manipulating strings, such as changing case, finding
substrings, replacing portions of the string, and more.
String Manipulation
(indexing)
With indexing, we can access the individual elements of the string
e.g.:

String _lion = “Simba”;


char middle = _lion[2]; // the variable middle contains ‘m’
char last = _lion[4]; // the variable last contains ‘a’
char start = _lion[0]; // the variable start contains ‘S’

!!!NOTE that the character ‘S’ and ‘s’ are not the same
characters!!!
String Manipulation
(indexing)
With indexing, we can access the individual elements of the string
e.g.:

String _lion = “Simba”;


_lion[0] = ‘s’;
_lion[1] = ‘a’;

_lion now contains : “samba”

!!!NOTE that the character ‘S’ and ‘s’ are not the same characters!!!
String Manipulation (indexing)

With indexing, we can access the individual elements of the string e.g.:

String _lion = “Simba”;


_lion[0] = ‘s’;
_lion[1] = ‘a’;

_lion now contains : “samba”

!!!NOTE that the character ‘S’ and ‘s’ are not the same characters!!!
String Manipulation
(length)
We can use the length feature to get the length of the
string e.g.:

String _lion = “Simba”;


String _cat = “ cheetah ”;
int num = _lion.Length();
Int num2 = _cat.Length();

The variable num contains 5 // the length of the string


The variable num2 contains 9 // the length of the string
including spaces
End
Ascii table (for reference)

You might also like