C++
ARRAY-1
Lecture-11
Raghav Garg
Today’s checklist
1) Introduction to Arrays
2) Syntax , accessing elements of Arrays
3) Printing Output and Taking Input
4) Types of Arrays
5) Size operator
6) Memory allocation in array,address of array elements
7) Linear search
8) Basic problems
What is an array? -> liste collection similar
of data types
Data Structure a
storage to store data
class 1-A 3100 bacche
int X;
↓
charch; percentage
99.8
float
21.=
90.1
floatx2 =
Syntax and Declaration
~size
intx [7];X 10 $58
⑧ I 2 3 Y S 6
x (3]=
5;
-> 320 are indices
x (0) 10;
=
x (3]
=
8;
int arr[3]= [1,2,33;
How to access Elements in Array ?
attssz
arr[4]
Printing Output and Taking Input
For look -
output &
using
input indices
i
Ques: Given an array of marks of students, if the
markSof any student is less than 35 print its roll
number. [roll number here refers to the index of the
array.]
marks, 83 3497
O 12345
Output/Input
I
-
v
·Enter no. of students:4
-
~
Makso/zs
Eren marks:31
the
V
36 9023
- 3
I ·03
3
W
Ques : Are the following array declarations correct?
int a (25) ; wrong
int a [25];
int size = 10, b[size] ; correct int size 10;
=
int b(size];
int c = {0,1,2} ; wrong
intcl] 0,
=
1,23;
Ques : Which element of the array does this
expression reference?
num[4] -> yth index-St element
Types of Arrays
1) One dimensional Array
2) Two dimensional Array -> matrix
Size and sizeof operator(How can we use it to
find the Length of array?)
intavr[] 22, 3, 4, 1,
=
2, 9, 10, 11, 100, 17, 19,18,163;
int n size of
=
(arr)/sizeof (arr(0]);
A
Memory Allocation in Arrays
Continuous allocation.
memory
[ 1,2,3,53;
-
int (4)
=
arr
arr 19 -
01 2 3
ayosawisis anties aroses
contdarr;
↓
cout<< arr;
I -> address -
first element - fair (0]
Memory Allocation in Arrays
01 2 3 4 56789 a b c d e f
dY
d8
dC
20
e4
Address of Array Elements
d
continuous
Predict the output : output
I
I
200
main( ) him
Alt.. 12 25
do
{ 200 100
·
temp
- int num[26], temp ;
~ num[0] = 100 ;
vnum[25] = 200 ;
vtemp = num[25] ;
-num[25] = num[0] ;
e
num[0] = temp ;
Wcout<<endl<<num[0]<<” ”<<num[25] ;
}
Point out the errors(if any) in the following code:
outputinput
I
int main( ) {
~ int size ;
a
. y
vcin>>size ;
zint arr[size] ;
W for ( i = 1 ; i <= size ; i++ ) {
cin>>arr[i] ;
cout<<arr[i] ;
}
return 0;
}
undeclared identifier
Ques : Calculate the sum of all the elements in the
given array.
I 432 S 6
⑧ I 23 4 S
int Sum;
Linear search
Ques : Find the element x in the array . Take array and
x as input.
81113, x 8
int an
=
ni
cincn;
int avo[h];
if (i) x) element
present";
"
is
(avr =
=
cont<<
11
input
int x;
in 2;
Ques : Find the maximum value out of all the elements
in the array.
output
9 / 2 102 4 5 6
are 100
⑧ I 2 34 567
int max: arr[O);// INT_MIN
max:
for (intc=1; i < n
=
-1;i +
+
72
I if (max <arr(i)) max=
awr[i];
3
cont<<max;
max 9100 102
=
Ques : Find the second largest element in the given
N
Array.
arr
(5118191416
0123456
Step-1:Find largest, 18
max=
Step-2:traverse through the if Ismaxcarrlil && arr(i)!= max)
-
away,
int max= INT-MIN; if (max <arr(i)) max= arr[i]
int smax= INT MIN;
Ques : Find the second largest element in the given
Array. max: I MIN
F A & 18
I
BY$67012
=
i 0X &
18
9
smax: IFMIN A &$9
animals
s
MCQ : What is the difference between the 5’s in these
two expressions?
-> size of num
int num[5] ;
num[5] = 11 ;
II daal do
->
5th index pe
1. first is particular element, second is type
2. first is array size, second is particular element
~
3. first is particular element, second is array size
4. both specify array size
MCQ : What would happen if you assign a value to an
element of an array whose subscript exceeds the size
of the array? int arr [S];
arr (8] 2;
=
evrow
1. the element will be set to 0
2. nothing, it’s done all the time
3. other data may be overwritten
4.
v error message from the compiler
State TRUE or FALSE :
1. The array int num[26] has twenty-six elements. T
2. The expression num[1] designates the first element in
the array F
3. It is necessary to initialize the array at the time of
declaration. F
4. The expression num[27] designates the
twenty-eighth element in the array. T
int air (26];
s
im
Ques : Count the number of elements in given array
greater than a given number x. arr, X
int count 0;
=
for lint i 0;
=
i < n
=
-1;i +
+
)[
& if(arr(i)> x) count++;
cont<<rount;
THANK YOU