chap09 - Chapter Vectors ana Arrays CHAPTER GOALS To become...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter Vectors ana Arrays CHAPTER GOALS • To become familiar with using vectors to collect objects • To be able to access vector elements and resize vectors • To be able to pass vectors to functions • To learn about common array algorithms • To learn how to use one-dimensional and two-dimensional arrays :nany programs, you need to col- .: ll1ultiple objects ofthe same type. <.lI1dard C++, the vector construct ,\\.~ you to conveniently manage :;:ctions that automatically grow to ., desired size. In this chapter, you ': learn about vectors and common ..:,)r algorithms. The standard vectors are built on top of the lower-level array construct. The last part of this chapter shows you how to work with arrays. Two-dimensional arrays are useful for representing tabular arrangements of data.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CHAPTER 9 CHAPTER CONTENTS 9.1 Using Vectors to Collect Data Items :1:!8 9.4 Parallel Vectors 345 Syntax 9: - V~<:t(f Vdr"!(.Jt.llf: - ,-':1' -'/~i'l ',-"',,. r,J::IKO:::- ~, Syf)[<.JX ~-~ ejl ~}~II),','" ~no 9.2 Vector Subscripts :::w 9.5 Arrays :\01.> " '-"I . I J.'" lJ«)'.IC]CIC· 3:14 j5fi QUdll:y --;';; , 1-'1' , ! Rc:1I )Lh")l~l r, ':.-, 1 ~: H ' .. ;l~ i t\· It·- 9.3 Vector Parameters and Return Values AdVcH il_:~_-'\. ~-()f JI( l":~ I Suppose you write a program that reads a list of salary tlgures and prints out the' marking the highest value, like this: 32000 54000 67500 29000 35000 80000 highest value => 115000 44500 100000 65000 In order to know which value to mark as the highest, the program must tlrst read a1: them. After all, the last value might be the highest one. If you know that there are ten inputs, then you can store the data in ten vari'lr:. salaryl, sal ary2, sal ary3, ..., salary10. Such a sequence ofvariables is not very pr.:, SvnLl'\ 9.1 : \'CCfor \'ariab1c I kl111ition ve eto r«l'pc_lltlllle>variablc_lIamt!; vee to r«J'Pc_namc> ·vtlriablc_llame(initiaLsizt!) ; 1':"ll1lf,lc: veetor<i nt> scores; veetor<Employee> staff(20); I'll J!""L': Define a new variable ofvector type, and optionally supply an initial size.
Background image of page 2
,f 'S 9.1 Using Vecwrs tll Collect Data Items salaries [OJ I I \~ [1J [2J [3J I 35000 I [4J 10 ~-1 [5J [6J [7J [8J I j [9J Figure 1 Figure 2 lector of salari es Vector Slot Filled with daub1e Value tical to usc. You would have to write quite a bit of code ten times, once for each of the variables. There might also be a hundred employees on the staff In C++ there is a bettcr way of implementing a sequence of data items: the vector construct. A 'vector is a collection of data items of the same type. Every element of the collection can be accessed separately. Here we define a vector of ten employee salaries: vector<double> salaries(10); This is the definition of a variable salaries whose type is vector<double>. That is, salaries stores a sequence ofdouble values. ll1e (10) indicates that the vector holds ten values. (See Figurc 1.) In general, vector variables are defined as in Syntax 9.1. To get some data into sa1ari es, you must specity which slot in the vector you want to use. That is done with the [J operator: salaries[4J = 35000; Now the slot with index 4 ofsa1ari es is fuled with 35000. (See Figure 2).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 47

chap09 - Chapter Vectors ana Arrays CHAPTER GOALS To become...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online