You are on page 1of 21

ARRAYS

 In C++ programming, an array is a collection


of data items of the same type.

 When created in computer memory, array


occupies contiguous/consecutive memory
slots.
Memory slots
in RAM
 Theposition number is more formally called
a subscript or index
 This number specifies the position of an element
from the beginning of the array.
 A subscript must be an integer or integer
expression - one that evaluates to an integer.
 The first element in every array has subscript 0
(zero).
 The highest subscript in an array is less than the
number of elements in the array by 1.
 For instance: If an array has 100 elements, the first
element is available at index 0 while the last element
is at index 99.
 An array must have a name and type of
data to be stored.

 Its size must also be known

 Declaration syntax

dataType arrayName [arraySize];

 Example
 char grades[5];
 Sometimes, the programmer can declare an array and
assign values to it at the same time.
 The values supplied during initialization forms the
intializer list of the array declaration
 Declaration with initialization syntax

dataType arrayName [] = initializer_list;

 Example
 char grades[] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’};

Reminders: 1. If initializer is given, you may leave out the array size.
2. If the array size is specified, the number of
elements in the initializer list should NOT exceed
the specified array size.
 C++ supports two versions of array dimensions
 Single dimension – which is declared with a single []
bracket.
 Example:
 int values[11]; //this declares an array equivalent to a
single row matrix with eleven columns

One row

Eleven columns
 Multi-dimension – which is declared with multiple
[] brackets.
 Example: Two dimension
 int twoDimPlane[4][4]; //this declares an array
equivalent to a 4 by 4 matrix
 Example: Three dimension
 int threeDimPlane[5][5][3]; // this declares an array
equivalent to a cube

4 rows 4x4 matrix

4 columns
 You can assign a value to a specific position on the
array using the following syntax

arrayName[index] = value;

 Example
grades[0] = ‘A’;
grades[1] = ‘B’;
grades[2] = ‘C’;
grades[3] = ‘D’;
grades[4] = ‘E’;

Reminders: 1. The index must be valid 0 ≤ index < array size


2. The value must be of the array type
 You can extract a value from the array by
specifying its position.
 Example
// assigns to variable secondGrade the value
//stored at the second slot in the array grades
char secondGrade = grades[1];

// assigns to variable lastGrade the value


//stored at the fourth slot in the array grades
char lastGrade = grades[4];//last element

Reminder: 1. The index must be valid 0 ≤ index < array size


 Loops are a handy tools for working with arrays
 Successively assigning values to the array
 Processing/reading from the array
 Example: Initializing array with position number
int arraySize = 20;
int values[arraySize ];//array of 20 integers
for(int i = 0; i< arraySize ; i++)
values[i] = i;//assign I to index I

 The array will finally contain 0 to 19

Reminder: 1. The loop variable should not be bigger than the


array size-1 0 ≤ loop variable < array size
 Example: Printing values from an array
for(int i = 0; i< arraySize ; i++)
cout << values[i] << endl;

 Will print 0 to 19 vertically

Reminder: 1. The loop variable should not be bigger than the


array size-1 0 ≤ loop variable < array size
 Defining a function that receives an array
 The array parameter in the function header must
show the data type of the expected array with
empty [] brackets.
 Example
void printGrades( char grades[] )
{
//your code to print the grades
}

Reminders: 1. The parameter name has [] brackets without size


 It is very important to decide how
the function
will know the size of the array.
 Pass the size as a second argument
 Example
void printGrades( char grades[] ,int arraySize )
{
//your code to print the grades
}
CODE

Reminders: 1. The parameter name has [] brackets without size


2. The function must receive the array size
 Calling a function that receives an array
 Only the name of the array is passed in the function
call. DO NOT INCLUDE THE [] brackets.
 Pass the size of the array if your function receives a
size
 Example
char grades[5];
//someother code
printGrades(grades, 5);
//other code
CODE

Reminder: 1. Only the array name is passed without [] brackets


2. Send also the size or the function knows how to
calculate the array size
 Consider the following array declarations
1. char grades[5];

int numberOfGrades = 5;
2.
char grades[numberOfGrades ];

3.
const int NUMBER_OF_GRADES = 5;
char grades[NUMBER_OF_GRADES];
 The first case uses a direct literal to specify the
array length :: NOT ADVISED
 The second one offers an improvement but yet
is not very good one.
 These two alternatives may require you to edit
several places if you want to change the size
 The third one is the best. Declared as global
variable, the constant defines the array size for
all of your program (within main and other
functions):: MOST ADVISED
 Change once and the effect occurs everywhere.
CODE
U can use the sizeof operator to work out
the length of the array
 REMEMBER
 Array is of data type T where data type T
needs y bytes for storage
 If we want to know how many elements are

there in array X, then we find the total size


of the array X and then divide by the size of
a single element of type T.
 HENCE
 arrayLength = sizeof(X)/sizeof(T)

CODE
void printGrades( char grades[], int arraySize )
{
for(int i = 0; i < arraySize; i++)
{
cout << grades[i] << endl;
}
}
#include <iostream>
void printGrades( char grades[], int arraySize )
{
for(int i = 0; i < arraySize; i++)
{
cout << grades[i] << endl;
}
}

int main()
{
char grades[] = {‘A’,’B’,’C’,’D’,’F’};
printGrades(grades,5);
cin.get();
}

You might also like