0 ratings0% found this document useful (0 votes) 54 views13 pagesC++ Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
(ARRAYS)
* Single unit 05 multiple values:
«A fixed size collection OG tems with same datatype
» Hemogenaus collection.
Storage 7
Arvay is stored sequentially in meniory.
-? Contiguous Memory 3
*.a chunk of memory allocated without any gaps in
the addvesses it occupies
+ One single unbroken " black” °f memory.
What (fF memery is non- contiguous :- * viel
An away is p6ficontiquous as it is an indexed based dato structure.
In absence contig
to create an ayray-
An enor will be thrown.
vous Space available in MeEMOrY,. Program wil) fel.
Declaration :
Dalatype Arvayname [ int - expression]
avray subscripting
fat array Var 4. eye eperator-
Datatype
atray narne Size (Sze mst be integer os ¢t is
the count of array elements )
Memory View :
arrayVay 4. [oJ Ue)
orray Ver 4 [4] 20
Size 5 arvayVay 4 [2] 30
away Var 4 [33 4o
avvay Vay 4. [4] 50
/
Index¢ More than one ayray can be declared ona Sine:
tnt student [10], Faculty [30] , Player [20] 5
« Aways can be declared alongwith the declaration 06 voriables.
int var, var 2, marks [8] 5
Inikalizers =
Elements of array.
int £5] = §1523,4.5}5
aay
initializers
'F size is not provided during declovation, initializes will determine the
size.
Limitations of arrays =.
° We must know how many elements are to be stored in orray
e Array is static Structure with pixed size,
so the memory allocated
to array cannot be changed:
0 As the memary is contiguous, insertion and deletion is ‘difficult and
Hime consuming.
Types oh arroys :
ARRAY
Muli
one two dimensional
dimensional dimersionalA (2D-ARRAYS)
* Arrays with 2 or more dimensions
* Covrespord to matrices
1D arrays —> List
2D arrays —> Table
Definition:
* A fixed size collection of items off same data type arranged fn two
dimensions-
© 2D arrays can be considered 95 a table with rows and columns.
e Each element 06 2D array ts referred. to with help 06 2 indexes.
4 index indicates row and 2 index indicates column-
Declaration :
Mt myarray [2](3] 5
data. Column
type array subscript
hame Row
subscript
—> int myAray (3) [2] = {£12016}, {22,525}, {32035}}
W
column O column 4
Row O 12 15
Row 1 2! 25
Row2 32 35(FUNCTIONS)
+ Problem solving approach for complex [forge problerns
+ Basic Rule — Divide and Conquer
v :
division of forge problem
into smaller parts and then
solving them
Definition :-
A function groups a number 6 prograny statement
into a unit and gives tt a name.
« Dividing a progyary into Bunctions is. one of the major principle h
Structured Programming.
. Every Ct progrann hos atleast one fercton, thot 1s maint)
Advantages :-
Using unctions in a program is very beneficial ond
is considered a good approach:
Following ave Listed some advantages OB Benctions +
» Avoid repetition of samme code in program.
> Improve readibility of program.
& Make program modification easier-
& Debugging becores easier.
& Mulkiple persons can work on the program (on seprate functions)
Types:
2 types of functions iin c++.
Function
pre- defined. user-definedpre- -defined function +-
Functions that Ctt fs alreddy Providing
to users through different braries -
pre-defined functions are organized into seprate Bibyories .
+ For input ond output functions, fostream ts available-
+ For all math Genctions, cmath is available.
examples are :
«clyscr () — cleay screen
= pow () — power
= sat (26) — saqvare root
user-defined function +-
Furckons that ave created by users
according to the requirements - :
€-g+TO find sun 0B 2 numbers
Calculate income tax
USER DEFINED FUNCTION
oP oS
value
vetuning votd
Benction benction
e Value returning Benction +
These fyrctions have a return type- They must >
¥elurn some valve of) a specific data type.
© Void. function :
These Gunctions dont have any retum type They dont
use return statement to retum a value.USER DEFINED FUNCTIONS
Functions created by user according fp requirements:
Components :
+ Function declayation ( prototype)
¢ Furction definitfon
¢ Funchon callt-
syntax of cormponents :
Decloyation
returtype functon.name ( porameters)
Colt
forction.. name ( parameters)
Depinitfon
relum. type function name (porarneters ) f
statements 5
Declavation :-
« Function declaration provides information to compiler by telling its
relum type» parameters {datatypes and function name -
* Declaration is followed by a Semicolory (5)
* Declaration is required When a fyrcton is defined betow or apter its
call: In this case, funcHon should be declared before call.
int sum (int x> int y) $$
ais /
return type
semicolon
furction porameters
hameDefinition De
« Function bady is called its definition.
elt tells the actual Bunctonabty and Logic of program.
elt actually informs what the Buncton will do in program .
NY irop
If forction is defined. before its call, then its definition also serves as
tts declarakon, so a prototype (seprate) is unnecessary.
But 16 function is defined apter its call, then prototype is necessary
before call
int_sunn (int x» int) > obo acts os declorator
statements 3 —> body of, function.
}
Return type > Retum type is the data type of value Hat function returns-
Parameters —> It is just Sike a placeholder. We pass actual values to these
Parometers during function calf:
Those actual valves are called as ‘arguments’
In Ctty you give a definition of what a function has to do yank
then to use that bunction , you will hove to call that function
Wn Burchon calf, you have to pass the actual values (arguments) instead
H the povometers used in definition
sum (arguments) 5 a.
Fonction name semicolon
arguments to
be passed.(POINTERS)
Pointeris a vorioble that holds the memory address of) another variable
Normal Vorable <> use to Stove values of their type.Cdivect veference)
Pointers => use to Store address cf voriable. (indivect reference)
Memory address :-
Location of another vorioble where it has been stored.
tn the memory.
+ pointer holds the address of a Variable, we eax say that
it points to thot variable:
Declaration :
Must be declared before their usage-
* Stas
datatype variable — name 5
+ Int®x (pointer to int)
+ float * ptr (pointer to Foot)
+ char*z (pointer to chor)
Address operator (&)
« Address operator (8) is used tp produce an address of voriable by
Operating on voriable name-
« If we wont to refer to address ofa particulor varable, we con use the
address operator
var 4
vert —> This will give the oddress
Dereferencing operator (*)
© unary operator thot operates function on poinfer voriable-
2 We can access the value stored in the variable through dereferencing
Operator:
« It retums the contents of variable Located at specific address-
float* ptr;const pointer | non constont data :
Pointer ‘tselg is constants but the value t's poinking to is non-constcer
pointing towards
Const: pointer :- pointer &s a specific voriable- Apter
constant, the variable to which it is pointing
becom
cont’ be changed.
ink x = 63
int * const ptr = &xs V~
ptr=ay; x
Non- constant data :- As data is non-constant, so the value of, the
Variable pointer is pointing to can be changed -
int x= 63
int*const ptr = &X 5 vw
Aptn= $5
const pointer / const data :
pointer and the vorioble it is pointing to , beth are constant
fint value = 55
const int * const ptr = & valve 5
Noither the address of, pointer can get chonge nor the valve it ts
pointing to
non-constant pointer /constont dato.
pointer itself is not constant but the valve it is pointing to fs constant
ard hixed-
Non- constant pointer 2 As the pointer ts non-constant) it can point
to multiple addresses. We can chonge the address:
const int x = 55
consE int * ptr =bas |]
const int y= 65
ogy ptr = &y3const - data +
int x = 53
const int * ptr = &x 5
thiough pointer.
X=65] 4
(RECAP)
int x = 535
4: int * ptrd = &x 35
(non-constant pointer and non-constant dota)
Dis const int* ptr2 = &% >
(non- constant pointer and constont data)
3: int * constant ptr3 = AX 3
(constant pointer and non-constant data)
4: const int * const ptr4 = &X 5
( constant pointer and constant data)
data is constont, its value cannot be changed
sptr= 63] x we cant? chonge the volvePOINTERS TO VOID
General purpose pointers hat can point to ony datatype
void ptr 5
POINTERS IN FUNCTIONS
3 methods for passing parameters fn fynetions
e Pass by value
e Poss by reference
© Pointers
Definition =-
Void furction-name (datatype * ptrvor) 5
Cal :-
function- rame (& var_nome) 3Static Memory
Allocation
« Aso called as
Compile Hime allocation
«size and Location where
variable will be pexee
Stoved. fs fixed during
compile Lime.
* Both alfocabin and
dellocakion Of, memory is
done by wmpiser rtself-
+ shigyily faster
* memory allocaben on
Stack.
DMA
New operator
e used {0 allocate memory
bo a variable
@ pointer is used to allocate
memory Hyrami cally.
® pointervarfable = new datatypes
Dynamic Memory
Allocation.
+ Mso cabled os
Ruun time calfocation
+ Memory yequirement Should
be rey olepined during the
execution 08 program .
© Programmer needs to write
proper code ber alfocatin
and delDocation. |
° sdigntSy sDower-.
+ memory alfocaHon on
heap -
Dalete operator
used to deOlocate tte
wemory occupied by variable.
pointer is used to deagifocate
ithe memory -
delete potervariable sLOCAL VARIABLE
Variable defined inside a
Puretion body
Its scope ts Limited to the
Guretion where it ts defyined
Is Lifetive ends viren the
Byrction exits:
GLOBAL VARIABLE
Voriable depined globaulty ,
outside all Bunctions
Its scope is the whole
program:
is Bife erds only when
Yhe progrem ords
STATIC LOCAL
Also defined inside a
Bunction-
Its scope f th whole
program from where it fs
initial zad -
lis Dife ends with the
ending o§ program.