09 More Arrays
09 More Arrays
More Arrays
Charlotte Curtis
October 9, 2024
Where we left off
Arrays vs Python lists
C-style arrays
Array indexing
Passing arrays to functions, with and without const
Textbook Chapter 7
1
Today's topics
Partially filled arrays
Multi-dimensional arrays
C-style strings
2
But first, a note on % ...
The % operator doesn't behave the same way in C++ as it does in Python
Python C++
4
Returning arrays from functions
Short answer: you can't! The following does not compile:
int [] fibonacci(int n) {
int fib[n] = {1, 1};
for (int i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
return fib;
}
5
Partially filled arrays
Arrays of fixed-length seem quite limiting, especially coming from Python
high_temps = []
temp = float(input("Enter the next temperature: "))
while temp != -100:
high_temps.append(temp)
temp = float(input("Enter the next temperature: "))
A workaround for C-style arrays is to allocate the maximum size you think you
might need, then keep track of the fill size of the array
6
Partially filled array example
const int MAX_SIZE = 30;
double high_temps[MAX_SIZE] = {};
int num_temps = 0;
double temp = 0;
cout << "Enter the next temperature: ";
cin >> temp;
while (temp != -100 && num_temps < MAX_SIZE) {
high_temps[num_temps] = temp;
num_temps++;
cout << "Enter the next temperature: ";
cin >> temp;
}
7
Multi-dimensional arrays
So far we've only looked at one-dimensional arrays
Typically we think of the first dimension as the rows, the second as the columns
This is different from a char[6] or a char[8] - the size is part of the type
8
Multi-dimensions means multiple loops
To process a 2D array, you'll (almost always) need nested loops
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
cout << connect4[r][c];
}
cout << endl;
}
9
Passing multi-dimensional arrays to functions
Multidimensional arrays are passed by reference just like 1D arrays
An initialization function might have the following prototype:
void initialize(char connect4[][COLS], int size);
10
Processing row by row
Depending on the data, you might want to process one row at a time:
Practice time!
11
C-style strings
C-style strings are arrays of characters
We said that you can't do this:
int primes[] = {2, 3, 5, 7, 11};
cout << primes << endl;
12
The null terminator
Issue: how long should the string be?
We could keep track of a partially filled array size, like this:
char letters[26] = {'a', 'e', 'i', 'o', 'u'};
int fill = 5;
The null terminator is a sentinel that marks the end of the string
13
C-string shorthand
C++ has a shorthand for initializing C-strings:
char vowels[] = "aeiou";
14
Some C-string gotchas
Initializing with a string literal is a shorthand - the following are identical:
This means that you cannot reassign a C-string, just as you can only use the curly
bracket syntax when initializing an array
15
C-strings plus functions
Since a C-string always has a null terminator, no need to pass the size
Example: a function to calculate the length of a string
int len(const char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
This is so common that C++ provides a function strlen in the <cstring> library,
along with other useful string manipulation functions
16
Coming up next
Assignment 1 is due Friday
Assignment 2 will be released Friday (lots of 2D arrays!)
Reading week already! No classes next week
I will be available by email and for office hours after the Monday holiday
After the break: More on C-strings, file I/O and command line arguments
17