DIPT003_Week 9 Arrays
Chapter 7: Arrays
Introduction to Arrays
Definition: An array is a sequential collection of data storage locations, each capable of holding the same data type.
Variable Nature: An array is a variable that stores multiple values of the same type.
Memory Layout: Values are stored in adjacent memory locations.
Declaration Syntax: Arrays are declared using the
[]operator, e.g.,int tests[5];.
7.1 Arrays Hold Multiple Values
Arrays allow the storage of multiple values of a defined data type in a contiguous block of memory.
Array Memory Layout
When declaring an array such as
int tests[5];, memory is allocated for the number of elements specified in the array declaration. The layout is:first elementsecond elementthird elementfourth elementfifth element
Array Terminology
For the declaration
int tests[5];:int: The data type of the array elements.tests: The name of the array.5: The size declarator, indicating the number of elements.Size Calculation: The size of an array is calculated as:
Size of Arrays
Total Memory Size:
Example:
int tests[5]occupies 20 bytes assuming eachintis 4 bytes.Example:
long double measures[10]occupies 80 bytes assuming eachlong doubleis 8 bytes.
Named Constants as Size Declarators
Use named constants for better maintainability:
const int SIZE = 5;
int tests[SIZE];
Changes in array size can be easily made by adjusting the constant value.
7.2 Accessing Array Elements
Each element in an array has a unique subscript starting at 0:
Subscripts:
0, 1, 2, 3, 4.
Last Element Subscript: The last element’s subscript is n - 1, where n is the number of elements.
Using Array Elements
Array elements can be used just like ordinary variables:
tests[0] = 79;
cout << tests[0];
cin >> tests[1];
tests[4] = tests[0] + tests[1];
Attempting to access the entire array using its name is illegal:
cout << tests; // not legal
Accessing Array Contents
Elements can be accessed with a literal subscript:
cout << tests[3] << endl;
Can also
Introduction to Arrays - Definition: An array is a collection of data storage locations of the same type. - Variable Nature: An array stores multiple values of the same type. - Memory Layout: Values are in adjacent memory locations. - Declaration Syntax: Arrays declared using [], e.g., int tests[5];.
7.1 Arrays Hold Multiple Values - Arrays allow storage of multiple values in contiguous memory.
Array Memory Layout - For int tests[5];, memory is allocated for elements specified. Memory layout consists of: - first element - second element - third element - fourth element - fifth element
Array Terminology - For int tests[5];: - int: Data type. - tests: Array name. - 5: Number of elements. - Size Calculation:
Size of Arrays - Total Memory Size: - Example: int tests[5] occupies 20 bytes (assuming 4 bytes per int). - Example: long double measures[10] occupies 80 bytes (assuming 8 bytes per long double).
Named Constants as Size Declarators - Use named constants for maintainability:
const int SIZE = 5;
int tests[SIZE];
Adjusting the constant changes array size easily.
7.2 Accessing Array Elements - Each element has a unique subscript starting at 0. - Last Element Subscript: n - 1 (where n is number of elements).
Using Array Elements - Elements can be used like variables:
tests[0] = 79;
cout << tests[0];
cin >> tests[1];
tests[4] = tests[0] + tests[1];
Accessing the entire array using its name is illegal:
cout << tests; // not legal
Accessing Array Contents - Elements accessed with a subscript:
cout << tests[3] << endl;