Charlotte Curtis February 5, 2024
Textbook Chapter 7
int cup_sizes[] = {8, 12, 16, 20}; for (int i = 0; i < 4; i++) { cout << cup_sizes[i] << " oz" << endl; }
const
Textbook Chapter 7, 8.1
double sum_10_elements(double arr[10]) { double sum = 0; for (int i = 0; i < 10; i++) { sum += arr[i]; } return sum; }
Why wouldn't we just use a global constant? Or sizeof?
sizeof
double sum_all(double arr[10], int size);
[10]
[]
void add_one(double arr[], int size) { for (int i = 0; i < size; i++) { arr[i] += 1; } }
&
double sum_all(const double arr[], int size);
const in a function header means that the function cannot modify the parameter
This does not modify the const-ness of the value that is passed
void print(const int x) { cout << "You passed " << x << endl; // no problem x = 5; // compiler error, x is const in this scope } ... int y = 10; print(y); // no problem y = 5; // also no problem
What if I changed it to void print(const int &x)?
void print(const int &x)
What about the following?
double[] get_temps() { const int FORECAST_DAYS = 7; double high_temps[FORECAST_DAYS] = {}; for (int i = 0; i < FORECAST_DAYS; i++) { cout << "Enter high temp for day " << i + 1 << ": "; cin >> high_temps[i]; } return high_temps; }
What is the output from the following code?
0
4
int arr[5] = {}; cout << arr[4] << endl;
int arr[5] = {}; cout << arr << endl;
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 actual size of the array
This is called a partially filled array
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; }
num_temps
Searching through an array to find a value is a common task
Example: find the first day with a temperature below 0
int first_freezing_day(const double temps[], int size);
Things to consider:
Repeat until the array is sorted: Go through the array and find the smallest element Swap the smallest element with the first element Update the start of the array to be the next element
So far we've only looked at one-dimensional arrays
How about a two-dimensional array?
Just like Python's list of lists
board = [['', '', ''], ['', '', ''], ['', '', '']]
const int ROWS = 3; const int COLS = 3; char board[ROWS][COLS] = {};
The first dimension is the rows, the second is the columns
Given this declaration:
What is the type of each of the following?:
board
board[0]
board[0][0]
char board[ROWS][COLS] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
char board[ROWS][COLS]; for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { board[row][col] = ' '; } }
void initialize(char board[][COLS], int size);
This is probably a good place to use a global constant
Depending on the data, you might want to process one row at a time:
const int MAX_RECORDS = 100; const int NUM_FIELDS = 5; int records[MAX_RECORDS][NUM_FIELDS] = {}; for (int row = 0; row < MAX_RECORDS; row++) { read_record(records[row], NUM_FIELDS); }
read_record
int primes[] = {2, 3, 5, 7, 11}; cout << primes << endl;
char vowels[] = {'a', 'e', 'i', 'o', 'u'}; cout << vowels << endl;
Textbook Chapter 8.1, 8.2
Do some demos
Show example of how calling doesn't change
Error!
How do you choose max size?
Take some time to think on algo
Draw on the board
LCVs, update?
Do example with tic-tac-toe game