This document discusses using arrays and matrices for microcontroller programming in C/Assembly languages. It covers:
1) Declaring array variables and initializing arrays with values either inline or element by element.
2) Two-dimensional matrices including declaration with dimensions, accessing/modifying elements using subscripts, and initializing values.
3) Examples of one-dimensional and two-dimensional arrays of integers and characters.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
44 views
Array and Matrix in MikroC Programming
This document discusses using arrays and matrices for microcontroller programming in C/Assembly languages. It covers:
1) Declaring array variables and initializing arrays with values either inline or element by element.
2) Two-dimensional matrices including declaration with dimensions, accessing/modifying elements using subscripts, and initializing values.
3) Examples of one-dimensional and two-dimensional arrays of integers and characters.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7
MlkroC rogrammlng ln
A88A? or MA18lx and LexL sLrlngs
lnLroducLlon Llke ln any programmlng language Lhe concepL of a varlables ottoy can also be used for mlcroconLrollers programmlng an array ls llke a Lable or a group of varlables of Lhe same Lype each one can be called by a speclflc number As such maLrlces help Lhe programmer Lo organlze collecLlons of daLa efflclenLly and lnnovaLlvely LaLer we wlll look aL Lhe concepL of Lhe loJex polotet whlch ls a naLural exLenslon of Lhe maLrlx (where Lhe maLrlx can be named as an lndlcaLor consLanL) Dec|arat|on of array var|ab|e or example 1 an array can be declared Lhls way All LhaL wlll do ls wrlLe Lhe maLrlx and Lhen Lype Lhe name and slze also comes ln Lhe lllusLraLlon
O lf we have a maLrlx conLalnlng Lhe four elemenLs (varlables) Lo announce by Lhe words int orr 4 O 1hls means LhaL we have four places ln Lhe book of memory each Lhe slze of tbe votloble lot nor din 5 eclares an array named dlce conLalns 3 varlables of Lhe Lype of characLers 8e Lhe flrsL varlable dlce 0 and Lhe laLLer have dlce 4 O nor disp/oy10
Lhls wlll creaLe a group of 10 varlables Lach one of Lhem ls accesslble by lLs number amp|e1 If we want a matr| (group) of s| |ntegers (ca||ed numbers) we wr|te |n the |anguage of C
O nd an array of (free and ) are ca||ed |etters we can wr|te
where dlsplay1 wlll be equal Lo 40 noLe LhaL dlsplay conLalns 10 dlfferenL varlables numbered from 0 Lo 9 ln LhaL prevlous example accordlng Lo Lhe varlable declaraLlon Lhere ls noL such varlable locaLlon as 'dlsplay 10 and uslng lL wlll cause an error ln Lhe compller
@ M@Ik INI@II2@ICN 1here are Lwo ways Lo lnlLlallze maLrlx 1 1he flrsL way 1hen Lhe maLrlx ls wrlLLen lmmedlaLely afLer Lhe values of (le eclaraLlon ln one llne) curly brackeLs such as nor p l l9 lL ls a maLrlx composed of leLLers Croup 1he flrsL elemenL ls p 0 value 3 and Lhe second elemenL ls Lhe p 1 value of 6 and Lhe Lhlrd elemenL ls Lhe p 2 value of 9 int orr 4 10121l15 O or example lf we puL Lhe Lhree values ln Lhe above example fourLh places wlll remaln wlLhouL value 2 1he second meLhod lnlLlallzaLlon of each elemenL ln Lhe maLrlx (elemenLs ln Lhe row sLarLlng from Lhe flrsL elemenL and lLs lndex 0 Lo Lhe fourLh and flnal elemenL and lLs lndex 3) O amp|e
O nother eamp|e
lf we wanL Lo access Lhe asslgned value ln Lhe 2 nd array or lf wanL Lo sLore Lhe value ln Lhe 2 nd array we can lnlLlallze ln Lhe above formaL 1he remalnlng elemenL ln Lhe array wlll be zero Array can lnlLlallze ln Lhe above formaL O amp|e 2
1he flrsL llne varlable declaraLlon of Lype lnL as x (a place ln memory) 1he second llne Make Lhe value of Lhe varlable x value found ln Lhe Lhlrd elemenL (slgn no 2) polnL of Lhe maLrlx deflned prevlously noLe IN C NGUG @ NUM8 @@ING ICM 0 C IC DCING @ NG@ CI I IMC@N@ MN@ 0 CIN@ 0 NUM8 @ VU IN @ I @ I2 CI @ MINU CN IN @ kM 8CV I @ GUID @C @ MN@ CI 0 @C S MU@IDIMNICN C SupporLs mulLldlmenslonal array 1he slmplesL Lype ls Lhe Lwodlmenslonal maLrlx Whlch creaLes a maLrlx (Croup) conslsLlng of a recLangular rows and columns @wod|mens|ona| arrays Dec|arat|on of the matr| w|th d|mens|ons 1o wrlLe Lhe Lwodlmenslonal maLrlx flrsL wrlLe maLrlx lengLh and Lhen add row as follows
When declarlng an array daLa Lype array name and number of elemenLs of each dlmenslon should be speclfled
ow to wr|te the two d|mens|ona| array |n the pre determ|ned va|ue as shown |n f|gure
Note to wr|te the matr| of the f|gure on the |eft we m|ght do the fo||ow|ng |n a comp|ete program Note that the array cons|sts of 9 (33) e|ements of type |nteger vold maln()
@he "green" matr| va|ues are pre determ|ned va|ue of the array
G|ve va|ues for the twod|mens|ona| matr| () Method 1 O 1he baslc sLrucLure of lnlLlallzlng a Lwodlmenslonal array ls much llke a onedlmenslonal array Dec|arat|on and then create |e |nt name 2 4 Method 2
It |s s|m||ar to the oned|mens|ona| matr| O amp|e char Lemp 3 2 ere Lhe eclaraLlon of maLrlx characLers (le number of row and column) and lLs name Lemp (name of maLrlx) value of a maLrlx composed of 3 rows and 2 columns 1he flrsL elemenL Lemp 0 0 0 and Lhe flrsL row flrsL column 0 1he second elemenL ls Lemp 0 1 Lhe flrsL row of column 0 and Lhe second 1 1he Lhlrd elemenL ls Lemp 1 0 1 and Lhe second row flrsL column 0 1he fourLh elemenL ls Lemp 1 1 Lhe second row of column 1 and Lhe second 1 1he flfLh elemenL ls Lemp 2 0 2 and Lhe Lhlrd row flrsL column 0 1he lasL elemenL ls Lemp 2 1 2 Lhlrd row second column 1 O amp|e or a maLrlx of characLers conslsLlng of 3 rows and 3 columns can be wrlLe as below
eclaraLlon char name MaLrlx name characLers Lwo_d the number of rows Number of co|umns 3 and S
@o access ] mod|fy the va|ue of e |n th|s matr| we need gu|des subscr|pts
O 1he flrsL llne eclaraLlon of Lype char varlable named ch O 1he second llne uL Lhe value of Lhe varlable ch ln Lhe Lhlrd row (le0Lo23) (wlLh Lhe value of 2) and Lhe flfLh column (wlLh Lhe value of 4)
(Cr)
O 1he above example has a value of 'k' (lndlcaLed ln Lhe quoLaLlon leLLers) ln Lhe flrsL row and Lhe flrsL column of Lhe maLrlx Lwo_d |m||ar|y we can conf|gure a mu|t|d|mens|ona| as fo||ows
le lnL conLalns 2 rows 3 columns and Lhe elemenL of Lhe maLrlx was lnlLlallzed 1he elemenLs of Lhe maLrlx ln Lhe flrsL row (le 0 Lh row) 3 2 1 and Lhe second row elemenLs (le 1 sL
row)6 7 8 Dea||ng w|th the str|ngs (tet) us|ng matr|ces