Professional Documents
Culture Documents
Topic 5 - Array (1) 230726 105933
Topic 5 - Array (1) 230726 105933
ARRAY
INTRODUCTION
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];
max
index
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”
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];
Array list;
list[3]=24;
list[1]=5;
list[4]=10;
sum = list[1] + list[4];
The reserve word const in the declaration of the formal parameter can
prevent the function from changing the actual parameter
EXAMPLE