Charlotte Curtis February 7, 2024
const
Textbook Chapter 7
int counts[N_LETTERS] = {}; char letter; cin >> letter; while (!cin.eof()) { if (is_alpha(letter)) counts[to_index(letter)]++; cin >> letter; }
Textbook Section 8.1
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
The following function is intended to initialize a 2D array of integers to all -1. What is wrong with it?
arr
rows
cols
void initialize(int arr[][], int rows, int cols) { for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { arr[r][c] = -1; } } }
What is the output of the following code?
arr[][0]
0 0 0
const int ROWS = 3; const int COLS = 3; int arr[ROWS][COLS] = {}; cout << arr[][0] << endl;
int primes[] = {2, 3, 5, 7, 11}; cout << primes << endl;
char vowels[] = {'a', 'e', 'i', 'o', 'u'}; cout << vowels << endl;
char vowels[5] = {'a', 'e', 'i', 'o', 'u'}; int size = 5;
char vowels[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
An array of chars is not a C-string until it has a null terminator
char
char vowels[] = "aeiou";
char a_ch = 'a'; char a_str[] = "a"; char greeting[32] = "Hello!"; char hello[6] = "Hello!";
Initializing with a string literal is a shorthand - the following are identical:
char message[] = "Hello!"; char message2[] = {'H', 'e', 'l', 'l', 'o', '!', '\0'};
This means that you cannot reassign a C-string, just as you can only use the curly bracket syntax when initializing an array
You can reassign individual characters:
char message[] = "Hello!"; message[0] = 'G';
Don't forget to allocate enough space for the null terminator!
cout << "This is a C-string" << endl; char message[] = "This is also a C-string"; cout << message << endl;
char name[32]; // need to guess a size! cout << "Enter your name: "; cin >> name;
cin
getline
cin.getline(buffer, size, [delimiter]); // optional third argument
size - 1
const int MAX_NAME = 32; char name[MAX_NAME]; cin.getline(name, MAX_NAME);
If you enter more than than size - 1 characters, they'll be left in the buffer!
get
cin.get(buffer, size, delimiter)
cin >> var
cin.ignore(n)
n
cin >> ws
int len(const char str[]) { int length = 0; while (str[length] != '\0') { length++; } return length; }
strlen
<cstring>
strlen(str)
strcpy(dest, src)
strcat(dest, src)
strcmp(str1, str2)
char name[4]; strcpy(name, "Charlotte Curtis");
hello = "Hello" world = "World" message = hello + " " + world + "!" print(message)
char hello[] = "Hello"; char world[] = "World"; char message[32]; strcpy(message, hello); strcat(message, " "); strcat(message, world); strcat(message, "!"); cout << message << endl;
strcmp
For the function call strcmp(str1, str2), the return value is:
0
str1
str2
-1
1
char fruit[]; cout << "What kind of fruit would you like? "; cin >> fruit; if (strcmp("apple", fruit) == 0) { cout << "Great choice, you can make pie!" << endl; }
string
lab.h
lab.cpp
#include "lab.h"
main.cpp
g++ -c lab.cpp
lab.o
g++ -c main.cpp
main.o
g++ -o main main.o lab.o
make
Compiling in multiple steps is annoying, so we dump it in a makefile
makefile
# This is "Makefile". Notice that comments begin with "#" program: lab.o main.o g++ main.o lab.o –o program main.o: main.cpp g++ -c main.cpp lab.o: lab.cpp g++ -c lab.cpp
#include
applicant.h
score.h
#ifndef
#endif
#ifndef APPLICANT_H #define APPLICANT_H ... // contents of applicant.h #endif // APPLICANT_H
APPLICANT_H
Which of the following are good reasons to use separate compilation? Select all that apply.
The #include directive is a preprocessor directive that means:
.cpp
Textbook Chapter 10
Do example with tic-tac-toe game
Write the prototype on the board