SlideShare a Scribd company logo
A First Book of C++A First Book of C++
Chapter 7 (Pt 2)Chapter 7 (Pt 2)
ArraysArrays
∗ In this chapter, you will learn about:
∗ One-Dimensional Arrays
∗ Array Initialization
∗ Arrays as Arguments
∗ Two-Dimensional Arrays
∗ Common Programming Errors
∗ Searching and Sorting Methods
A First Book of C++ 4th Edition 2
Objectives
∗ Two-dimensional array (table): consists of both
rows and columns of elements
∗ Example: two-dimensional array of integers
col[0] col[1] col[2] col[3]
row[0] 8 16 9 5
row[1] 3 15 27 6
row[2] 14 25 2 10
∗ Array declaration: names the array val and
reserves storage for it
int val[2][2];
row colA First Book of C++ 4th Edition 3
Two-Dimensional Arrays
array val with 2 cols and 2 rows
row 0
row 1
col 0 col 1
∗ Locating array elements (Figure 7.7)
∗ val[1][3] uniquely identifies element in row 1, column 3
∗ Examples using elements of val array:
// assign value in row 2, col 3 into variable price
price = val[2][3];
val[0][0] = 62;//assign 62 to row 0, col 0
newnum = 4 * (val[1][0] - 5);
sumRow = val[0][0] + val[0][1] + val[0][2] +
val[0][3];
∗ The last statement adds the elements in row 0 and sum is
stored in sumRow
A First Book of C++ 4th Edition 4
Two-Dimensional Arrays (cont'd.)
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 5
∗ Initialization: can be done within declaration
statements (similar to single-dimension arrays)
∗ Example:
int val[3][4] = { {8,16,9,52},
{3,15,27,6},
{14,25,2,10} };
∗ First set of internal braces contains values for row 0,
second set for row 1, and third set for row 2
∗ Commas in initialization braces are required; inner
braces can be omitted
A First Book of C++ 4th Edition 6
Two-Dimensional Arrays (cont'd.)
col 0 1 2 3
row
0
1
2
A First Book of C++ 4th Edition 7
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 8
Two-Dimensional Arrays (cont'd.)
∗ Processing two-dimensional arrays:
∗ nested for loops are typically used
∗ Easy to cycle through each array element
∗ A pass through outer loop corresponds to a row
∗ A pass through inner loop corresponds to a column
∗ Nested for loop in Program 7.7 used to multiply each val
element by 10 and display results
∗ Output of Program 7.7
Display of multiplied elements
80 160 90 520
30 150 270 60
140 250 20 100
A First Book of C++ 4th Edition 9
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 10
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 11
Two-Dimensional Arrays (cont'd.)
∗ Prototypes for functions that pass two-dimensional
arrays can omit the row size of the array
∗ Example (Program 7.8): optional
display (int [ ][4]);
∗ Row size is optional, but column size is required
∗ Assuming 4 bytes for an int, the element val[1][3] is
located 28 bytes from the start of the array (i.e., offset)
A First Book of C++ 4th Edition 12
Two-Dimensional Arrays (cont'd.)
∗ Determining offset of an array (val [1][3])
∗ Computer uses row index, column index, and column
size to determine offset
A First Book of C++ 4th Edition 13
Two-Dimensional Arrays (cont'd.)
3+1
Two-Dimensional Arrays (cont'd.)
A First Book of C++ 4th Edition 14
4 bytes 4 bytes 4 bytes 4 bytes
4 bytes 4 bytes 4 bytes
∗ Arrays with more than two dimensions allowed in C++ but
not commonly used
∗ Example: int response[4][10][6]
∗ First element is response[0][0][0]
∗ Last element is response[3][9][5]
∗ A three-dimensional array can be viewed as a book of data
tables (Figure 7.10)
∗ First subscript (rank) is page number of table
∗ Second subscript is row in table
∗ Third subscript is desired column
A First Book of C++ 4th Edition 15
Larger Dimensional Arrays
Larger Dimensional Arrays
(cont'd.)
A First Book of C++ 4th Edition 16
∗ Forgetting to declare an array
∗ Results in a compiler error message equivalent to “invalid
indirection” each time a subscripted variable is
encountered within a program
∗ Using a subscript that references a nonexistent array
element (exceeding declared size/index)
∗ For example, declaring array to be of size 20 (e.g, int
val[20])and using a subscript value of 25 (e.g,
val[25])
∗ Not detected by most C++ compilers and will probably
cause a runtime error
A First Book of C++ 4th Edition 17
Common Programming Errors
∗ Not using a large enough counter value in a for loop
counter to cycle through all array elements
∗ Forgetting to initialize array elements
∗ Don’t assume compiler does this.
∗ Example:
val [8] = 60; // initialize or assign value to your array element
A First Book of C++ 4th Edition 18
Common Programming Errors
(cont'd.)
∗ One-dimensional array: a data structure that stores a
list of values of same data type
∗ Must specify data type and array size
∗ Example:
int num[100]; //creates an array of 100 integers
∗ Array elements are stored in contiguous locations (in
sequence) in memory and referenced using the array
name and a subscript
∗ Example: values[4]
∗ means access the 5th
element of the
values array at index 4
A First Book of C++ 4th Edition 19
Summary
∗ Two-dimensional array is declared by listing both a
row and column size with data type and name of
array
∗ Arrays may be initialized when they are declared
∗ For two-dimensional arrays, you list the initial values, in
a row-by-row manner, within braces and separating
them with commas
∗ Arrays are passed to a function by passing name of
array as an argument (to be discussed in next class)
A First Book of C++ 4th Edition 20
Summary (cont'd.)
∗ Most programmers encounter the need to both sort
and search a list of data items at some time in their
programming careers
A First Book of C++ 4th Edition 21
Chapter Supplement: Searching
and Sorting Methods
∗ Linear (sequential) search
∗ Each item in the list is examined in the order in which it
occurs until the desired item is found or the end of the
list is reached
∗ List doesn’t have to be in sorted order to perform the
search
∗ Binary search
∗ Starting with an ordered list, the desired item is first
compared with the element in the middle of the list
∗ If item is not found, you continue the search on either
the first or second half of the list
A First Book of C++ 4th Edition 22
Search Algorithms

More Related Content

What's hot (20)

PDF
Queues-handouts
Fajar Baskoro
 
PPT
Data structure lecture7
Kumar
 
PPTX
Array ppt
Kaushal Mehta
 
PPTX
Arrays in C language
Shubham Sharma
 
PPTX
Arrays
Trupti Agrawal
 
PPTX
Stack - Data Structure - Notes
Omprakash Chauhan
 
PPTX
My lecture stack_queue_operation
Senthil Kumar
 
PPTX
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
PPT
03 stacks and_queues_using_arrays
tameemyousaf
 
PPT
Stack and queue
Katang Isip
 
PDF
stacks and queues
DurgaDeviCbit
 
PPT
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
PPT
Stacks and queue
Amit Vats
 
PPTX
C++ lecture 04
HNDE Labuduwa Galle
 
PPTX
Queue - Data Structure - Notes
Omprakash Chauhan
 
PPTX
Stacks in c++
Vineeta Garg
 
PPTX
A Presentation About Array Manipulation(Insertion & Deletion in an array)
Imdadul Himu
 
PPT
C programming , array 2020
Osama Ghandour Geris
 
PPTX
Arrays In C++
Awais Alam
 
Queues-handouts
Fajar Baskoro
 
Data structure lecture7
Kumar
 
Array ppt
Kaushal Mehta
 
Arrays in C language
Shubham Sharma
 
Stack - Data Structure - Notes
Omprakash Chauhan
 
My lecture stack_queue_operation
Senthil Kumar
 
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
 
03 stacks and_queues_using_arrays
tameemyousaf
 
Stack and queue
Katang Isip
 
stacks and queues
DurgaDeviCbit
 
Array Presentation (EngineerBaBu.com)
EngineerBabu
 
Stacks and queue
Amit Vats
 
C++ lecture 04
HNDE Labuduwa Galle
 
Queue - Data Structure - Notes
Omprakash Chauhan
 
Stacks in c++
Vineeta Garg
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
Imdadul Himu
 
C programming , array 2020
Osama Ghandour Geris
 
Arrays In C++
Awais Alam
 

Viewers also liked (13)

PDF
Daniel Witherite UT-UXO Certificate
Daniel Witherite
 
PPTX
Growth Hacking with Kentico
Brian McKeiver
 
PPTX
El informe de investigación
Carlos Cesar Robles Carrillo
 
PDF
Casa design 2014
casadesign
 
PPTX
Sistemas de Información en la Empresa
Edicion Ticnews
 
DOC
11review(inheritance andpolymorphism)
IIUM
 
PDF
Mediterranean – Adriatic Underwater Cultural Heritage links
UNESCO Venice Office
 
PDF
YALI_Certificate (2)
Shinaaz Zoutenberg
 
PDF
Apostila de empilhadeira rv1
ITAR INSTITUTO TECNOLOGICO ARQUIMEDES
 
PPTX
PRIMAVERA - A Nova Fiscalidade Nacional em Angola
PRIMAVERA Business Software Solutions
 
PPT
Oportunidad Negocio (2 )
HERBALIFE
 
PDF
Startup Braga - value proposition workshop
Startup Braga
 
PDF
Brochure Seminario 23 de mayo
Emprende Claro
 
Daniel Witherite UT-UXO Certificate
Daniel Witherite
 
Growth Hacking with Kentico
Brian McKeiver
 
El informe de investigación
Carlos Cesar Robles Carrillo
 
Casa design 2014
casadesign
 
Sistemas de Información en la Empresa
Edicion Ticnews
 
11review(inheritance andpolymorphism)
IIUM
 
Mediterranean – Adriatic Underwater Cultural Heritage links
UNESCO Venice Office
 
YALI_Certificate (2)
Shinaaz Zoutenberg
 
Apostila de empilhadeira rv1
ITAR INSTITUTO TECNOLOGICO ARQUIMEDES
 
PRIMAVERA - A Nova Fiscalidade Nacional em Angola
PRIMAVERA Business Software Solutions
 
Oportunidad Negocio (2 )
HERBALIFE
 
Startup Braga - value proposition workshop
Startup Braga
 
Brochure Seminario 23 de mayo
Emprende Claro
 
Ad

Similar to Csc1100 lecture09 ch07_pt2 (20)

PPT
Csc1100 lecture07 ch07_pt1-1
IIUM
 
PPT
Csc1100 lecture12 ch08_pt2
IIUM
 
PPT
Fp201 unit4
rohassanie
 
PPT
Chap 6 c++
Venkateswarlu Vuggam
 
PDF
Chap 6 c++
Venkateswarlu Vuggam
 
PDF
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
PPTX
Structured data type
Omkar Majukar
 
PDF
Multidimensional arrays in C++
Ilio Catallo
 
PPTX
ARRAYS.pptx
MamataAnilgod
 
PPT
Lecture#5-Arrays-oral patholohu hfFoP.ppt
SamanArshad11
 
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
PPT
19-Lec - Multidimensional Arrays.ppt
AqeelAbbas94
 
PPTX
Data structure array
MajidHamidAli
 
PPTX
Data structure.pptx
SajalFayyaz
 
PPTX
Yash Bhargava Array In programming in C
simranroy370
 
PPT
CHAPTER-5.ppt
Tekle12
 
PDF
C++ How to Program Early Objects Version 9th Edition Deitel Solutions Manual
abosinnurina
 
PDF
Arrays and library functions
Swarup Boro
 
PPT
Arrays and vectors in Data Structure.ppt
mazanali7145
 
PDF
Chapter12 array-single-dimension
Deepak Singh
 
Csc1100 lecture07 ch07_pt1-1
IIUM
 
Csc1100 lecture12 ch08_pt2
IIUM
 
Fp201 unit4
rohassanie
 
05_Arrays C plus Programming language22.pdf
bodzzaa21
 
Structured data type
Omkar Majukar
 
Multidimensional arrays in C++
Ilio Catallo
 
ARRAYS.pptx
MamataAnilgod
 
Lecture#5-Arrays-oral patholohu hfFoP.ppt
SamanArshad11
 
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
19-Lec - Multidimensional Arrays.ppt
AqeelAbbas94
 
Data structure array
MajidHamidAli
 
Data structure.pptx
SajalFayyaz
 
Yash Bhargava Array In programming in C
simranroy370
 
CHAPTER-5.ppt
Tekle12
 
C++ How to Program Early Objects Version 9th Edition Deitel Solutions Manual
abosinnurina
 
Arrays and library functions
Swarup Boro
 
Arrays and vectors in Data Structure.ppt
mazanali7145
 
Chapter12 array-single-dimension
Deepak Singh
 
Ad

More from IIUM (20)

PDF
How to use_000webhost
IIUM
 
PDF
Chapter 2
IIUM
 
PDF
Chapter 1
IIUM
 
PDF
Kreydle internship-multimedia
IIUM
 
PDF
03phpbldgblock
IIUM
 
PDF
Chap2 practice key
IIUM
 
PDF
Group p1
IIUM
 
PDF
Tutorial import n auto pilot blogspot friendly seo
IIUM
 
PDF
Visual sceneperception encycloperception-sage-oliva2009
IIUM
 
PDF
03 the htm_lforms
IIUM
 
PDF
Exercise on algo analysis answer
IIUM
 
PDF
Redo midterm
IIUM
 
PDF
Heaps
IIUM
 
PDF
Report format
IIUM
 
PDF
Edpuzzle guidelines
IIUM
 
PDF
Final Exam Paper
IIUM
 
PDF
Final Exam Paper
IIUM
 
PDF
Group assignment 1 s21516
IIUM
 
PDF
Avl tree-rotations
IIUM
 
PDF
Week12 graph
IIUM
 
How to use_000webhost
IIUM
 
Chapter 2
IIUM
 
Chapter 1
IIUM
 
Kreydle internship-multimedia
IIUM
 
03phpbldgblock
IIUM
 
Chap2 practice key
IIUM
 
Group p1
IIUM
 
Tutorial import n auto pilot blogspot friendly seo
IIUM
 
Visual sceneperception encycloperception-sage-oliva2009
IIUM
 
03 the htm_lforms
IIUM
 
Exercise on algo analysis answer
IIUM
 
Redo midterm
IIUM
 
Heaps
IIUM
 
Report format
IIUM
 
Edpuzzle guidelines
IIUM
 
Final Exam Paper
IIUM
 
Final Exam Paper
IIUM
 
Group assignment 1 s21516
IIUM
 
Avl tree-rotations
IIUM
 
Week12 graph
IIUM
 

Csc1100 lecture09 ch07_pt2

  • 1. A First Book of C++A First Book of C++ Chapter 7 (Pt 2)Chapter 7 (Pt 2) ArraysArrays
  • 2. ∗ In this chapter, you will learn about: ∗ One-Dimensional Arrays ∗ Array Initialization ∗ Arrays as Arguments ∗ Two-Dimensional Arrays ∗ Common Programming Errors ∗ Searching and Sorting Methods A First Book of C++ 4th Edition 2 Objectives
  • 3. ∗ Two-dimensional array (table): consists of both rows and columns of elements ∗ Example: two-dimensional array of integers col[0] col[1] col[2] col[3] row[0] 8 16 9 5 row[1] 3 15 27 6 row[2] 14 25 2 10 ∗ Array declaration: names the array val and reserves storage for it int val[2][2]; row colA First Book of C++ 4th Edition 3 Two-Dimensional Arrays array val with 2 cols and 2 rows row 0 row 1 col 0 col 1
  • 4. ∗ Locating array elements (Figure 7.7) ∗ val[1][3] uniquely identifies element in row 1, column 3 ∗ Examples using elements of val array: // assign value in row 2, col 3 into variable price price = val[2][3]; val[0][0] = 62;//assign 62 to row 0, col 0 newnum = 4 * (val[1][0] - 5); sumRow = val[0][0] + val[0][1] + val[0][2] + val[0][3]; ∗ The last statement adds the elements in row 0 and sum is stored in sumRow A First Book of C++ 4th Edition 4 Two-Dimensional Arrays (cont'd.)
  • 5. Two-Dimensional Arrays (cont'd.) A First Book of C++ 4th Edition 5
  • 6. ∗ Initialization: can be done within declaration statements (similar to single-dimension arrays) ∗ Example: int val[3][4] = { {8,16,9,52}, {3,15,27,6}, {14,25,2,10} }; ∗ First set of internal braces contains values for row 0, second set for row 1, and third set for row 2 ∗ Commas in initialization braces are required; inner braces can be omitted A First Book of C++ 4th Edition 6 Two-Dimensional Arrays (cont'd.) col 0 1 2 3 row 0 1 2
  • 7. A First Book of C++ 4th Edition 7 Two-Dimensional Arrays (cont'd.)
  • 8. A First Book of C++ 4th Edition 8 Two-Dimensional Arrays (cont'd.)
  • 9. ∗ Processing two-dimensional arrays: ∗ nested for loops are typically used ∗ Easy to cycle through each array element ∗ A pass through outer loop corresponds to a row ∗ A pass through inner loop corresponds to a column ∗ Nested for loop in Program 7.7 used to multiply each val element by 10 and display results ∗ Output of Program 7.7 Display of multiplied elements 80 160 90 520 30 150 270 60 140 250 20 100 A First Book of C++ 4th Edition 9 Two-Dimensional Arrays (cont'd.)
  • 10. A First Book of C++ 4th Edition 10 Two-Dimensional Arrays (cont'd.)
  • 11. A First Book of C++ 4th Edition 11 Two-Dimensional Arrays (cont'd.)
  • 12. ∗ Prototypes for functions that pass two-dimensional arrays can omit the row size of the array ∗ Example (Program 7.8): optional display (int [ ][4]); ∗ Row size is optional, but column size is required ∗ Assuming 4 bytes for an int, the element val[1][3] is located 28 bytes from the start of the array (i.e., offset) A First Book of C++ 4th Edition 12 Two-Dimensional Arrays (cont'd.)
  • 13. ∗ Determining offset of an array (val [1][3]) ∗ Computer uses row index, column index, and column size to determine offset A First Book of C++ 4th Edition 13 Two-Dimensional Arrays (cont'd.) 3+1
  • 14. Two-Dimensional Arrays (cont'd.) A First Book of C++ 4th Edition 14 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes
  • 15. ∗ Arrays with more than two dimensions allowed in C++ but not commonly used ∗ Example: int response[4][10][6] ∗ First element is response[0][0][0] ∗ Last element is response[3][9][5] ∗ A three-dimensional array can be viewed as a book of data tables (Figure 7.10) ∗ First subscript (rank) is page number of table ∗ Second subscript is row in table ∗ Third subscript is desired column A First Book of C++ 4th Edition 15 Larger Dimensional Arrays
  • 16. Larger Dimensional Arrays (cont'd.) A First Book of C++ 4th Edition 16
  • 17. ∗ Forgetting to declare an array ∗ Results in a compiler error message equivalent to “invalid indirection” each time a subscripted variable is encountered within a program ∗ Using a subscript that references a nonexistent array element (exceeding declared size/index) ∗ For example, declaring array to be of size 20 (e.g, int val[20])and using a subscript value of 25 (e.g, val[25]) ∗ Not detected by most C++ compilers and will probably cause a runtime error A First Book of C++ 4th Edition 17 Common Programming Errors
  • 18. ∗ Not using a large enough counter value in a for loop counter to cycle through all array elements ∗ Forgetting to initialize array elements ∗ Don’t assume compiler does this. ∗ Example: val [8] = 60; // initialize or assign value to your array element A First Book of C++ 4th Edition 18 Common Programming Errors (cont'd.)
  • 19. ∗ One-dimensional array: a data structure that stores a list of values of same data type ∗ Must specify data type and array size ∗ Example: int num[100]; //creates an array of 100 integers ∗ Array elements are stored in contiguous locations (in sequence) in memory and referenced using the array name and a subscript ∗ Example: values[4] ∗ means access the 5th element of the values array at index 4 A First Book of C++ 4th Edition 19 Summary
  • 20. ∗ Two-dimensional array is declared by listing both a row and column size with data type and name of array ∗ Arrays may be initialized when they are declared ∗ For two-dimensional arrays, you list the initial values, in a row-by-row manner, within braces and separating them with commas ∗ Arrays are passed to a function by passing name of array as an argument (to be discussed in next class) A First Book of C++ 4th Edition 20 Summary (cont'd.)
  • 21. ∗ Most programmers encounter the need to both sort and search a list of data items at some time in their programming careers A First Book of C++ 4th Edition 21 Chapter Supplement: Searching and Sorting Methods
  • 22. ∗ Linear (sequential) search ∗ Each item in the list is examined in the order in which it occurs until the desired item is found or the end of the list is reached ∗ List doesn’t have to be in sorted order to perform the search ∗ Binary search ∗ Starting with an ordered list, the desired item is first compared with the element in the middle of the list ∗ If item is not found, you continue the search on either the first or second half of the list A First Book of C++ 4th Edition 22 Search Algorithms