Ch07 Arrays and Strings.ppt

28
Introduction to Programming Engr. Rashid Farid Chishti [email protected] Chapter 07: Arrays and Strings International Islamic University H-10, Islamabad, Pakistan http://www.iiu.edu.pk

Transcript of Ch07 Arrays and Strings.ppt

Introduction to ProgrammingEngr. Rashid Farid [email protected] 07: Arrays and StringsInternational Islamic University H-10, Islamabad, Pakistanhttp://www.ii.ed.pkArraysArraysIn everyday life we commonly group similarIn everyday life we commonly group similar objects into units. We buy eggs by the carton. objects into units. We buy eggs by the carton.In computer languages we also need to groupIn computer languages we also need to group together data items of the same type. The mosttogether data items of the same type. The most basic mechanism that accomplishes this in C++basic mechanism that accomplishes this in C++ is the array. is the array.Arrays can hold a few data items or tens ofArrays can hold a few data items or tens of thousands. The data items grouped in an arraythousands. The data items grouped in an array can be simple types such as int or float, orcan be simple types such as int or float, or they can be userdefined types such asthey can be userdefined types such as structures and objects. structures and objects.An array groups items of the same type. TheAn array groups items of the same type. The items in a in an array are accessed by anitems in a in an array are accessed by an inde! number. "sing an inde! number to specifyinde! number. "sing an inde! number to specify an item allows easy access to a large numberan item allows easy access to a large number of items. of items.#efining, $eading and Writing Array#efining, $eading and Writing Array%% gets four ages from user, displays them&include 'iostream(&include 'stdlib.h(using namespace std)int main*+,int age-./, j) %%array 0age0 of . intsfor*j12) j'.) j+++, %%get . agescout '' 34nter an age5 3)cin (( age-j/) %%access array element6for*j12) j'.) j+++, %%display . agescout '' 3age-3 '' j '' 3/ 1 3 '' age-j/ '' endl)cout ''3Address 3 '' 7age-j/ '' 3 1 3 '' age-j/ '' endl)6system*38A"943+) return 2)6Calculating AverageCalculating Average&include 'iostream(&include 'stdlib.h(using namespace std)int main*+,double avg, sum 1 2 ) int i ) int mar:s-;2/ ) %< array declaration n4nter a string5 3)%% cin.get*+ means a member function get*+ of the stream%% class of which cin is an objectcin.get*str, NAS+)%% put string in str%% first argument to get*+ is the array address where the%% string being input will be placed.%% The second argument specifies the ma!imum siBe of the%% arraycout '' 3Rou entered5 3 '' str '' endl)system*38A"943+) return 2) 6$eading Nultiple Tines$eading Nultiple Tines%% linesin.cpp %% reads multiple lines, terminates on 0U0 character&include 'iostream(&include 'stdlib.h(using namespace std)const int NAS 1 C222) %% ma! characters in stringchar str-NAS/) %%string variable strint main*+,cout '' 3>n4nter a multiline string5>n3)cin.get*str, NAS, 0U0+) %%terminate with Ucout '' 3Rou entered5>n3 '' str '' endl)for*int i12 ) i';2 ) i+++cout '' str-i/ '' 313 '' int*str-i/+ '' endl)%% 4nter 1 ;2, 9pace 1 JC)system*38A"943+) return 2)6Copying a 9tring the Vard WayCopying a 9tring the Vard Way%% strcopy;.cpp%% copies a string using a for loop&include 'iostream(&include 'cstring( %%for strlen*+&include 'stdlib.h(using namespace std)int main*+, %%initialiBed stringchar str;-/ 13Eh, Captain, my CaptainK 33our fearful trip is done3)const int NAS 1 D2) int j) %% NAS is siBe of strC bufferchar strC-NAS/) %%empty stringfor* j12) j'strlen*str;+) j+++ %%copy strlen charactersstrC-j/ 1 str;-j/) %% from str; to strCstrC-j/ 1 0>20) %%insert G"TT at endcout '' strC '' endl) %%display strCsystem*38A"943+) return 2)6Copying a 9tring the 4asy WayCopying a 9tring the 4asy Way%% strcopyC.cpp%% copies a string using strcpy*+ function&include 'iostream(&include 'cstring( %%for strcpy*+&include 'stdlib.h(using namespace std)int main*+,char str;-/ 1 3Tiger, tiger, burning bright>n33In the forests of the night3)const int NAS 1 D2) %%siBe of strC bufferchar strC-NAS/) %%empty stringstrcpy*strC, str;+) %%copy str; to strCcout '' strC '' endl) %%display strCsystem*38A"943+) return 2) 6Array of 9tringsArray of 9trings%% straray.cpp%% array of strings&include 'iostream(&include 'stdlib.h(using namespace std)int main*+,const int #AR9 1 A) %%number of strings in arrayconst int NAS 1 ;2) %%ma!imum siBe of each string%%An array of stringschar star-#AR9/-NAS/ 1 , 39unday3, 3Nonday3, 3Tuesday3,3Wednesday3, 3Thursday3, 3Lriday3 , 39aturday3 6)for* int j12 ) j'#AR9 ) j+++ %%display every stringcout '' star-j/ '' endl)system*38A"943+)return 2)6Array of 9tringsArray of 9tringsAssignment #61. !rite and test the "ollowin# "nction that retrns thro#h its re"erence parameters both the ma$imm and the minimm vales stored in an array: void #et%$tremes&"loat' min, "loat' ma$, "loat a(), int n*+,. !rite and test the "ollowin# "nction: doble stdev&doble $(), int n*+-he "nction retrns the standard deviationo" a data set o" n nmbers $0, ., $n/10. !rite a and test a pro#ram to calclate1eterminant and 2ltiplicative Inverse o" a 3$3 matri$, also write a pro#ram to mltiply two 3$3 matrices.Assignment #60. !rite a pro#ram that takes a strin# "rom the ser. -he pro#ram then calclates the-otal nmber o" 4haracters-otal nmber o" 5paces-otal nmber o" -abs-otal nmber o" 6ines7. !rite a pro#ram to calclate mean, median and mode o" an array8. !rite a pro#ram to convert a #iven year to its 9oman e:ivalent i.e.,Decimal Roman Decimal Roman 1 i 100 c 5 v 500 d 10x 1000 m 50le.g. 1988 = mdcccclxxxviii1525 = mdxxv