Chap 5 CPP 6 TH
Chap 5 CPP 6 TH
Arrays
• Improves readability
• Improves versatility
• Improves maintainability
• Example
int arr[] = {20, 30, 40, 50};
for (int x : arr)
cout << x << " ";
cout << endl; Output: 20 30 40 50
Copyright © 2017 Pearson Education, Ltd. All rights reserved. 5-14
Arrays in Memory
• Recall simple variables:
– Allocated memory in an "address"
• Array declarations allocate memory for
entire array
• Sequentially-allocated
– Means addresses allocated "back-to-back"
– Allows indexing calculations
• Simple "addition" from array beginning (index 0)
– Sorting
– Searching
• Example:
void DisplayPage(const char p[][100], int sizeDimension1)
{
for (int index1=0; index1<sizeDimension1; index1++)
{
for (int index2=0; index2 < 100; index2++)
cout << p[index1][index2];
cout << endl;
}
}
• Multidimensional arrays
– Create "array of arrays"