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

C++

ARRAY-2
Lecture-12

Raghav Garg
Today’s checklist

1) Passing array to functions


2) Dynamic allocation will be covered in
- >
a
separate video

3) Vectors in C++
4) Operations on Vector
5) Problem on arrays and Two pointers
ereference
Passing Array to Functions

I
142746

24
I

an

as h
o
MCQ : When you pass an array as an argument to a
function, what actually gets passed?

1. address of the array


-
2. values of the elements of the array
3. address of the first element of the array
v
4. number of elements of the array
Arrays and Pointers
char arr (3): ['a'2', '$';

arr[] [1,5,2,3,43; int 4;


=

int = x

- int* ptr=&m v
int* ptr= arr;2
;

int4ptr x; X
=

int* ptr darr;


=
X

int* ptr &


=
arr[0]; v

int* ptr arr[0];


=

X
Arrays and Pointers
an
x48xs
ee
8
# 52
x x56
Y
60
W
x
0

pty
output
-
700
x

426 17
Vector in C++ ->
Dynamic Array

vectors int> arr;
array
hi
replacement
I

problem -
fixed size arrt51s
int arr[S] =
31,2,3,4,53;
y5/6/1)
11 I
(192,3,4,5,63;
Basic Operations on Vectors
·

Syntax
-
capacity,
w
size, at, sort
·
push-back, pop-back,

vector - int> v(S); to


Basic Operations on Vectors
capacity
L

-

Basic Operations on Vectors
Output

I
9 10 x

I
6.1

6 a

10 10Garbage
assing tors to Endions:Diff. to
Arrays

vectors are passed by value. Each time


you pass, new vector

is created.

& ->
amper cant
Looping in vector index
A

Ques : Find the last occurrence of x in the array.


int x i vector into vi

V 13 2 Y 34 16

* I 2 34S 67

X 1
=
intidx =
-11; 16
Ques : Find the doublet in the Array whose sum is
equal to the given value x. (LeetCode 1) (Two Sum)
-

V 13 2 Y 34 16

I 2 34 56 I

x I
=

10,7)

targets,
[3,4)
(4,5)
(6,7)
I
~
~
v Ea
X
Enter target:7
-
8
-
~ W 8
Enter
away size:
v.sizel) Enter
away elements:
=

- 9
- I 3243416
-

I
·

(0,7)
·

(1,3)
·
(1,5)
0

vitili
i
j
Ques : Write a program to copy the contents of one
array into another in the reverse order.
V 13 2 4 3 Y I 6
it j size-1
=

" I 2 3456 I

v2 6 I Y 3 4 2 3 1

* I 2 3 4S 6 I

Vector (int> v2(v.size());


Two Pointers
Ques : Write a program to reverse the array without
using any extra array.
2 34 S

F
01 6

V 6 I Y 3 423

j i

int i 0; =

intj v.sizel)-1;
=

is
j
=
Re barraway
⑧ I 2 3 4 S

arr 16 2 37 Y

CIm
wer(4): 1
= 326

73264
⑧ I 2345

j i
Ques : Rotate the given array ‘a’ by k steps, where k is
non-negative.
Note : k can be greater than n as well where n is the
size of array ‘a’.

awr
16 2 3 7
Y

2
C 741623
S

I -1,
& -
Y I

- &
I &
I
3
2
Agithm:Hintereverse partof away
visizel) -
1
-
⑧ I 2 3 Y S 6

I 6 2 3 7 Y 8 k2
are
=

- -
↓ k kd
n -

26 I 8 Y
-
t

816 23 7
-> Y

Set:reverse part (0,n-k-1,V);intn vsizel);


=

reverse (n-K, n-1,


Part v);

reverse Part(0,n-1, V);


⑧ 1 2 3 Y S 6 n 7
=

6237 8 k9
=

I y

48 I 6 23 7

if (k n) same
if (k >
n)
=
-
away is

R K% n
=

Iwork:Leetlode Array
THANK YOU

You might also like