0% found this document useful (0 votes)
5 views11 pages

Dimensionl Arrays Are:: Be Dynamicals Alocated Using A Single

The document discusses two methods for representing two-dimensional arrays in programming: using a single pointer and using an array of pointers. It provides code examples for dynamically allocating memory, inserting and deleting elements, merging arrays, and multiplying matrices. Additionally, it explains how to calculate the address of elements in both row-major and column-major forms.

Uploaded by

priyanksingh9875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Dimensionl Arrays Are:: Be Dynamicals Alocated Using A Single

The document discusses two methods for representing two-dimensional arrays in programming: using a single pointer and using an array of pointers. It provides code examples for dynamically allocating memory, inserting and deleting elements, merging arrays, and multiplying matrices. Additionally, it explains how to calculate the address of elements in both row-major and column-major forms.

Uploaded by

priyanksingh9875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

I.

Two ways of representing two


dimensionl arrays are:
- Using a single pointer: A•D aroay co
be dynamicals alocated using a single
pointer. Ihe pointer points to the irst
elemernt of the array, and the elements
are stored in row-mayor order. lo access
the element ati, We need to
calculate the offset asi cols t , where
cols is the number of columns in the
For example, to create a D
rray of size 3 x <, We can use the
rray
follounq code:

CkBde
nt rows = 3, cols = 4;
=

Allódate memory for the rray


int *ar =Cint *)mallocrows * cols *
sizeofin+),
Assen ualues to the array
I. Two ways of representing two
dimensional array Gre:
- Using c single pointer: A•D array co
be dunamicaly allocated using a single
pointer. Ihe pointer points to the irst
elemernt of the array, and the elements
are stored in row-mGjor order. lo access
the element at Ci, We need to
calculate the offset asi cols t |, where
cols is the number of columns in the
For example, to create a D
array of size 3 x 4, We can se the
rray
follong code:

CkBde
rows = 3, cols = 4;
Alócate memory for the rray
int ar= Cint *)mallocrows * cols
sizeofin+),
Assen ualues to the array
int coUnt 3O
for Cinti = O, i< rows, it+)
for int j - O;j< cols, jt+)
*Larr i*cols t ) = +tcount,
Pradt the array
for Linti -= CO,i rowy, it+)
for Lint < cols, jtt)
| *
pintf"n).
cols tti

3
Foee the memory
freelar
- Using an of pointers: A Daray
Can nGso be dunmically allocated wsing
dynhe array
of pointers. he array of
ypointers point to the frt elerment of
each row of the array. lo accesS the
element ati, , we an use a r J
notation, For exaample, to create a U
of size 3 x 4, we can se the
follong code:

CkBde to create a arrayusing an


Grray of pointers
int ros = 3, cols ==A;4
Alódate memory for the array of
ot
pointers
int *karr = Cint **)mallocrows *
sizeofint *)),
Al\Qcate memory for
for each
eoch ro
ro) of the
rray
for inti = O;i< row, itt)
arrl= Cint *)malloc cols *
sizeofint),
As&kn ualues to the arry
int cOUnt =
for inti = O, i<rows it+)
O; i<
for int-O,j< cols, jt+)
arrT- ttcount,
P
Pradt the array
for inti = O;i <rows, itt)
for Cint
print"
j O ; <cols jtt)
pintf
Fee the memnory
for Linti =O, i <os; it+)
freelae),
freeare,
Giuen a •D array num3]L3], Base
addresS =|0O, w=bytes, the address
of the following elemernts are:
- numL3]in row-major form: The
address is clculated as Baset *
cols t * w, where i is the row index, iS
the column index, col is the number of
column, and wis the size of ecch
element. Iherefore, the address of
nymL31isIO0 +(a *5+ 3) *a
- numLL•lin column-major form: The
addressis calculated as Base + *
rows t )*W, whereiis the rowindex, is
the column index, rows is the number of
rows, cnd wis the size of each element.
Therefore, the addresS of numLLis
=||4.

3. Vaious operatiDns in an array are:


nsert an element in an lo
insert an element at a giuen position in
Gn rray, we need to shift the elements
from that pasition to the ight by one
place, cnd thern assign the new element
to that position. For example, to insert
B0 at position 2 in the arrauIO, aO,
30, 4O we can we the following
code:

CéBde to insert an element in an

iot ar-I0, 20, 30, 403


4: /Bze of the array
int pOS Lposiion to insert
Z6alue to insert
SMtt the elements from pos to ight
by one place
for Linti=n-,i pos, i--)
hbert the walue at pos
arpas) oaly
krement the size of the Grray
ntt,
Kdt the array
for inti = O;in, itt)
pintfu7d", arrLl),
- Delete an element in an array To
delete an element at a gioen p0sition in
Cn Grray we need to shit the element
from that position to the left by one
place, and then reduce the size of the
array bs one, For example, to delete the
by
elemernt at pDsition • in the arrauIO,
20, 30, 403 We can use the following
code:

CkBde to delete an element in an


int arD-J0, 20, 3O, 403
int n = 4; ize of the array
int pos =2; Lposiion to delete
S t the elenments fcom poS to left by
one place
for Cinti = pas; i<n - , itt)
Dérement the size of the

pradt the arrcy


for Cint i = O;i<n; it+)
printf"od", arrL)
merge two arrays: lo merge tu
into aa single array
rrays into we need to
2w
Create a new arrGy that can hold the
elements of both arrays, and then copy
the elements of the first Grray followed
by the elements of the se cond aray. For
example, to merge the SIo, B0,
303 and E4Ð, 50, oO3
Single aray, weWe carn ue the follouing
code:

Ckbde to meroe two rays


int ar-S0,0, 303
int ol = 3, ize of the first aray
int arrI-4O, 50, (aO}
int na = 3, sine of the second array
Crete a new Grray that can hold he
Lelements of both arrays
int arr3Lnl t n ;
Cop the elements of the first array
for Cinti = O,i <n,itt)
Cofg the elements of the Jecond array
for Cini - O,i<na, it+)
Pradt the merged array
O,iZnl t nitt)
for Cinti - OiZnl
printfn7%d ", arr3L)

4. Afunction that will takKe tuo matices


as parameters and return the produet of
the two matices i3:

Ckode to mutipls two matices


Asóome that the atices Gre of size
3x3
defne N3
Fdhon to multiplstO matrices A
Gnd B and store the result in C
uoid multhiply int ALNILN, in+ BLNLM, int
CLNLM)
Loop Akcough each cou
row Df A
for inti =
Loop +hrábah each column offB
for Cint - O;<N, itt)
dnitiaize
Löop througk/each element of AL and
BLJ
for int k = O, k <N, kt+)
Add the productlof ALILK] and B[k
to CLLI
CLIL] t= ALILK] *
3
3
3

3. Aproqram to readn number of values


in an aray and display them in reuerse
order i3:

Cksde to readn number of ualues in.


aray and display them in reverse
Drder
# inclucde
# include
int mainl)
//Declare an arraypointer
int *ar
Declde a uaiable to store the si2e of
the array
int n
Promg the user to enter the size of
the array
printf"nter the size of the Grray
Readthe size from the user

locaté memory for the

You might also like