CS112 Lecture 2.2 Notes: Arrays
Lecture Notes for CS112: Arrays (Semester 2, 2007)
Initialization of Arrays
Explicit Initialization List: An array can be initialized directly at the time of its definition.
Example:
int myArray[5] = {6, 7, 888, 987, 0};
Manual Initialization: Arrays can also be initialized using a loop to assign values.
Example:
int myArray[10];for (int i = 0; i < 10; i++) myArray[i] = 0;This executes at runtime, assigning each element to 0.
Alternative Initialization:
int myArray[10] = {0};This initializes all elements to 0 at compile time; considered better practice.
Exact Size Requirement: When initializing with specific values, it is critical to ensure the number of initializers matches the declared size of the array.
Examples:
int myArray[3] = {3, 4, 5};int myArray[] = {3, 4, 5};This creates a three-element array.
Initializing Arrays of Characters
Character Arrays: Strings can be represented as arrays of characters, typically null-terminated.
Examples:
char charArray[6] = {'h', 'e', 'l', 'l', 'o', '\0'};char charArray1[] = {'h', 'e', 'l', 'l', 'o', '\0'};char charArray2[] = "hello";The size is determined automatically by the compiler due to the null-terminator.
Null Character: The special character
‘\0’signifies the end of the string in C/C++ programming.Printing Characters: Accessing elements in character arrays can be done using loops.
Example:
char exOne[] = "bula";for (int j = 0; j < 5; j++) cout << exOne[j];This prints
bulato the console.
Multidimensional Arrays
Representation: Multidimensional arrays are often utilized to represent data in tabular formats, such as matrices.
Examples:
float temperatures[12][31];Intended for storing temperature data for each day across the year.
char daysofweek[7][10];For storing names of the days in a matrix format.
Accessing Multidimensional Arrays
Nested Loops: A common technique for accessing elements in multidimensional arrays is through the use of nested for loops.
Example:
#define MAXI 50;#define MAXJ 75;int i, j;float values[MAXI][MAXJ];for (i = 0; i < MAXI; i++) {for (j = 0; j < MAXJ; j++) {values[i][j] = whatever;}}Note: The correct syntax for accessing an element is
values[i][j]. Usingvalues[i, j]would be incorrect.
Common Errors for Arrays
Invalid Indexing: A frequent error encountered when dealing with arrays is the use of incorrect loop limits, which leads to out-of-bounds access.
Example:
int i;float b[10];for (i < 0; i <= 10; i++) {}This loop should utilize
i < 10instead ofi <= 10to prevent accessing invalid indices.
Initializing Multidimensional Arrays
Array Initialization with Specific Values: When initializing multidimensional arrays, it is important to properly structure the initial values provided.
Example:
int a[2][3] = { {1, 2, 3}, {4, 5, 6} };int b[2][3] = { 1, 2, 3, 4, 5 };int c[2][3] = { {1, 2}, {4} };If fewer initializers are provided for a row, the remaining elements default to 0.
Accessing Elements: Each element can be accessed via similar nested loops as used for single arrays.
Print Example:
for (int i = 0; i < 2; i++) {for (int j = 0; j < 3; j++) {cout << a[i][j] << ' ';}}
Output for initialized arrays:
Array
a:1 2 3 4 5 6Array
b:1 2 3 4 5 0Array
c:1 2 0 4 0 0
Passing Multidimensional Arrays to Functions
Function Parameter Requirements: When passing multidimensional arrays as parameters, only the size of the first subscript is not required. All subsequent subscript sizes must be specified.
Function Declaration Example:
void printArray(int array[][3]);Function Call Example:
int myarray[4][3];printArray(myarray);