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

TOPIC 5:

ARRAY
INTRODUCTION

 Array is a collection of a fixed number of components, all of same


type, that is given a single name.

int score1 = 10;


int score2 = 20;
int score3 = 5; int score[] = {10,20,5,80,10};
int score4 = 80;
int score5 = 10;
ONE-DIMENSIONAL ARRAY

 Is used to store multiple values in a single identifier.


 General array declaration syntax:

data_type array_name [size_of_array];

 For example:
➢ int max[5]; // to store 5 integer values in max
➢ float temp[4]; // to store 4 float value in temp
➢ char y[20]; // to store a string of values
ONE-DIMENSIONAL ARRAY (CONT.)
 The definition:
➢ int max[5];

allocates the following memory:

max

first second third fourth fifth


element element element element element
ONE-DIMENSIONAL ARRAY (CONT.)

 Each item in an array is called an element or component


 Every item has its position in an array , this position is called index or
subscript; always start at 0
 Example:
➢ int max[5]= {10,3,2,40,1};

index

max[0]; refer to the first data


[0] [1] [2] [3] [4] max[1]; refer to the second data
max 10 3 2 40 1 max[2]; refer to the third data

array element
name
DECLARING AN ARRAY
 Declaration:
➢ int num [10];

 Initialization:
➢ int num [10] = {0};
➢ int temp [5] = {1,2,3,4,5};
➢ int temp [ ] = {1,2,3,4,5};
➢ char codes [6] = {‘s’ , ‘a’, ‘m’ , ‘p’ , ‘l’ , ‘e’};
➢ char codes [7] = “sample”;
❑ Note: a string is terminated with a null character “\0”

[0] [1] [2] [3] [4] [5] [6]


codes s a m p l e \0
DECLARING AN ARRAY (CONT.)

 Example illegal declarations:


➢ int arr [10] = { }; //must specify at least 1 element. It cannot be completely empty;
➢ int arr [3] = {1,2,3,4,5,6}; //illegal to add more elements than the size of the array
➢ int arr [ ]; //must specify the array size

 Question:
 What if number of elements are less than the length specified?
➢ int num[10] = {45, 20, 4, 77, 1};

 Answer:
 The remaining locations of the array are filled with value 0
➢ int num[10] = {45, 20, 4, 77, 1, 0, 0, 0, 0, 0};
ASSESSING ARRAY COMPONENTS

 int list[5];

[0] [1] [2] [3] [4]


list

Array list;

 list[3]=24;

[0] [1] [2] [3] [4]


list 24

Array list after execution of the statement list[3] = 24;


ASSESSING ARRAY COMPONENTS (CONT.)

 list[1]=5;
 list[4]=10;
 sum = list[1] + list[4];

[0] [1] [2] [3] [4]


list 5 24 10

sum = list[1] + list[4]


sum = 5 + 10
sum = 15
PROCESSING ONE-DIMENSIONAL ARRAY

 Some basic operations performed on a one-dimensional array:


➢ Initialize
➢ Input data
➢ Output data stored in an array
➢ Find the largest and/or smallest element
PROCESSING ONE-DIMENSIONAL ARRAY
(CONT.)

 Given the declaration:


int list[100];
int i;

 Use a for loop to read data into array:


for (i=0; i<100; i++)
cin >> list[i];

 Use a for loop to print element from array:


for (i=0; i<100; i++)
cout << list[i];
EXAMPLE
ARRAYS AS PARAMETERS TO FUNCTIONS

 Arrays are passed by reference only


 The symbol & is not used when declaring an array as a formal
parameter
 The size of the array is usually omitted
➢ If the size of one-dimensional array is specified when it is
declared as a formal parameter, it is ignored by the compiler
EXAMPLE
ARRAYS AS PARAMETERS TO FUNCTIONS
(CONT.)

 The reserve word const in the declaration of the formal parameter can
prevent the function from changing the actual parameter
EXAMPLE

You might also like