SlideShare a Scribd company logo
1 of 22
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

Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - NotesOmprakash Chauhan
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operationSenthil Kumar
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayimtiazalijoono
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)EngineerBabu
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queueAmit Vats
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - NotesOmprakash Chauhan
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)Imdadul Himu
 

What's hot (20)

Queues-handouts
Queues-handoutsQueues-handouts
Queues-handouts
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Array ppt
Array pptArray ppt
Array ppt
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Arrays
ArraysArrays
Arrays
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Stacks
StacksStacks
Stacks
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
C programming , array 2020
C programming , array 2020C programming , array 2020
C programming , array 2020
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 

Viewers also liked

Daniel Witherite UT-UXO Certificate
Daniel Witherite UT-UXO CertificateDaniel Witherite UT-UXO Certificate
Daniel Witherite UT-UXO CertificateDaniel Witherite
 
Growth Hacking with Kentico
Growth Hacking with KenticoGrowth Hacking with Kentico
Growth Hacking with KenticoBrian McKeiver
 
Casa design 2014
Casa design 2014Casa design 2014
Casa design 2014casadesign
 
Sistemas de Información en la Empresa
Sistemas de Información en la EmpresaSistemas de Información en la Empresa
Sistemas de Información en la EmpresaEdicion Ticnews
 
11review(inheritance andpolymorphism)
11review(inheritance andpolymorphism)11review(inheritance andpolymorphism)
11review(inheritance andpolymorphism)IIUM
 
Mediterranean – Adriatic Underwater Cultural Heritage links
Mediterranean – Adriatic Underwater Cultural Heritage linksMediterranean – Adriatic Underwater Cultural Heritage links
Mediterranean – Adriatic Underwater Cultural Heritage linksUNESCO Venice Office
 
Oportunidad Negocio (2 )
Oportunidad Negocio (2 )Oportunidad Negocio (2 )
Oportunidad Negocio (2 )HERBALIFE
 
Startup Braga - value proposition workshop
Startup Braga - value proposition workshopStartup Braga - value proposition workshop
Startup Braga - value proposition workshopStartup Braga
 
Brochure Seminario 23 de mayo
Brochure Seminario 23 de mayoBrochure Seminario 23 de mayo
Brochure Seminario 23 de mayoEmprende Claro
 

Viewers also liked (13)

Daniel Witherite UT-UXO Certificate
Daniel Witherite UT-UXO CertificateDaniel Witherite UT-UXO Certificate
Daniel Witherite UT-UXO Certificate
 
Growth Hacking with Kentico
Growth Hacking with KenticoGrowth Hacking with Kentico
Growth Hacking with Kentico
 
El informe de investigación
El informe de investigaciónEl informe de investigación
El informe de investigación
 
Casa design 2014
Casa design 2014Casa design 2014
Casa design 2014
 
Sistemas de Información en la Empresa
Sistemas de Información en la EmpresaSistemas de Información en la Empresa
Sistemas de Información en la Empresa
 
11review(inheritance andpolymorphism)
11review(inheritance andpolymorphism)11review(inheritance andpolymorphism)
11review(inheritance andpolymorphism)
 
Mediterranean – Adriatic Underwater Cultural Heritage links
Mediterranean – Adriatic Underwater Cultural Heritage linksMediterranean – Adriatic Underwater Cultural Heritage links
Mediterranean – Adriatic Underwater Cultural Heritage links
 
YALI_Certificate (2)
YALI_Certificate (2)YALI_Certificate (2)
YALI_Certificate (2)
 
Apostila de empilhadeira rv1
Apostila de empilhadeira rv1Apostila de empilhadeira rv1
Apostila de empilhadeira rv1
 
PRIMAVERA - A Nova Fiscalidade Nacional em Angola
PRIMAVERA - A Nova Fiscalidade Nacional em AngolaPRIMAVERA - A Nova Fiscalidade Nacional em Angola
PRIMAVERA - A Nova Fiscalidade Nacional em Angola
 
Oportunidad Negocio (2 )
Oportunidad Negocio (2 )Oportunidad Negocio (2 )
Oportunidad Negocio (2 )
 
Startup Braga - value proposition workshop
Startup Braga - value proposition workshopStartup Braga - value proposition workshop
Startup Braga - value proposition workshop
 
Brochure Seminario 23 de mayo
Brochure Seminario 23 de mayoBrochure Seminario 23 de mayo
Brochure Seminario 23 de mayo
 

Similar to Csc1100 lecture09 ch07_pt2

Similar to Csc1100 lecture09 ch07_pt2 (20)

Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1Csc1100 lecture07 ch07_pt1-1
Csc1100 lecture07 ch07_pt1-1
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt
 
Arrays
ArraysArrays
Arrays
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
 
Arrays
ArraysArrays
Arrays
 
Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2Csc1100 lecture12 ch08_pt2
Csc1100 lecture12 ch08_pt2
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Unit 2
Unit 2Unit 2
Unit 2
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 

More from IIUM

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhostIIUM
 
Chapter 2
Chapter 2Chapter 2
Chapter 2IIUM
 
Chapter 1
Chapter 1Chapter 1
Chapter 1IIUM
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimediaIIUM
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblockIIUM
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice keyIIUM
 
Group p1
Group p1Group p1
Group p1IIUM
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoIIUM
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009IIUM
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lformsIIUM
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answerIIUM
 
Redo midterm
Redo midtermRedo midterm
Redo midtermIIUM
 
Heaps
HeapsHeaps
HeapsIIUM
 
Report format
Report formatReport format
Report formatIIUM
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelinesIIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam PaperIIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam PaperIIUM
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516IIUM
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotationsIIUM
 
Week12 graph
Week12   graph Week12   graph
Week12 graph IIUM
 

More from IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Heaps
HeapsHeaps
Heaps
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 

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