Multidimensional Arrays Notes
Multidimensional Arrays
- A more complex data structure that enforces correlation rules.
- Used for sophisticated problems like time series data analysis.
- Commonly used in machine learning algorithms.
- Can have thousands or millions of dimensions, but we'll focus on two-dimensional arrays.
Task Update: Employee Hours
- Goal: Record the number of hours each of the five employees work each day from Monday to Thursday.
- Segmentation:
- Getting the data.
- Implementing the algorithm.
- Updates:
- Update the data to handle a multidimensional array.
- Update the algorithm: for each employee, for each day, print the hours.
Representing Data with Two-Dimensional Arrays
- One-dimensional arrays are insufficient for representing rows and columns.
- Two-dimensional arrays allow specifying the number of rows (employees) and columns (days).
- Number of rows = 5 (employees).
- Number of columns = 4 (days).
- Theoretically and implementation wise, swapping rows and columns can make a difference.
Creating Multidimensional Arrays
1. Creating an Empty Array and Adding Elements
Specify the array type, followed by square brackets and the identifier.
The number of commas within the square brackets indicates the number of dimensions minus one.
- Two-dimensional array: one comma.
- Three-dimensional array: two commas.
Syntax:
type[,] identifier = new type[length1, length2];Example:
int[,] hours = new int[5, 4];Default Values:
- Numeric types: 0
- Characters: Unicode null character
- Booleans: false
Setting Values:
- Use the array name followed by indices to set values for each element.
- Can be done individually or via loops.
- Example:
hours[0, 0] = 6;(Jenny's hours on Monday set to 6).
2. Setting Values During Array Creation (with new keyword)
Specify values upon creation using the
newkeyword and compound type.Syntax:
type[,] identifier = new type[,] {{val1, val2, ...}, {val3, val4, ...}, ...};The compiler infers array lengths from the provided values.
3. Setting Values During Array Creation (without new keyword)
Similar to the previous method but without using the
newkeyword or specifying the type on the right-hand side.Syntax:
type[,] identifier = {{val1, val2, ...}, {val3, val4, ...}, ...};This method is used for the test case example.
Visualizing Two-Dimensional Arrays
- Table: Rows and columns (employees x days).
- Array within an Array: An array of employees, where each employee has an array of days.
hours[employeeIndex, dayIndex]accesses the hours for a specific employee on a specific day.
Algorithm for Printing Values
- Uses nested
forloops. - Outer loop: processes employees.
- Inner loop: processes days.
Outer Loop
- Iterates through each employee.
- Initial value: 0.
- Change statement: increments the value.
- Condition statement: uses the length of the first index of the array (employees).
getLength(0)returns the number of employees.
Inner Loop
- Iterates through each day for the current employee.
- Initial value: 0.
- Change statement: increments the value.
- Condition statement: uses the length of the second index of the array (days).
getLength(1)returns the number of days.
Printing Hours
- Prints the hours for each day within the inner loop.
- Right and RightLine statements used for presentation.
Presentation Improvements
- Print Employee Names:
- Arrays can only hold one type (names are strings).
- Use a separate
namesarray to print the name during the outerforloop execution.
- Calculate Total Hours per Week:
- Useful for verification.
- Sum the hours for each employee across all days.
Testing Considerations
- Empty or single-element arrays.
- Valid/invalid input.
- Array lengths: ensure they are the same or handle different lengths.
- Test cases with the outer array (employees) larger than the inner array (days) using different values.
Multidimensional vs. Parallel Arrays
- Multidimensional array: Stricter controls about correlation.
- Conceptually like a table with rows and columns.
- Useful for complex datasets like time series.
- Disadvantage: only supports data of the same type.
- Choice depends on the context of the problem.