0% found this document useful (0 votes)
53 views

Control Statement

PPS UNIT -2 MATERIAL

Uploaded by

k.kalaivani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
53 views

Control Statement

PPS UNIT -2 MATERIAL

Uploaded by

k.kalaivani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 36
| UNIT- LooP C Contxe} Slajemenlo ; _ %5 defined 0S 0 block oF Ghalements cakich ne nepentedsy “hl in in of Hines - executed fon ae numbe: 1576 You wont fo "pean the some opeadkion sepedtedly number of theres, then nop Gill be used | steps ta Leos 1+ Loop vonioble The vaniable used fn Yhe Loop. 3h % the Findk Slep fa cohich the Skanking con) tnilial value ‘5 assigned 10 the Soop voniable . dition denmineS how many Himes Q. Init olizakion + he Lop will enecule 3. Test conditions “eat Cor eee: (Updating the oop voaioblé - i ‘Tncnemen pe © lorquose suppers 3 nop cork stolemenlS: 1 The while loop B- do-wkire 100P 3: fon Loop” jp bie While Loops She basic fonmat of while loop 5, | we cent a | { toody oF the LooP [ee lane hile % an enlay-conlagued koop- op tysoRttat —— i Dihe test-condikion {5 evaluated fingtond *F the condition % dave, then the body of she loop 'S executed ‘ “ e>Aflen execution oF the body oF the loop, the 'eah-cordition 16 once ogaia evaluated ond *f nue, the body oF he exp %5 euecued once oaain. rue Process of repealed execubion of the body conkinucs ku, the tect-Condition becomes falge- DIF the tech condition becomes false, thea the control % inongfenxed out OF the 100 Gnd the pnogram execulion conkiques ith te ‘ahabement ‘immediolely aflen the Joop , ‘Buample paggaam:- : ®ilaile 0 pacgnam to display the message “welcome” io -Hmes- ~ (2% Pogaam 40 display the meseage “Welcome” 19 Hmes using While Leopxe/ ‘maint ) f ourPuT:- | fr ey Welcome fet; hielicome hile (£210) welcome Welcome Paintf(m Welcome"): elon fof; Soe Lb Welcome 3 welcome, getche; ee 4 welcome eptalon: Th the above program, % f ‘rikilized to 1 The whise Loop checks the Cendikon fon fesi- Stace the Cordiiion % tnue, ten the Joop. wit be ” ereculed: ie. tt gieplays the meesage “welcome” and tncnements. ¥ value by 4 Now + value becomes &: Again & Gilt check. the condition, the condiKon % tue and ody of the Loop gel6 execuled: Tis process conknues KM the Condition becomes false- ere B waite program to fied factorial oF 0 given numben ust) __whike _100P" i /% Facloniol of a gen cumben ¥/ mainc ) a Jot 9, fact =4; Clageal pninlf(" Enlén the numben:"); scanF(" Ad", &); | while(n>z1) fact =factany 2 Mey 3 printf("\n Fachonial oF gwen numben {53 yd", fact): getchc, 3 gnlen the number; foclonial & gven numben ig. 24 fxplondlion: - 8 Henalton: 6 Henghon uth Stenabion 20 tendo nck n=3 nz] condikion ups) 15 brue az & hue 2>21 % bue » fack=!xh=4 «facts ha Fock 122 \ nen-qc3 , neg? | gh Rendon : a nO 1 >i condition % false. G0 contnok % Honsfenned out of the loop > Fradly, *b displays the value of fack ce 21 i i tye! 5 tue facts 24 224 nz-lsd |8-do-cakile Lop’ — "=D Ihe basic foumak of docile Jeep f: bady oF Fe Loop: Jootite (test Condtkon) s Ned-skalemerk olen tke Loop q 3h db-cbike loop, Fash the body of the Loop % execuled exes Wa checks the condition ab the. end of He Lap: JF the condition %6 ve body of the Loop with be execuled once again. This paces Contin as tong as the condition %S true- . When the condilion becomes false, the loop wilh be Yenminaled and ‘the Conlnol goes 40 the statement immediately often the oop ro fF the Bee ‘loop *$ Gn @iit-Conisalled leop toecause ledt-condihon a ore ragnam:- je al 1% Program to deploy Fngt 10 numbers using do-whike Loop 2/ , Coreot tok fy a fey 3 4, Chngenc): i bo 6 ‘i 7 ~painlfCn ad Ds j Peay to Juhihe Cigzto): getchcy \ a | Dienence belween hike and do-while;- are nce ete ee while Atop: (-@. te cabise Lcop the Condition | ‘6 evolualed Fngt and Fi % fue then only the body OF the Loop coin be | executed. BIE He condiion 16 ‘nitiolly false, | the hike Loop cai vk be ereculed | aHleost once: - | ee Fale ahile( $< 30) painlfC." Welcome"); s Silene, he chile loop ott oF bE executed nce the condiion i false output: None 3° Syntou! Zz am 3 ckite(condiion) ij slalements; | woke: coke srauld not be end with | y do-wkite The chile loop 8 an -€nkny-Contnolled 1.the do-cohite Loop % Gn ef. loop: ie. tq do-while lop fingt the loody oF the Loop %6 executed, Offer thab ab he end of Aoop the, cendon 6 evoluated : 5 i 8. The do-while Joop wth execute ahleast once’ even fF the condition 'S irthoby folse- ex foo do { : PrinlfC" welcome), Jehile Cfez0) > Hese, the db-akzte Joop & Creates one Kee ever F the Condition &%, false Output! hleLeome Synkor do ( Sialemenks; | Jeatite (condition): | semicolon: junle: “he do-while leep Souid | denentnate tath-Gemi color | . | jphe fon loop 1S anolhen enlny-conlsolled oop: [Phe genengh foam of fon hoop fo: % [Phe genera fn ae ce wet th 7 [fat witzaln 3 teat-condikion » facremedpecaement) { body of the loop: "WORKING: — the execulton of the fen loop %8 aS follows: \-Intklizalion oF the loop voniable % done Ainek using assignment | Glalemenls such 0S £10, count=0, j=9 ele Inikialigakion’ Paxk xeculed only. once: : 2: The teeh-condilion %6 any nelkional expaession such 08 ‘cio that delenmined | tahen to @xtk Sag the Loop: The fn oop Corktrues to eveule a6 Jong 05 the test Condition & nue when the condikon becomes false the. le $5 leamtnaled and the executor conkinues with the statement often the i Loop 3. Idhen the body of the Loop '5 executed, the coniol % tnansfensed back to the fon stalemeck- The loop variable *5 updaled and once ogatn the Lest condition '6. cnet TF the Condikon %5 Ine, the body of the oop & Oggin enecubad.This process continyes YL tre condikon becomes false - 1 Ov OY Be nhs OVE Re prinlf(" 0") 1), } LO 1H Prognam—o—di at 10 qumbens fn descending onder */ be) output: Ep nu Gs Oo fk fy Chagenty fon(? 10; f>zi5 f--) prin" 4d), f); os gechur; 4 Explanalion:- he value oF f & Giiiglited 40 10 ahen prod fon Joop execution Slaalg - > new, the test Condition fozt %5 evoluaked - Snibially the condor, 5 nue sinee f210, tor2t $5 edlished: So, the Joop wid © “15 Cipon executing he patntf Galement Compiléa sends Conlnal back fo thé fn Jaap Ghene t value B deeremenled ty 1: Aflen decnomenting fhe { value, the test cordiifon § once’ again checked: i condition & ' is es conkinues Hit § value bewmes i tue, Ye rds te eveculed Ws Pro onkinues Hi 0 \ - |Beample. progr I 7x raga 's dopey a Me OM ruben Gow 1 10 97 | meine) Oui: 9, , 4 {wt 4 8 fon (fae {ecto fafe2) {calf Bd: | | JumPs IN LOOPS i boop performs a seh oF operdkions nepedtedly unk Some condikon } becomes false: DSomekimes 1} 16. necessary to SKIP a post oF the Joop om edt the teop | @3 Soon aS a ceslain condition occunS a Fox exomple, consisen ypu cont fo seaxch fon a poakiculon nome ' Q List of 100 nameS- he Searching process should, be tenmind! i | 05 S00n as the desined nome ig found: C supponls the following jump dolemenls: i | |: break i 8: Conkinue “ | 3: goto ‘le bseale | Ra DThe bwak slolement 15 used to # tenminale tre Joop" > ldhen a break ghalement i encountered tnside 0 Leop, Yeaminaled ond the progaomn execukion Conkinues wi the Loop- i.e. the beck siolemeat skips from the Loop Dre bnesle stolement % used with Condikonal tf gotement fon( ttsicztos tea) the Joop 16 fmmedntely | the Stolement often tn-cdigh St 35 defined But cohite (condi lion) do $E(condion) r— bneaky, ; wy TLS g 7 f cf Yeorie (condi Hon), 4 _ Ly----- oo Pon X Due can also use break Slalements imsige nesKeo ton 1oopy then -boneok statemed. % used tm nested Loop, the Seek would —|yernatate ay treme —toop—trr—tahich tie dened _ SE fonl----- ») cake (condition) jo { cahile (condikon) ot to. | *F (condi Kon) $£(conditon) & breaks ‘oaeok! _—— “4 Se ybnqple Peegnams!- Oluatie d’prognar. Yo, exit oop when you encounlen 5° anoint) { fat oe foal *=15 (cto; f+) 3 . { i \ if(fz<8) ‘breaks | prtntf"Ad 1); pooey ; — Sin the above progrdm the loop will be jenminated 8 Goon 05, 5 becomes 5 © nee int fe, fonts 3) (oe pnialf( "wn Heo"): ifc?zzwo) loneak, Gas | | getche): 1 > Jn the bove exegram the loop with be enmieaied 0 goo 08 Fale becomes 10" ‘B-Conknye glatement:- RRA Rn the conknue statement iS opposite 4o the break. glolement ‘ She Cockinue Solement 6 used 40 Conknue «ath the néxh tHenabion” ; i ‘ ‘care 0 Joop, tk Fells the ‘come'sen jdhen, conkinue sholement 1S used inside P, i | sips the fling gllomentsOnd_ colin cot the n8 Vv foncizt 2102 Fs) 44(condiion) os ae te, con algo use gio ! chile condition) tF(€nnon) goto atop: $F (condition) er Golo abe from sod = x Sangean es F TAR PROGKAY @ ee ve_can jump out OF @ frogrdm by using the library funcon eit: - ea *(condtHon) yee euitto): & seit AS, he end) funckon takes an integer value 0 is cagument Noamally 0 -» inditales noamol teaminokion non Zeno -> indicoles Jenminglion due to Some e707 ° & tesdenfie | | \ | I ote use of -extt) function senuines the * Yechusion | | | | ‘Additional Fealunes oF fon loop: - Imphe fn loop hos Severo! Gdeikonal fealunes. “Le Mone thao one Vaniable Con be fnilalized ota Hme in the for galement, BG fon t210,J0 tetos fee) f +->-Rexe the tnikolizalon section has two posls Pio and jzo which ony Qx€ Sepenaied by Comma B- L2ke fn the inthdiantion declton, the Tncxemenl|Becxement Geckon gy Qls0 hove mone thon one panb Be fin(fsw, fois faos f--Jj+¥) | { J 3: Further more, the test Conditfon may have any Compo BB On(Peio, Jats Prt Gb jestos fen Jat) und nelolton nee painlf(“in sd \E Ads 45) Example progaam’- oo 7 Pegoarm To duplay tubers fom | to 10 fr osvending kdkeconding onord/ ~ maint) uteur: t int $j 10 Clageaty: fon(fer foo: fHere both Saihaligalfon ond incxement- {7 ve Seclions ane omitted fn the (ex Loop~ paint" n hd; ° han — | ndes even SF the Geclons axe vot present, he Semi sepeng = the gecltong must nemdlin: 5+ Tnfirike Loops: SIE the 'e9 Conditon %3 vot present. the fon Loop fonms on ike 1 oop: . | xs fm teng 5 fer) > Here tere % vo eat condileor Jt | ends 40 an ‘nfiile 0P* j i [2a fex(5 3) fx Inftetle lop 1 = a? nin &, | - nate loop &/ g > fea(az0; a=10;) on | | prialf("d?, 1): 4 | This % also infinike Leop, since % néiten Sncxeased on decneased. | a | Sohe condition %5 clurys ue iN NesrED fon LeoPS- ID We con algo use Lop aithin Loos- ly Nesled fon La0pS means, one fan slatemeat within anothen fon slatemen}.- I} i | | Be fon (fz13 %&=103 t+) —— icbthe numben of Senators fm this type OF lps will be equal to the umben of Slesakiong tn he culen Imp mulkiplied by number oF exakiong fn the fanen fon Loop 2) Jn the abwe example, the culen teop will be exreculed 10 times and the fnnen loop ail evecule 5 limes Total nueben OF ‘lenakions = 10x65 = 50 mes => the neskag may cosknue upto any clesinad Level anne pase = > jskle a program to disploy alt the patime numbers belween 1 lo .20. r2ain() Gok %,5, count; Clagcatys fon (i232 t<1005 Pa) { Count =02, fon §215 jazts jxa) t Orr e { TECIn 20) Counts? J f( countz=2) t printf thd" i): 3 i 3 getchcy: = Phognam to dieploy tke flloting patienn- * maint) ze xX toe tin EK aera printf (° Falen 0 values)? gcanfl" &n)} fon (82 13 220 BY) { | : fon Seasjetrdea) ( ootate ('8"D omiatfC'W0): 4 Gd. ® IF UNIT-h py @ I ARRAYS | Aen: An annay %S a collection of homogeneous elemenls which hanes the common name (oR) An Qxnay £5 a collection of Similan dota tlems- [PAL the elements %m the annoy Should be Same dato type- £ - |POadinaay Vaniable can stone only one volue at a Hme-these variables can be Useful to-hondle Small amounts of data: [> Jn many application, we need to handle lange ameunts of data Fon example Considen we need to Stone manks oF 60 Studenks fn a clnss: using Ordinay variables we have to declare 60 variables to Stone manks of 60 studenls ne vo. hold only fi 3.mh, --------=-7™MbO: —» Because one vaniable Can Tne ima, ma, mma, mb Saat saes TMS Incseases paogaam code-To avotd thts fk % betten ko use Grnay Sy using Gans we Can Stone monks of 60 Students fn a class undena [Single variable name | Lu ink monks{éa); ; whene ranks % aN annay which stones Maks OF 60 Sludents RARRLE here argys can be used: 1-To Stone List.oF employees in Gn onganizakion 2: List oF Lempanoluxes xecondéd every houn in @ cay: 3+ List of pnoducls 4 To Syne manks of Students fn a class. 5+ List. of customenS and theta phone numbens Arrays can be used to nepneseat a ist oF numbens om List of nan ARRAY SUBSCRIPT: % Subscript of Gn annoy % an tntegea expression cos) Snlegen constant Con) integer janiahte thot xefens to the fndividual elements. tn the Qnnay - AL: ink Salanyltools . atecapt a — The obove axroy Stones salaries of 100 Employees tn an ongasizokion - > We can access the individual salaries by wriking index ©» subScarpt in bnockets Qflen the anany name: © -salony [5] > nepiesenls the caloxy of sth employee sadony [20 aepresents the salary of aolt employee : Ly Subscaipl | CLASSIFICATION OF ARRAS: lone. cur use angys to nepnesent sk of volues “and table of dola in two, three |o7 mone dimensions » |DGenerally axnoys ane classified based on the dimensionality - aa ie Dimensionclity % delenmined by the number of subscatpls paesent In an ans [> Arsoys ove classified flo: 4. one gimensional amoys : Two dimensional. 27045 | 3. mulli-dimensional A706 ° lou the Gamay Contains one Subscaipt, then sk t caned one-dimensional. axnays- | JF the aanay contains two Subscripts, ik 15 called two-dimensional annoy * | Mulki-dimengional GandyS ConkoinS more thon two Subscatpts- LONE - DIMENSIONAL ARRIYS: ARRRYS:~ _=done-dimensicinal Ganays have only one Si 'hD. lisk-of tems. .con—be givea one ‘vaniab! tlems con be nepaesented by 0 Single variable name using only Ont ubscaipt- obey only-one -cubscaipt. and-sut A Lick OF Subscaipt and Such voniobles #5 called one-dimensional anaay Con) ingle Subs vaniable | Deceapntion oF oye-unenstonn A8GAIS | DEC LARA [2 Aaangs mash be declased befone they are used tn the progam: ype “Ganay-name (sited; | Whene. Datatype —rspecifies type of elements thot Con be Stored facie the Qn» Such aS int, flsot on chan Qnacy-name > %5 a valid tdenlifien Size > Gize Indicates the maximum number of e1ements that Ca: Stoned inside the Garay n be. ample: Jf we jwant to Represent a sek oF 2 integer numbe® by an array i variable tg', then we Con declane the dandy 0S follows: ! int a6; Now:the computen neseaves’ five stinoge locations as shown beloto: [vim Muueo we me danay elements Can be assigned AS follow’: | alo] =10 atj 20 a(2] =30 afd 240 atid =50 >The elements axe stored in Conbinuous memony locations. ao} _ af) af} ats) alg 7600 1002 1004 1006 100 & fot numbenfiol; Float height is}: Chan nametia]; value. the size and type oF the axnay v. ‘nk atod; > Tokal memory Space quocated = 2#IC lobytes Sizer Le no-of elements & chan nametio]; 2 bao > Total memory space allocated 5 skew =10 bytes XL Float height 50]; > Total memoay Space alicaled = 4 x50 = 200 bytes INITIALIZATION OF ONE-DIMENSIONAL ARAYS:- =>DAflen Gn Gamay % declared, 11S elements must be alized - > An xaay Can be initialized at either of the following Stages: J+ Ak Compile Hime B+ Ak stun Lime |The Size oF the Ganay Should be unBigned Talegen constant 01 Q Symbolic Constant . NOTE: Size Should not be emply, tt Should not be regative value ond foating- pork D> then we declaxe on Qaaay, the Compiler allocates memory Space based on epbes ToloL memory Space allocated to Gn Garay = numberof datatypes occupied by | the dabatype 4 Size of the annay. Assigning volues bo the annoy elements % known’ 05 tntkializakion OF aaxqys | sCompile Hime bime irikalizotion® e can “lize aanay elemen's ok the ploce OF thein Gectoxation ftselF - i> Wie Can 4 Gynt [ Datelape oraay-nomels 22) (1s oF values eapencted by commas} 3] Examples Sk w(69 = {1 21% 14, 38h; chon Pasa (ey eta By | Accessing aanay ebemgets a cane dividual 1 eiements of an Grady Con be accessed by wating finder o7 e indivi f i Subscatph fn brackets after Me Gnas rome Ee fink oc[6] ={10, 100, 20, 30, 40, 50} The Gubscaipt on index nangeS from 9 to Site-J- te x{oJ clo {J = 100 4 afgy=20 : ofa) 230 i anti = 40 2{5] +50 examples: Fok numbeal53 Lud Sye 1 Chan c{4d={ ‘AL By'c) Dy; float height (3) = 51,44, 6 3Y; tk aay, the the list oF elements specified ane Aess than the Size of the Gnaay, ue ol assign ony elements Oe assy initialized Remaining elements one 9 Gasbage values) 2eve Bu fk piste fr ay . = a 5 Plo} =1, PLiye2, Pi21=3, PEr=garbagewatat, Plu} sganbage-vore |DDuning compile time. | thak alization, °F we. dovt specify the size of the Gaaay,. ht | the Compiles ues ‘he Size OF the anay beced on namben OF elements fei the | £8 fk XLI2 {45,44}; | Since the anny x hos A values fo the Uist, the: size of aaaay fs A [=D Any access to the array elements culside its Boundanies(ie. oulside ils Aimils, | Would mo eseess Cause any exnon- Tt nesulls in unpnediclable esulks - aa Ink xls] = (10, 20, 30, Ho, 504 Suppose if We by fo access <£6] element, e coin no Show Gry enrior Ly tere ixe.ane accessing beyond the Aimtt Pr C Performs, .no. bounds: checlu'ng- So You Should engune thak-the Gnaay- indi ne tihtn the declaned Ai ban ansay con be inj Joage annays - => We can use Sconfir function to “Oy ikialized ab nun lime- thts Oppacach §5 used fon intlralizing alize Qn Qanay ak nunkime & fot clays > SconF "sd 4d ng" Gxl0I, &x619, &xt23); Cor) fon(iz0; <3; fs) Scanf ("4.97 Satis): * fon(i=0; icsizes fax) Displaying: fonlizo; fesize; t+) Scanfl" 4a, Bath); 1 inte" yd atta); | 3 3 Exaepe expen |> notte Program to mead ond display aanay of 5 integers. Read Gnxoy elements at Compile time #include void mafnt y © pao ots3s tat ts Pairif(" Enten 5 Real numbens; fontizo; f¢5: P++) scanfc" 6", BOLD); + cc vn pe elements anes’): j Example Beample pegens Liste @ progaam to Gigplay arnay elements fn sevens: onden - | | #include oukpus | ##tnckude ccontorhy a elements: | Wold maint * galen the array elements: I 1p 20 30 ko 50 | tat als pnsay elements tr neverce onde | patnif("enten the anndy elerents:"); 50 yo 30 20 10 | fox 53 x4) i 4 + pact | scanf(" 4d, gotta) i od i painif(" Aanay elements fn reverse ondea:")> i fen f=h 2) x cioplay elements faor Sie-% toe te fuom h tO1) Oc ainte(h te t-d3 OCT): (5-3) dimensional onaay and prints fg index Gnd \ pagaam to implement cre Ll tng “alae . ao sincuude ngul: enker or70y element8:13 '2 ; void maint? culpils Index vole ae “0 tat Gf5)) ; ° printf" enten annay elements"): 5 pe | font t=02i<5: f+) 5 i wa 4 9 ; © conf" dt LOUD ; 3 ; | pain(" Tades VE value’): fon liso; 1¢5; f+) L : paintf ("in ‘hd te 40° $0087); 5 ofa 29 volue i 3 { 6 index | getcho: Void maint) =>histle @ progaam to calcu: fot tS), Sum-o; Prinlf(" Enken Ganay fonl fz03¢5; f+) Sean" Ad" datiy): Foni=03 1¢5;f44) elements:"); Sum = Sum+a(iy) Prinlf("Sum oF annay elements fs : #include Void maint) 5 fink manks[50], 5sum =0; Float avenage; " painlf("Enten 50 Students manxs ); fon(tzo; £50; £44) Scanfl"*d), &mankstiy: } “Fon(i=0; 4<50! 4+) Sum = Sum monks li] avenage = Sum] 50? pntnlf" Avenoge rants 152746" avout) Splinite Q progaam to calculate Sum ‘of aanay elements include lange =aL01; font t= ' 5 4 of cacis7 4079 e) hoage =O; Pe catia< small) i sma =alth | 3 painif('yn Smattest cement in the canay toed) Small); * pnietf("in Loagest element tn the canay '5:-Ad" Longe); getchey J Se | tute practice the follovoing ‘programs: iW vanite G@ Program to Calculate the Sum of Floating point numbend e untte 0. progam to display only -ve elements of an Gnay - 6 Luntke G program bo display only even numbers tn an annay . @ yintle a progam to caltiuate sum of positive numbers And sum of negaive UF ta an Gnany” \. (B tasite @ program to add too tntegen OanayS~ @ uintle prognam te mead “pnd display’ a chanacken Ganay- =Plisike a prognam to Gost the annay elements in ascending onde #Hinclude< stdio+h> Void maint > { ink afio2,n, f temp, j Clascacy, Prinlf ("In enten numben oF Onaay elementss"); Scanf("%d" &n); Painlf("in Enken annay elements"); fonlizo; fen; fr4) f scanf("Ad" &acty); 3 fon (fz0; fens f+4) fon( S=b+1 fen: fer) FC ati3>a(j3) temp =att: Athy = atit tid =temp* 3 y 3 Painlf("m Annay elements #n ascending cadensin"): foa(izosicn: fet) - t painkf("\t #0" OCI): 3 / gekcha; ‘npuk Enlen the numben of Qanay elemenls 35 Enter anray elemento: 4 1 6 5 3 cutpub’ Ansay elements in ascending onden:1 3 4 5 6 [aygafle a pacgeicm bo Soak the cay elements tn descending ordes i | Logic fonlizo:i¢n; Pee) 7 fon( S=Prusjenfey eee | tF(atiI ¢ aCj]) temp=atiy: atid =atia Ci] Lemp: a 3 Dalle @ pregsam to find wh smattest element in Gn Qanay Lagic: + Recd Grey elemenks 2-Annange the Grey elemeals fn ascending onden 3 Pain ch element of sented anaay Fs ae - w pone dimensional Osays Can Stone List of values ws |> Thene Ane situations, cohené we work to stone O fable of values Two-dimensional, Grnays axe used to represent on Stone table Of values. Two-dimensional gazay: An Qxmay hich Contains two Subscripts % knocon aS Hoo-dimenstonal Gancy Example: mabnis. Ls mabnix Contains two dimensions te. Rows & Columns - Colwmn cghume 6 a Roo} y 2 wor] 3 y Th matnix, pankiculan en 18 nepnesented by two Subscripts. Fay example Ag refens to the value fh the Th pry and j* column tn mabnie A Aoo <2, Aov=2, Arg=3, Aayz4 ee € language matnix can be nepnesented by two-dimensional Ganays Di ‘ION OF TwO- 8. ARRAYS: [PESLARATION OF Tho DIMENSIONAL ARRAYS: SD Two-dimensional Gandys Gre declaned 05 follows:- | Suge [datatype amay-name [aasie) (colin EX nk atay3); 7 Contains 3 ows and 3 Columns fot b{31Z1; > Contains 3 rows ord 2 came ume! chan CL5I[41)—> contains 5 n0ws ond & Col ; gimensional anaoy £5 = Rowsize & ColumnsiZe by the of elements. => Tolal numben of elements in a two- sed ce “occupied by the two-dimensional Garay = numben OF bytes.occsr re oe datolype & tokal number Exomple: ink atsIu}y total numben of elementS= SH =20 Space = 220 =o bytes: > Each dimension of the annay % frdeved fiom o to tts morimum.size -) > The finsk index selects jhe not Ord Second Mndex selects Column within thal noo zl 1- Compile iene ApInitiali zing two-dimensional Qandys ok the Kime of declanation Tkeelf EL tok Eobiel2I13)= (0,00, 4014" The above Statement infhializes frst now. to 0 and Seco jon 5 done now by now- ie. { oo il vad Lpue can also inikiolize two-dimensional Gnnay in the fonm Of motsrie 05 shown bel | fet Yobleta3Es} = { {o,0,04, fas: fre of3929 ={ fs, (174, (BI) ; ls then the Gamay 15 Completely ‘witialized oth alt values, then no need bo specity size of the finst dimension Ex tnt at 33d 2§ (45,64, (10% 33);

