Documente Academic
Documente Profesional
Documente Cultură
Arrays
(Arrays and Functions)
Created By:
Amanpreet Kaur
SME (CSE) LPU
©LPU CSE202 C++ Programming
Outline
•
To declare an array
•
To initialize an array
•
To pass an array to a function
c[0] -45
– Group of consecutive memory c[1] 6
locations c[2]
c[3] 72
0
c[4] 3
– Same name and data type c[5] -89
c[6] 0
•
To refer to an element, specify: c[7]
c[8]
62
-3
c[9] 1
– Array name c[10] 6453
c[11] 78
– Position number in square
Position number of
brackets([]) the element within
©LPU CSE202 C++ Programming array c
Arrays
•
An array is an ordered list of values
The entire array Each value has a numeric index
has a single name
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9]
c 79 87 94 82 67 98 87 81 74 91
scanf(“%d”, &array[i]);
©LPU CSE202 C++ Programming
Program of
Initializing an
array to zero
using loop.
0 2
1 4
n[0] 2
2 6
n[1] 4
3 8 n[2] 6
n[3] 8
4 10 n[4] 10
n[5] 12
5 12 n[6] 14
n[7] 16
6 14 n[8] 18
n[9] 20
7 16
8 18
9 20
©LPU CSE202 C++ Programming
Program to
compute
sum of
elements of
array
array = 0012FF78
&array[0] = 0012FF78
&array = 0012FF78