Uploaded by Rudra Joshi

C++ in 10 Hours.pdf

advertisement
11
2)
Laptop/PC
Dedication
C++ Programming
in One Shot
Chapter 1 : Basics
1)
2)
3)
4)
5)
6)
7)
8)
9)
Basic Printing, \n and endl wali cheez.
Variables, printing variables, int, float, and +,-,*,/ of integers.
Variable naming rules.
Comments
Taking Input
Modulus Operator
Float to int, int to float
Hierarchy
Char and ASCII
Basic program in C++
#include<iostream>
using namespace std;
int main(){
cout<<“hello world”;
}
Output
Hello World
How to move in next line?
Example :
cout<<”Hello PW”;
cout<<”Hello CW”;
Output will be :
Hello PWHello CW
'DRY RUN'
Out
Hello PW
·
a
⑧
-
~
·
Hello CW
Use of escape sequence endl and ‘\n’
Example :
cout<<”Hello PW”;
cout<<endl;
cout<<”Hello CW”;
Output will be :
Hello PW
Hello CW
*
Athing:
Y;
1)
Cont
2)
Cont<<
"Y";
3)
cont
4+ 3;
1)
Cont
<<
<<
< <
"4+3";
Variables and their Declaration
Boxer,
Let us focus on int data type as of now.
1) Variables as containers :
-
-
s
X
Dabbe
I
Output
S
Printing Variables in C & Updation of Variables
-int x = 5;
~ cout<<x<<endl;
x
= 7;
rcout<<x<<endl;
x = x + 6;
-
- cout<<x<<endl;
I
W
/
7
S
/
X
x x
=
-
20
x 13
=
vx = x - 20;
~cout<<x<<endl;
=
-
-
20
7
I
Output
S
·
·
·
0
I
13
7
=
-
-
-
↳
X
tim
Output
-
·S
08
Arithmetic operations on int data type
W
int x = 5;
~int y = 2;
Wk
Wcout<<x+y<<endl;
~cout<<x-y<<endl;
~
cout<<x*y<<endl;
~cout<<x/y<<endl; </ issue
intlnt
x
y
I
7
·
-
·
3
10
·
·
2
Increment - Decrement operators
~int x = 5;
x<+;
cout<<x<<endl;
-x<-;
cout<<x<<endl;
w
~<+x;
cout<<x<<endl;
-
~–-x;
~ cout<<x<<endl;
#45
⑤S
X
-
Thodi
der
I
me
Output
-
6
S
6
S
Post
Increment
put
v
~
v
-
·
a
Y
·U
S
·
X
Due increment
-
~
~
~
~
↳
X
·4
·
&
S
S
float data type
float x = 3.1;
Storing
-
Real
Numbers
Arithmetic operations on float data type
float x = 5;
float y = 2;
cout<<x+y<<endl;
cout<<x-y<<endl;
cout<<x*y<<endl;
cout<<x/y<<endl;
Example : Calculating Area of a Circle
-
A=πr2
↓
area
d
radius
3. 1415...
Example : Calculating Simple Interest
SI
=
PRT
-
100
float p;
floatwi
float ti
float
si;
00
I
P
SA8. Y
540
=
2 32
=
t3
=
sup
32x3
+
-
100
=
-
S184
To
318.4
Homework : Calculate Volume of a Sphere
Variable Naming rules
1) Variables can start from an alphabet or underscore _ or $ .
~2)
~
3)
~
4)
Special characters except _ and $ are not allowed.
Some particular keywords are not allowed.
Commas or blanks are not allowed.
a
Auto
E
case
else
goto
double
sizeof
signed
struct
int
for
do
if
break
const
short
union
extern
static
switch
return
enum
long
char
void
unsigned
continue
volatile
register
while
float
default
typedef
Variable Naming rules - Examples
Q. Which of the following are invalid variable names and why?
BASICSALARY
_basic
basic-hra
#MEAN
group.
422
population in 2006
over time
mindovermatter
FLOAT
hELLO
queue.
team’svictory
Plot#3
2015_DDay
Taking input // Square of a Number
Take 2 numbers input from user and print their Sum
Modulus Operator
d
+,
->
x
-y
S
=
3
=
wea
aYob
is
remainder
divided
is
-
by
,,1,%
x
x
y
+
-
y
xxy
x/y
x%y
8
=
2
=
IS
=
1
=
2
=
b
8
-
3
-
5/3
IS
②5403
Modulus Operator
->
aYob
2)
Goa
3)
4)
=
a
=
[it a<b>
0
=
aY)-b) aYob
=
( a)%b
-
divisibility
2%8 2
Some impant pants:
i)
is
importance
=
(a%b]
Typecasting
float tint,
->
inte
float
Ques : Take integer ‘x’ as input and print
half of the number.
intx;
in
x;
Hierarchy of operators
a
very
important
(BOD MAS)
↓
int i = 2 * 3 / 4 ;
BY OP
cout<<i;
i
=
mate,an
0.75
*
2
=
-D
6/4
-
I
#(int)
i
2
=
3/4
*
6/4
=
=In
i 2
=
=
3/4
*
2
0
*
0X
=
D, M/ A,S
L
to
Right
Left
char data type
char ch = ‘a’;
4
a, b, c...
A, B, C...
2
2
!D,H, $,
·
1, 2,
%, v, 2, 1,
?, I,,.,
0,112,
3...
,
9
w,
2
4, 3, "is -st,
=,
2,3, 11,
2
ASCII values
char ch = ‘a’;
297
b
+
important
-very
65
+ 66
B
98
C3
199
67
100
2 +
122
49
1
/
S
/
i
!
11's
S
/
:
⑩8
I
I
de
[Typecasting]
'9'e57
=
90
MCQ Time !
Homework
In b = 6.6 / a + 2 * n ; which operation will be performed first?
(1) 6.6 / a
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
MCQ
Which of the following statements is false
(1) Each new C++ instruction has to be written on a separate line FALSE
(2) Usually all C++ statements are entered in small case letters
TRUE
(3) Blank spaces may be inserted between two words in a C++
statement TRVE
(4) Blank spaces cannot be inserted within a variable name TRUE
C.
W.
Homework float
-
f
d
i
The expression, a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ; evaluates to
:
(1) 8.28
(2) 6.28
(3) 3.14
(4) 0
~
7/22*5.1443/5
a
-
i
=
f
i
-
=>)
=)
-
A
*
O
=
a
a
a
C
5.14
*
i
043/S
-
Is
=
0
=
3/5
Ketant:
float
x
st
=
d
int
W
no
2
=
3.0/2
3.0/2.0
no
accept
2.S
=
5/2.0
real
5/2
2.5
=
2.S
=
1, 4,%7+,
MCQ
-
The expression x = 4 + 2 % - 8 evaluates to
x 4 2
(1) -6
->
=
+
~
(2) 6
6
=
2%
(3) 4
(4) None of the above
a%(b)
a%ob
ajob
=
a
=
[acb)
-
8
2%8
=
2
=
**MCQ
->
Homework
What will be the value of d if d is a float after the operation
d = 2 / 7?
(1) 0
(2) 0.2857
(3) Cannot be determined
(4) None of the above
Chapter 2 : Conditionals
1)
2)
3)
4)
5)
if, if - else
nested
Else if ladder
Ternary
switch
ans/output
situation
dependent
↑
IF - ELSE
I
5522 ad
Ques : Take positive integer input and tell if it
is even or odd
even
int
d
ni
Is
cinn;
1)
Cont "Even";
2) Cont
->
"Odd";
a&b
d
a
=
=
b,a!
b
=
it
C
rm
0
=
it
FO
0
=
even
odd
w
I
I
W
3
n
I
Output
Enter
3
n:
Odd number
HW : Take positive integer input and tell if it is
divisible by 5 or not.
Ques : Take integer input and print the
absolute value of that integer
x,
+,
-,
1, % Ari
OP
int ni
7
cin;
10
if (n >0)
-
cout<n
else ecout-h;
-
21
->
-
-
7
10
21
==,! =,
,,
plate
*Ques : If cost price and selling price of an item is
input through the keyboard, write a program to
determine whether the seller has made profit or
incurred loss or no profit no loss. Also determine
how much profit he made or loss he incurred.
int
up;
profit
it(sp> <p)
in cp;
It (cp sp)
int sp;
if (<p sp)
cinsp;
<
=
=
loss
sp-cp
s
-
cp-sP
to
no
loss
-
HW : Given the length and breadth of a rectangle,
write a program to find whether the area of the
rectangle is greater than its perimeter.
e,b
d
A l* b;
=
4
24(l b);
=
+
Multiple Conditions
Using && and ||
and
d
02
Ques : Take positive integer input and tell if it
is a three digit number or not.
if
(n > 99
1
true
and
n<1000)
Ene
cont<<
-
Ques : Take positive integer input and tell if it
is divisible by 5 and 3.
if (n%S
0
=
=
&&
n%3
=
0)
=
Ques : Take positive integer input and tell if it
is divisible by 5 or 3.
-
-
&
15, 18, 21, 24, 27,30...
12,
9,
6,
3,
5,
39...
25, 30,
10, 15, 20,
10, 12, 15,
3, 5, 6, 9,
if (4%5
n
=
0
=
22
=
11
18, 20, 21,
24, 25, 27,
1903 ==
0)
-
31,
Ques : Take 3 numbers input and tell if they
20, and
can be the sides of a triangle.
a, b,
c
if (19+ b)
1
a
b
a
b
+
b c
+
c
a
+
>
=
-
c
a
b
x
and 12+ ap b
and (c+ a) > b)
Ques : Take 3 positive integers input and print
the greatest of them.
a, b, c
it (as
else
else
b
and
if (b>
a
cont C
a
sc)
&&
cont<<
b, c)
a..
cont<< b
Homework : Take 3 positive integers input and
print the least of them.
HW : Take positive integer input and tell if it is
divisible by 5 or 3 but not divisible by 15.
if ((n%s
=
0
=
or
4%3
=
0)
=
and
(n%/s!=0))
Nested If - Else
12
if (
I
Esses
I
3
it
-
else-
Ques : Take 3 positive integers input and print
the greatest of them. withoutusing multiple conditions
HW : If the ages of Ram, Shyam and Ajay are input
through the keyboard, write a program to
determine the youngest of the three.
Else If
Ques : Take input percentage of a student and
print the Grade according to marks:
1)
2)
3)
4)
81-100 Very Good
61-80 Good
41-60 Average
<=40 Fail
HW : Given a point (x, y), write a program to find
out if it lies in the 1st Quadrant, 2nd Quadrant, 3rd
Quadrant, 4th Quadrant, on the x-axis, y-axis or at
the origin, viz. (0, 0).
I
I
⑧
⑧
④
⑳
I
-
⑧
⑧
-
IV
-
⑧
if-die
d
but
cool banneke
lines
liye
bachana
Ternary Operator
expression 1 ? expression 2 : expression 3 ⑨
another
replacementfor it-else
Switch Statement
↓
useless
Syax:
character
int
I
switch ( integer expression ) {
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
Radar's
Recommendation
-
·
exam
se
I
day before
↑
n1, n2,
int
op
Char
Ques : Write a program to create a calculator that
performs basic arithmetic operations (add,
subtract, multiply and divide) using switch case
=
and functions. The calculator should input two
numbers and an operator from user.
int
a;
int
bi
charch;et,-, 1,
->
achb
n)
=>
op
n2
A
MCQ Time !
Predict the output
main( ) {
~int
~if
x = 10, y = 20 ;
( x == y ) ;
cout<<x<<” “<<y ;
}
↳
I
Output
10
20
HW : Predict the output
int main( ) {
int x = 3, y = 5 ;
if ( x == 3 )
=
>
printf ( "\n%d",
x )I;
(out
xendl;
else ;
printf ( "\n%d",
y ) ; couty
=
>
}
endl;
*Predict the output
main( ) {
~int x = 3, y, z ;
~
- W
10
y = x = 10 ;
X
(Right
v z = x < 10 ;
↳
false
Left)
to
-
0
cout<<x<<” ”<<y<<” “<<z ;
}
true->/
y
O
z
I
out
10
10
O
Chapter 3 : LOOPS
1)
2)
3)
4)
5)
->
repetition
For Loop
Break and Continue
While and Do While Loop
Questions using Operators
Pattern Printing Problems
What and
Why?
baar hello world
For Loop
condition
int
↑
int i 1;
1
}
Iterator
-
3;
=
=
for(int i = 1; i<10; i++){
// code
x
increment
decrement
↓
declaration initilization
+
Ques : Print hello world ‘n’ times. Take ‘n’ as
Done!
input from user
How for loop works : the various parameters.
n3
Initialization
=
Steps
"
-
-
1)
I
Output
Hello World
·
Y
M
3
W
2
I
/
i
Hello World
·
Hello
·
*
World
23
Check condition
Work
3) Increment
Ques : Print numbers from 1 to 100
using
for
look
Ques : Print all the even numbers from 1 to 100
↓
it (1%2
Method-2:
e
=
0)
=
even
HW : Print all the odd numbers from 1 to 100
Ques : Print the table of 19.
↓
171, 190
19, 38, 57, 76, 95, 114, 133, 152,
HW : Print the table of ‘n’. Here ‘n’ is a integer
which user will input.
Ques : Display this AP - 1,3,5,7,9.. upto ‘n’
terms.
Method 1
-
nth term
!
formula
-
->
(n
an a
an
=>
+
=
1
=
-
1)d
(n-1)2
+
2n-2
an 1+
=>
=
an=
=>
27-1
4, 7, 10, 13....
Ques : Display this AP - 1,3,5,7,9.. upto ‘n’
terms.
-
-
-
Method 2:Using extra
-
-
variable
&
keeping'only for
rounds
&
the iterations
--
~
HW : Display this AP - 4,7,10,13,16.. upto ‘n’
terms.
3
a
an
an
an
an
a
=
4
=
4,d
=
(n 1)d
+
-
(n
-
+
1).3
4 3n
-
+
=
3n+ 1
=
⑳34n
1
+
3
=
Ques : Display this GP - 1,2,4,8,16,32,.. upto ‘n’
urvo
terms.
x2
x2
2x:
->
5,
15, 4S,
for
lint
i = 1,ic
n;i +)[
=
cont< a"
a a
=
3
135,
2;
+
+
";
x2
x2
HW : Display this GP - 3,12,48,.. upto ‘n’ terms.
Break;
↓
thodi
der
ques
he
me
saath
Ques : WAP to find the highest factor of a
number ‘n’ (other than n itself)
24
1, 2,
-
3, 4, 6, 8, 12,24
↓
-
W
intf=
for
1
3
lint
1;
1If
i =1;
if (nii
me
i
0)
==
<
factor
m;i
f i;
=
(
+
+
Ques : WAP to check if a number is composite
or not.
it
n
has
extra
factor/factors except
composite
d
if (n%i
L
look
-
0)
==
-
we
found
a
factor
- tnk
mitt
1 & then
it is
Ques : WAP to check if a number is prime or
not.
dane
Continue;
d
if you
to
want
skipa
round
↓
print
no.
From
20
1 to
except308
Hiw.
Ques : WAP to print odd numbers from 1 to
100. using continue
-
While Loop
-
An
alternate
to
for
look
I
int i = 0;
while(i<10){
// code
i++;
}
Kitni baal
chalega,
weird
↓
multiple conditions
scenario
Do- While
Loop -eK
baar
kam
se
kam
do {
//code
} while ( another == 'y' ) ;
zun
kane
Predict the output
int main( ) {
~int x = 1 ;
W
while ( x == 1 )
x=x-1;
cout<<x<<endl;
}
⑭
I
Predict the output
main( ) {
int i ;
->
Infinite
while ( i = 10 ) {
cout<<i<<endl;
i=i+1;
}
}
look
Predict the output
main( ) { 1972983
while ( 'a' < 'b' )
cout<< “malayalam is a palindrome" <<endl ;
}
Infinite
look
HW : Predict the output
main( ) {
int i = 10 ;
while ( i = 20 )
printf ( "\nA computer buff!" ) ;
}
HW : Predict the output
main( ) {
int x = 4, y = 0;
while ( x >= 0 ) {
x-- ;
y++ ;
if ( x == y )
continue ;
else
cout<<x<<” “<<y<<endl;
}
}
Questions using
Operators
+-*/
%
Ques : WAP to count digits of a given number.
intn;
->
n
ein;
#Hint:1)Integer division
-
2)
Divide
in
& again
by
10
again
5812
=
I
int count
0;
=
While (n>0) E
I
3
n
n/10;
=
count+t;
Ques : WAP to count digits of a given number.
n
=
n
n
=
123 Y
count 0
123
count-1
12
=
count=
2
=
1
n
=
O
n
0
=
finish
count= 3
C
count= 4
~
ans
Ques : WAP to print sum of digits of a given
number.
3
Sum 12
n 1234
#
Fit:1)
2)
3)
n
Modulus
(%) operator
2%10
1 2 3
+
+
the last
gives
y
+
512843
=
+
=
=
digit
4 3 2 1
+
=
+
+
+
4
=U
Ques : WAP to print sum of digits of a given
1
3 y 4 3
ed n%10
number.
1
2
+
Algorithm:
e
n
n
1234
=
=
123
12
I
⑧
look
finished
=
+
+
2
+
+
ed Y
=
+
=
Sum=
7
3
2
9
1
10
4
HW : WAP to print product of digits of a given
number. (There won'tbe
zero in the number)
a
For ex:
n =
1234
12*3 *y 24
=
pro
=
HW : WAP to print sum of all the even digits of
a given number.
n
I
1234
=
2 +y 6
=
A
Ques : WAP to print reverse of a given
number.
n
#Hint:
1234
+
=
1)
sum
2)
4321
5
4321
=
digits
of
0,0,4,
=
40, 43, 430,
432,4320,4321
Ques : WAP to print reverse of a given
number.
v
E
10
2
=
Algorithm
-
n
~
153412/3 x
0 44043
=
ed YBR 1
=
1) Id
2)
2
3)2
4
10
=
+
ld
=
4) n116
=
0
=
430/4328
/432 -
r r
=
4321
ld
+
HW : WAP to print the sum of given number
and its reverse.
n
1234,2 4321
=
=
Ques : Print the factorial of a given number ‘n’.
-
↳
Find the
5!
sum
from
in
ent
SX4x4X2x1
=
8x 7
8!
=
x6x5x4x3x2x1
n! nxn-Dx(n-2)
=
....
3 x2
x1
02
1x
2x3x4....(n-1)xh
HW : Print the factorials of first ‘n’ numbers
d
n!
#Hint:
n
3,
=
I
11
=
2!
3
2x1 2
=
=
=3x2x1
4!
5!
6
=
4x3x2x1
=
Sx4x3x2x
=
24
=
120
=
HW : Write a program to print all the ASCII values
and their equivalent characters of 26 alphabets
using a while loop.
65
A
66
66B
.
65
67C
I
:
90
Z
90
Chapter:4
-
nee
Pattern Printing
Questions
****
↓
****
****
Nested Looks
d
Nested
it-else
L
ek
ke
andar
ek
Ques : Print the given pattern
n
-
I *****
*****
M
*****
↓
ka
mxy
star
1
m
2
=
n=
6
↓
* *
*
*
& A & H
Solid Rectangle
* X
Y
A
rectange
Ques : Print the given pattern
****
****
****
****
Solid Square
Ques : Print the given pattern
-n 4
=
1234
1234
1234
1234
Number Square
n
3
=
123
123
123
Homework:
-
-
nY
=
-ih
1
n
2
=
11
22
n
S
=
111
2222 2
33333
44n4y
SSSSS
Ques : Print the given pattern
n
4
=
ABCD
ABCD
ABCD
ABCD
Alphabet Square
n
3
=
A BC
BC
A
A
BC
HW:1
-
HW: 2
-
a
b
c
BBB
a
b
c
cc CC
a
b
A A A
A
B
DDD D
(n 3)
=
c
Ques : Print the given pattern
I
I
2
3
Y
I
23 Y
*
**
***
****
-j
I
Star Triangle
=
i
i
i
i
1,j 1
2,j
=
=
1,2
=
3,j 1,2,3
=
=
4,j 1,2,3,4
=
=
jc
E
1
l
=
Ques : Print the given pattern
-
-
H. W.
1
12
123
1234
Number
-
Number Triangle
Square
code
-
j
=
u
=
d
jc i
=
HW : Print the given pattern
A
AB
ABC
ABCD
Alphabet Triangle
HW:
-
I
22
333
yyy
Y
I
I-
row no.
jecol
no.
ne no.
of
rows
m no.
of
cols
=
1
AX
X
123
I
x X
X
123
222
123
***
BC
A
ABC
BC
A
↳
-
I
Seid
333
*HW : Print the given pattern
n
S
=
1
AB
123
ABCD
12345
Alphabet Triangle
n3
=
I
A B
I
2
3
#
Hint:
If
else
-
↓
I
I
2
3
Y
I
Ques : Print the given pattern
234sj
I
****
***
**
*
y
=
=
1,2,3
2,j
i 3ej 1,2
i 4,1j 1
=
i
=
=
*
E
jc 1
*
*
n+1
=
=>
Imax
n+
=
1
-
a
Star Triangle Ulta
i
-1i
=
*
itjmax
=
=
*
a*
-
=
*
2*
3
i 1, j 1,2,3,4
HW : Print the given pattern
H.W.
1234
123
12
1
1
I
and
Number Triangle Ulta
22
3
4
3
M.W.
I
2
I
Y
and
93
432
432
I
HW : Print the given pattern
ABCD
ABC
AB
A
Alphabet Triangle Ulta
Ques : Print the given pattern
1
2
3
4
1
13
3 1 3 5
1357
I
2
i
i
-
j
!
2
1
2
1
2
->
AP
3
3
Y
du
jus
Odd Number Triangle
13579...
W
*Ques : Print the given pattern
I
I
2
3
Y
↓
24
3
ej
1
23
456
7 8 9 10
n4
=
I
Floyd’s Triangle
n 2
=
I
23
*Ques : Print the given pattern
I
2
345+ j
1
01
3 1 0 1
Y 0 1 0 1
when
I
(i+j7%2
2
S
I
0
10
else
I
0 & 1 Triangle
0
=
=
-
-0
1
Ques : Print the given pattern
12345
1
2
4
*
*
*****
*
*
##
##
##
#
#
#
##
#
#
S ##
↓
I
#
nS
=
Star Plus
-j
it(i 311j 3)
=
=
=
=
else
n
3
=
A I
#
*
**
a #
#
-
-
#
nodd
=
x
HW : Print the given pattern
I
23456
******
*
*
*
i*
******
-
j
I
2
-
- - -
--
d
stars
-
first
last
now
sow
firstcol
col
last
i
Hollow Rectangle
HW : Print the given pattern
* *
* *
*
* *
* *
-
-
-
-
-
-
-
-
-
--
-
-
-
Star Cross
modd
*Ques : Print the given pattern
↓
I
2
*
**
***
****
I
I
---
4
me
-
Star Triangle Reverse
3
looks
I
D
2
*
3
x
--
-
-
2
ander
2
- -
3
look
-
t
X
x
x
-
y
ya
x
ax
HW : Print the given pattern
1
12
123
1234
Number Triangle Reverse
HW : Print the given pattern
A
AB
ABC
ABCD
Alphabet Triangle Reverse
HW : Print the given pattern
I
****
****
****
****
I
I
-
2
--
2
- - -
2
--
3
y
-
Rhombus
-
Y
X
&
&
x
x
x
x
a
X
-
t
3
x
&
a
-
Y
&
x
&
& &
*Ques : Print the given pattern
*
***
*****
*******
Star Pyramid
HW : Print the given pattern
1
123
12345
1234567
Number Pyramid
HW : Print the given pattern
A
ABC
ABCDE
ABCDE FG
Alphabet Pyramid
**Ques : Print the given pattern
*
***
*****
*******
*****
***
*
Recommend
-
!
College
Wallah
Youtube Channel
d
playlists
d
c
Programming
Course
↓
Lecture 4-PP
L
Badiya (Tough)
Star Diamond
Functions
What and
Why?
DRY
as
not
repeat yourself
d
1)
2)
to
tackle
There
in
repetition
are
problem.
certainpeice of
code
we
that
use
a
lot
of times
Basic syntax
fun(){
// code
}
~~
of
~
function call
scall
I
Output
·Good
Morning
How
·
are
you?
Hey
·
Good
·
·
How
Morning
are
you?
main()
return ;
1
Reyword
kind
of
->
It
cannot be
jisse function
like
break;
called
ka
more
khatma
than
ho
once
jaata
Kaam ki baate :
1) main() ek hi baar aata hai.
2) Starts with main
3) unlimited functions
How functions work : ek ke andar doosra,
doosre ke andar teesra
Parameters:formal
-
7
-
↑
parameters/
dabbe
90
W
7
a
·
Fetual parameters
I
values
Pass
by
value
W
90
D
I
I
Outat
Return type : Sum function se samajte hai
d
72
9
Out
be Sum
Learn
a,
81
81
-
take
integer
I
81
Library functions
Squtt
<but()
min()
max()
pow (a,b)
e
ab
Ques : Combination and Permutation
↓
In,
"c
->
->
2
>
a
↓
me!
=
①
( x!
-
3
I
*6xSx1
-
u!"
·:
=
a)(byc)
=
⑱x
pr
=
y!x2!
⑰x1x2x1
O
=15
Ques : Pascal triangle
12345
O
0
I
I
1
I
I
2
↳
Y
S
d
I
printing
Pattern
D
ej
for
Nested
,
I
I
133
S
10
I
I
10
S
e;
I
I
2
I
33
I
I
146y
I
Look,
For
~
Y
is
"
1
I
S
I
roofs
Scope of variable
↓
that
Formal parameters and Actual Parameters
I
↓
Done v
variables
dabbe
d
actual values
that
are
passed
Ques : Swap 2 numbers
MI:
↳
y
X
temp
x
y
x
=
y
=
temp
=
temp
Ques : Swap 2 numbers
M-2
-
-
-
without
using extra
variable
Y
Lie
2 Y+
2
Y
+
2
X
My
X
y
x
=>
y
=>
I
x
=
x
y
+
x
=
x
=
-
-
y
y
X
y
Pass by value & Pass by reference
swap (x,3
v
~
12:45
Y
W
12
12
temp
y
x
~
↳ ↳
~
45
-
X
12 YS
⑧
Pass
by
value
y
I
Output
S
&
12
US
*Pointers
->
int
int
x
Y
bytes
↳ E
2;
=
I
x
y
IIIIIIIIIIIIIII
ibyte
0x16....
*Pointers
I
int
x
-
3;
=
int* ptr
2 x;
x
=
coutyptrendl;
0x1I
i
ptr
0x14
1
Access
pointer
the value
is
using
of of
the
variable whose
dereference/I operator
address
in
is
the
*Pointers
3
W
~
12
~
X
~
Ox1Y
~
W
↓
Pass
by Reference
⑲
ptr
0x26
I
Outfit
-
-
23
*Pointers
0x19
0x12
v
-
v
1/
*a
⑭
in
b
temp
0x44
0x 90
a
45
=
-
W
0x22
I
Output
&
12
-
-
~
*
~
12
w
w
~
⑧
Pass
by Reference
~
US
X
y
0x12
0x19
4S
State TRUE or FALSE :
1) The same variable names can be used in different
functions without any conflict. YES!
State TRUE or FALSE :
avoid
2) Every called function must contain a return
statement. FALSE
3) A function may contain more than one return
statements. TRUE
State TRUE or FALSE :
5) A function may be called more than once from any
other function TRUE
Recursion
Function calling itself
↓
1)
Repetition
2)
Infinite Loop
Ques : Print n to 1
↓
n
S
=
s
:
I
w
print
->
in
G
no
point
quintin-1
--
↓
-
~
2
↳
~
-
:I
:3
In
-
I
&
I
Base Case
-
condition
Output
=I
~
&
~
v
⑧
2
b
S
~
out
X
-
⑳
o
:
3
·E
·
Ques : Print 1 to n [Recursion]
↓
I
2
"
:
C
contnendl;
print (n+1)
e
n
Ques : Print 1 to n (after
recursive
call)
~
Without
extra
Parameter
X
Proper
men
Dry Pun
!
W
-
-
⑧
3
↳
"
Is
X
⑯
·
⑧
⑧
-
I
·
Output
-
Enter
I
·
⑧
·
.
a
3
n
:3
Ques : Print sum from 1 to n (Parameterised)
w
i
-
College
Wallah
I
Playlist
↓
C
programming
↓
Lecture
-
6
Recursion
↓
sum
N
1 to
parameterised
Ques : Print sum from 1 toE
n (Return type)
->
n 5
1 23 4 5
->
->
+
=
+
+
+
+
5 4 3 2 1
+
1 2 3 4 3
+
+
sum(r)
=>
sum(n)
=
+
+
15
=
+
+
+
2 1
..3
+
n
=
+
n
=
sumi-1)
+
+
ofmore
the
Ques : Make a function which calculates the
factorial of n using recursion.
3!
->
-
5x4x3x2x1
=
n!
nxn-xn-2x....3x2x)
=
n! ux(n 1)!
-
=
d
fact(n)
n
=
fact(n-1)
x
d
fact(1)
1
=
Ques : Make a function which calculates the
factorial of n using recursion.
Tree
Diagram
ener
:
120
fact( S C
2
n
3
=
24
S*fact (4)
↓46
4
fact(3)
x
22
3 *
factfat
i
Ques : Make a function which calculates ‘a’
raised to the power ‘b’ using recursion.
3"
a 3
=
I
b Y
=3x3x3 x3
3
81
=
=
->
a
=
axa xqx a...
a
-
b times
ab
->
a
=
pow(a,b)
=>
->
base
xab-
1
a x
=
case
e
powla,b-1)
b 0
=
ea:1
*Multiple Calls
Ques : Write a function to calculate the nth
fibonacci number using recursion.
1123381321345589
n
12345678
10
9
11
=
fibo(z)
fibo(8)
->
->
=
fiboln-1) fiboln-2);
fibo(n)
fibo()
+
=
1
=
fibo(2)
fibols)
+
1
=
. .
Gro
L
L
fibol
34
fibo(s)
8
-
+
↑
·
3
fibo(y)
Hottub0(2)
Fiboli)
fibolus
fibr Fibulz Fibiesfibores finisfied
3
-
1
2
·
t
L
2
+
L
Sibol
11
fibo(2)
+
Enterthe
tree
HW : Power function (logarithmic)
abz
⑳2
L
I X
=
a
8
2
2
-
x
28 24x24
=
2
2" 22 22
=
2x26
27
2
=
-
6
2
z4
2x2
-
2x2
Y
2x23
-
2x22
24
i
-
S
=
2x21
=
I
2
x
0
20
x
x
22 2'xz
=
2'
⑧
2x2
=
DSA
e
data structures
&
algorithms
1
data
stone
-
variable
->
intx;
Arrays
What is an array?
I
el
d
List
->
list
list
list
declaration se
of integers
of
char
of float
multiple
e
is
e
dabbe
11,76,13,88,46
A, B, A, C,
F
99.4,93.1,90.8,94.6,
band Santa
noon.
50.S
Indexing
Syntax and Declaration
0
int x(5];
Y
X
[0]
x
x[1]
x
[4)
x(2)
x(3]
I
4
=
2
=
10
=
6
=
=
-
2
2
e
2
6
-
3
Y
2
10
initializing of
array elements (individual)
Accessing Elements of Array
↓ Very
using
simple
square
brackets
arr
[41;
Printing Output and Taking Input
using looks
Ques : Are the following array declarations correct?
int a (25) ;
X
int
a
int size = 10, b[size] ;
int c = {0,1,2} ;
X
[25];
v
intc(3)
20, 1,
=
23;
or
intc[1=20,1,23;
int size 10;
=
int
b(size);
Ques : Which element of the array does this
expression reference?
num[4]
int
d
5th
element
from
start
&
the
4th
index
element
num
[6];
4,
Memory Allocation in Arrays
Elllllllllllllll
int
arr[3]=24,3,23;
Passing Array to Functions
-
Reference
-
~
-
w
-
I
arrity
Output
Ques : Calculate the sum of all the elements in the
3
S
01
given array.
2
intavr[] [5,
=
↓
Sum
G
0;
=
sum+=arr[0];
arr(1]
sum+=
&
&
!
arr[s];
sum+=
1, 2, 4,
4
6,33;
Homework : Calculate the product of all the elements
in the given array.
Ques : Find the maximum value out of all the elements
3
4
0
1
in the array.
intarr[] [1,5,6,4,33;
2
=
-
intrx
arr[O];
=
mx
=
d
mx max
=
(mx, arr(i])
56
Homework : Find the minimum value out of all the
elements in the array.
HW : What is the difference between the 5’s in these
two expressions?
int num[5] ;
num[5] = 11 ;
unit,
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
State TRUE or FALSE :
1. The array int num[26] has twenty-six elements. TRUE
2. The expression num[1] designates the first element in
the array FALSE
3. It is necessary to initialize the array at the time of
declaration. FALSE
4. The expression num[27] designates the
twenty-eighth element in the array. TRUE
HW : Given an integer n. Create an array containing
squares of all natural numbers till n and print the
elements of the array.
->
int ni
cinn;
intains;
n
4
=
↓
9/10
Homework;
Ques : Given an array of integers, change the value of
all odd indexed elements to its second multiple and
increment all even indexed value by 10.
-
>
2
01
intarr(s)
3
Y
[1,2,3,4,53;
=
↳
↳
↳1,
[11,
4,
3,8,53
4,
13,8,153
Homework
Ques : Count the number of elements in given array
greater than a given number x.
--
-
-
intarr[] 21,3, 0, 10,
=
intx 4;
=
int
↳
count=
0;
if (arrlit,x)
count++;
2,
5,
63;
HW : Find the difference between the sum of elements
at even indices to the sum of elements at odd indices.
01
2
3
4
5
6
intaur)1=21,3,0,10,2,5,03;
9
sumeven=
sumOdd
↳
9
-
18
18
=
-
Ques : Find the second largest element in the given
Array.
intarr[]
int
mx
for (int
I
mx
< 10, 1, 0,9, 4, 12, 1,
=
INT-MIN;
my
=
i
0; ic;
=
it+
(2
INN 1012
=
smax:EMIN 10
max(mx, awr [i]);
=
3
int
INT-MIN;
smax:
it+ d
for (int :=0:ic;
I if (aw(i)! mx) smax
=
3
=
23;
max/Smax, aur[i]);
Ques : Write a program to copy the contents of one
array into another in the reverse order.
intads] 3 1,2,3,4,53;a
=
bit."
intb[S];
for (i 0;
=
i
n
=
b(i)
3
ic 4;i
=
-
)[
+
+
1- i;
a
=
REALs
[s];
i -
j
E
E-i
i
+
n
=
-
1
Ques : Write a program to reverse the array without
using any extra array.
01
intal] [5,
=
2
2, 3, 4,
i
I
I
#
Lit 1) Swap
2)
Two
-
a
[i], al;]
variables
in;
3
4
13;
i 0;
=
j
n
=
-
1;
Homework : If an array arr contains n elements, then
check if the given array is a palindrome or not.
arr=[1,
2, 3, 4,
13
3, 2,
E 1,2,3,3,2,13
W
w
=>
[1,2,3,4,2,13
=
X
10 students
1) Basics
2) It
Eddi
Else
3)
Loope
4)
PP
5) Function
P
&
Pointers
6) Revision
2)
Arrays
Arrays
CM
E
2D Arrays
Array
-
2D
Away
list
Table
What and
Why?
So far we have explored arrays with only one dimension. It is also
possible for arrays to have two or more dimensions. The two
dimensional array is also called a matrix.
grid
ecolumns
int arr[r][c];
↳ras
This is a 2D array where r depicts number of rows in matrix and c
depicts number of columns in the matrix.
declaration.
,"I
e
↳ int
int als):
arr[4][3];
alz]
O
12
11
9
O
I
2
3
ar(2]/2):
20
↳
awe
->
arr[3][T
arr[2]/0]=11
20
=
9
3
=
int
an
[2][3];
O
I
2
O
6
I
2
I
Y
-j
-
435
35
an
[0][0]
aur
(OIC1)
arr
[0](2):2
(1](0)
Y
=
6
arr
1
arr[1] [11: 3
=
13
G
2S
I
=
64
612
arr[1] (2):3
Initialisation of a 2-Dimensional
Array
int arr[4][2] = { { 1234, 56 }, { 1256, 43 }, { 1434, 32 }, { 1312, 96 } } ;
int arr[4][2] = { 1234, 56 , 1256, 43 , 1434, 32 , 1312, 96} ;
int arr[2][3] = { 12, 34, 56, 78, 91, 23 } ;
int arr[2][3] = {12, 34, 56, 78, 91, 23 } ;
array
2D
-
array of
away
intarr[333]
O
O
I
2
I
=
[[1,2,33, (4,5,63,[7,8,933;
12
2
3
436
I
89
aww
intaur
int
(3[33:(91,2,33,44,5,63,97,8,933;
avr[][3]=[1,2,3,4,5,6,7,8,93;
Ques : Write a program to store roll number and
marks obtained by 4 students side by side in a
matrix.
Harsh, Sanket, Urvi
4
students
-
Ragnar,
76
R
O
H
I
S
U
2
3
8
I
0
I
16
81
13
76
0
82
al
I
88
90
0
13
82
88
76
91
90
2
3
82
88
to 90
90
I
7613
81
R
U
Ques : Write a program to store 10 at every index of
a 2D matrix with 5 rows and 5 columns.
int
aur
[S][S];
12
3
""
10
0
is
Y
18
co
2
10
10
10
10
10
3
10
10
10
10
10
Y
10
10
10
10
10
Homework
Ques : Write a program to add two matrices.
- -
O
I
0
I
9
4
I
O
a
2
2
7
O
t
2
2
I
0
6
I
12
O
11
S
S
I
S
10
11
I
I
3
Y
9
b
cliJi):a(i][] b(i][i];
+
->
0
C
Ques : Find the maximum element in a given
matrix. ↓
Same
Ditto
as
ID
away
↓
int
INT-MIN;
mx
busing
=
nested look
traverse
the entire
L
mx
max
=
(mx, arr(i)(i));
ID
array
HW : Find the minimum element in a given
matrix.
HW : Find the sum of all elements in a given
matrix.
d
int
sum
0;
=
↓
arr[i];];
Sum+=
HW : Find the product of all elements in a
given matrix.
int
product: 1;
product
x
aur(i)(i);
=
HW : Given a matrix ‘a’ of dimension n x m and 2
coordinates (l1, r1) and (l2, r2). Return the sum of
the rectangle from (l1,r1) to (l2, r2).
O
O
i 1 to Y
=
I
I
2
I
2
I
2
3
S
9
3
7
4
8
I
12
13
int
17
↳
10
11
13
16
17
19
18
20
-j
a[8][4];
-j
3
1 to 2
=
(11)
and
(4, 2)
Ques : Write a program to Print the transpose
of the matrix entered by the user. AW create
->
1
d
Already
done
the
transpose
store
Column Wise
O
O
7
I
2
3
0
I
Y
I
2
S
->
I
Y
S
6
I
O
2
2x3
2
3
6
3x2
Prin
ting
in
a
matrix
it
new
2)
DW
I
skills
PWskills
&
2)
Vector,2D vector
DS A
L
Time &
space complexity
Sorting, searching
Linked
list, stack, Quene,
Trees,
maps,
Set,
leaps
Graphs, DP, OOPS
Decode 2.0
College
Wallah
Batch
-
-
C++,
Playlist
DSA
(Paid)
Sanket
Mrvi,
eC++&DSAfoundation course
1D
Array
raghar
I
I
integer
float,
away
Char
Strings
!
char
array (6):
E','hav
What are strings
and Why are
they used?
L
questions
->
Char
Arrays
string
str;
Declaration of Strings
and taking Input
d
string
si
cins;
cout<<S;
↓
getline/cin,s);
~
strings:"Raghav Garg",
Indexing of
characters in
Strings
d
strings:"raghar";
cont<S;
cont
<<
S90]://v
I
length)).
strings:"abc";
sitic
"o'-hull character
d
ASCII
-
O
>
Ques : Input a string of length
n and count all
the vowels in the given string.
Updation of a
single character
in string
↓
Done
H.W.
Ques : Input a string ⑦@°=I>
of size n and Update all
the even positions in the string to character
‘a’. Consider 0-based indexing.
-
-
012345678910
strings:"Raghav Garg";
E
aaahavaGara
Built-in string functions
d
STL
↓
standard
template library
size() / length()
d
baat
kar
le hai
push_back() & append()
strings:"raghar";
pop_back() & clear()
string
to
empty string
“+” operator
reverse()
↓
strings:"raghar";
Ques : Input a string of even length and
reverse the first half of the string.
i
I
012345
->
string
↳
s
=
"raghar";
-
garhav
Homework
Ques : Input a string of length greater than 5 and
reverse the substring from position 2 to 5 using
inbuilt functions.
-
-
strings:"raghar";
012345
↳
reverse
(s. begin))+2,
s.
begin()
+
1);
6
to_string()
->
int
to
string
d
int
4
x
12345;
=
strings:to-string(x);
Ques : Return the total number of digits in a
number without using any loop.
Hint : Try using inbuilt to_string() function.
x
91632
=
stoi
d
string
"1412361";
=
intx stoils);
=
Maza
aa
gay a
Thank you!
Download