Teoo: @adingr fon(izo; tc nowsizeter) - font jeoyjecolurnsizesfey ) Sank (Ad) LOLiaEsg); J J Example prognam- FH include include > linite a program bo nead and display 3x3 mabniv- Tnpub- pnlen @ 3x3 makin: | 3 02 3 Output: The entered matnix fs, bas 78 q lo 2 3 => example present => [site ¢ pmogran to penfonm addition of tao rraknices #include Void maint) s ink ago, btinOns, HALL, mL mRcaref R Clascaly ° a printf" Enken number Of rows & columns in motnin a: scanf("sd td", &23, &C2); painlf("In Enten mabsire a2"); fon(fzo; tena; f+) fon(J20:d Void maine) = Prinif("in Resultant matnix f5s\n"); fon(izos Sen: f+) Ink aQo3fo], bboltior,| COogC101, us k ink v3, no, c1, ca; clascncy; Printf("Enlen no-of noas & Columns tn a: ScanF("d 4g? &m9, &¢1); 3 PatalfC"in Enken mokatna: " PrinHUn' fon Cizos tong; fr) J J fon (03 4e4; fey PaintF(" Aud" Cot tin: fon(j20; cca; $44) ese Scanfl" 4g’ LOCI) | PSB "nals tadhcokion fo wb vost i 3 3 | Prinlf(“In Enlem noof news & Columns fo bi), Gelchor, , © ScanF( "+d 40° done, &c2): 4 ; : Priel Cin enten mabnie bs) fon (fo; temo fae) { fon( §=055¢¢2; $+3) ae Scanf("Ad" &bUI 33): 3 ae TFC c1==59) _ Prinlf(" mobnix maulkioli cation &> possible’) © for (fosteny sha) | Fon j-0: fec21 fray a CLiIti =o; SON Fong FO3K hive G prcgram bo genemte tnanspbe of #include | Void maine) \ 4 fink (39 (3, BLAIS], bf Clascacy Pnintf("Enten 0 eabrix:”): fon(izosiz3;fa4) Fon(§=0; <3: fa) 3 Sconh" 4d" Social SD» fonti= rhea) foa(JzoJ Symmebnic jagnol elementS a mataix. sh enter a mabrin: 1 2 3 5 6 8q outpul: “Transpase OF given motnicts: \ 2 3 ut 58 64 Yon of Lao matnices elements fr a matni~ and fis gum mo mala. pits atyiad a{s0139 wen motsiz symmetric con nok i Evample @! wn | MULIZ-DIMENSTIONAL _AKKAYD: @ 5 =P An Qnaoy (hich Conlains mone than twe gubscaiptS %5 known as mulki-dimension Onnay. > Mulki-dimensional Gnrays ane used to aepnesenl- Sunvey data - Declanation:- A Multi-dimensional Qanoys ané declaned 05 follows : Glee datatype annoy-name (54) (52](53) ------ [Sei]: >hene 5; {5 the size of ith dimension « Romples fob cunvey(si(sI2]: Hook table (5I(uI(3I(HI; Hexe Suavey $5 a 3. dimensional anady, which Contains 180 fntegen elemeniS ard Fable ts a 4-dimensionol Qanay- j - ; Exam - ) ; [Tp stepnecent 0 sunvey data OF mainfall during the last 3 yeans in 5 cibres from [e to decemben, the Garay Can be Geckaned 0S FodlocwS: ink sunvey(33 (5102: year city month Yeon Rah TES yan TT SE yy ny TFT tee city i ' 2 2 : Bs 3 3 r 3 4 e a 5 “fon eromple, the" element sunvey(23(3300] nepnesents the nainfalt in the ‘|month’ of ockoben duaing the second yean fm city 3- To nepnesent gold rakes duning the Lost 2 years fan from Jonuaay to december every day, the annay con be declared G5, follows: ink qold-sotet 2 (121 (331; 1 Compile lime aon anit dimensional Gmaays Can be aa ab Bice Hime | Eu tnt o(33(30[3 = {8 23, Gi & £3) | pina i (aay, fi, esi { (42.84, {05.0}, {aeayh { | eer pen us 2-Run-Hme Anitiolizabion- NN mulli- dimensional aaaays can be snghiaized at mun Hime ‘using Scarf funclion Ex! ink oc ( 332315) fon( P=0 1¢32 44) fon( Saa5<22 fra) _ fon( k=O K<5! ka+) { ! scanft'¥d) &xci30490KI): j 3 3

You might also like