PF Lecture 9.ppt hakakbshisokwb jaiksnsjomabhj

abdullahumtbsme 5 views 25 slides Jun 30, 2024
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

Jjagisnshsj sjisnscsusihssvjskozmdbdhs sjsjsuj


Slide Content

Programming Fundamentals
Lecture 9

2
Objectives
Inthischapter,youwill:
•Learnaboutarrays
•Explorehowtodeclareandmanipulatedata
intoarrays
•Understandthemeaningof“arrayindexout
ofbounds”
•Discoverhowtopassanarrayasa
parametertoafunction

3
Introduction
•Beforeformallydefininganarray,letus
considerthefollowingproblem.
•WewanttowriteaC++programthatreads
fivenumbers,findstheirsum,andprintsthe
numbersinreverseorder.

4
Introduction (continued)

5
Introduction (continued)
•Thisprogramworksfine.However,ifyou
needtoread100(ormore)numbersandprint
theminreverseorder,youwouldhaveto
declare100variablesandwritemanycin
andcoutstatements.Thus,forlarge
amountsofdata,thistypeofprogramisnot
desirable.
•Thedatastructurethatletsyoudoallofthese
thingsinC++iscalledanarray.

6
Arrays
•Array:isacollectionoffixednumberof
componentsallofthesamedatatype
•Aone-dimensionalarrayisanarrayinwhich
thecomponentsarearrangedinalistform
•Syntaxfordeclaringaone-dimensionalarray:
intExpisapositiveintegerandspecifiesthe
numberofcomponentsinthearray.

7
Arrays (continued)

8
Arrays (continued)

9
Accessing Array Components
•Generalsyntax:
WhereindexExp,calledanindex
•Indexvaluespecifiesthepositionofthe
componentinthearray
•[]isthearraysubscriptingoperator
•Thearrayindexalwaysstartsat0

10
Accessing Array Components
(continued)
•Considerthefollowingstatement:
intlist[10];
•Thisstatementdeclaresanarrayof10
components.
•Inotherwords,wehavedeclared10
variables

11
Accessing Array Components
(continued)
•Theassignmentstatement:
list[5]=34;
stores34inlist[5],whichisthesixth
componentofthearray
•Supposeiisanintvariable.Then,
inti=3;
list[i]=63;

12
Accessing Array Components
(continued)
•Next, consider the following statements:
list[3] = 10;
list[6] = 35;
list[5] = list[3] + list[6];

13
Accessing Array Components
(continued)
Note: When you declare an array, its size must be known.

14
Processing One-Dimensional
Arrays
•Somebasicoperationsperformedonaone-
dimensionalarrayare:
−Initializing
−Inputtingdata
−Outputtingdatastoredinanarray
−findingthesumandaverageoftheelements
ofthearray
−Findingthelargestand/orsmallestelement
•Easilyaccomplishedbyaloop

15
Processing One-Dimensional
Arrays (continued)
•Considerthedeclaration
intlist[100];//arrayofsize100
•Usingforlooptoaccessarrayelements:
for(inti=0;i<100;i++)//Line1
cin>>list[i]; //Line2
•Thisstatementsread100numbersfromthe
keyboardandstorethenumbersinlist
•Similarly,youcanoutputthedatastoredin
thearray

16
Practical Example
•Takeanarrayof5elementsandimplement
thefollowingoperations:
−Inputtingdata
−Outputtingdata
−Outputtingdatainreverseorder
−Findthesumofarrayelements
−Findtheaverageofarrayelements
−Findthelargestnumberinthearray
−Findthesmallestnumberinthearray

17
Array Index Out of Bounds
•Ifwehavethestatements:
doublenum[10];
•Thecomponentnum[i]isvalidifi=0,1,
2,3,4,5,6,7,8,or9
•Otherwise,wesaytheindexisoutof
bounds
•InC++,thereisnoguardagainstindicesthat
areoutofbounds
•Itistheprogrammer’sresponsibilitytomake
surethattheindexiswithinbounds.

18
Array Initialization During
Declaration
•Likeanyothersimplevariable,anarraycan
beinitializedwhileitisbeingdeclared.
•Example:
doublesales[]={12.25,32.50,16.90,23.85,
45.68};
•Inthiscase,itisnotnecessarytospecifythe
sizeofthearray
•Sizedeterminedbythenumberofvaluesin
thebraces

19
Partial Initialization of Arrays
During Declaration
•Thestatement:
intlist[10]={0};
declareslisttobeanarrayof10
componentsandinitializesallofthemtozero
•Thestatement:
intlist[10]={8,5,12};
declareslisttobeanarrayof10
components,initializeslist[0]to8,
list[1]to5,list[2]to12andallother
componentsareinitializedto0

20
Partial Initialization of Arrays
During Declaration (continued)
•Thestatement:
intlist[]={5,6,3};
declareslisttobeanarrayof3components
andinitializeslist[0]to5,list[1]to6,and
list[2]to3
•Thestatement:
intlist[25]={4,7};
declaresanarrayof25components;
initializeslist[0]to4andlist[1]to7;all
othercomponentsareinitializedto0

21
Some Restrictions on Array
Processing
•Considerthefollowingstatements:
•C++doesnotallowaggregateoperationson
anarray:
•Solution:

22
Some Restrictions on Array
Processing (continued)
•Thefollowingisillegaltoo:
•Solution:

23
Address of an Array in Memory

24
Searching an Array for a
Specific Item
•Practical Example?
Sample Run
Enter 10 integers:
2 56 34 25 73 46 89 10 5 16
Enter the number to be searched: 25
25 is found at position 3

Reading
Chapter 9:
Arrays and Strings
Tags