C++ Arrays
Basics
Arrays are commonly used to store multiple values. It's one of the basic data structures. We can use it to model a list/array/collection of items. Say, an array of cellphone numbers, an array of first names, etc.
To declare an array in C++, we need to specify 3 things, using the square bracket notation.
- Data Type
- Size
- Name of the array
For example,
To initialize values in the array during declaration, we can use the curly bracket notation.
Or simply omitting the size, the compiler is smart enough to count the number of values to be inserted.
We can access each of the values in an array by its index, using the square bracket notation. C++ is zero-indexed, meaning the first value in the array has an index of 0.
For example, to retrieve the second scores from the scores
array, we can simply
If the fourth score should be 98
instead of 89
, we can modify the value by index as well
Iteration
Since there are multiple values in an array, and each of value can be controlled by its index, we can easily loop through an array with a loop.
With a for
loop,
loop-through-array-1.cpp | |
---|---|
Or with a while
loop,
loop-through-array-2.cpp | |
---|---|
Both of the above looping examples are not elegant enough since we hardcoded the number of values in an array, which is not necessary.
sizeof()
would return the size in bytes. When we divide the size in bytes of an array by the size of data type, there comes the number of values.
Modern C++ also support foreach
like loop to iterate all the values,
loop-through-array-4.cpp | |
---|---|
Assignment 12
Create a C++ program named score-keeper.cpp
that would prompt the user for the number of scores it's going to keep. Then it asks the user to enter a numeric value score for that given number of times. Finally it displays the average of the scores.
A sample run looks like the following.
g++ score-keeper.cpp -o score-keeper
./score-keeper
Enter number of scores: 3
Enter score: 100
Enter score: 99
Enter score: 60
Average score is 86.3333
Sample Solution
Multi-Dimensional Arrays
A multi-dimensional array is a set of embedded arrays. For example, a 2D array is an array of arrays. A 3D array is an array of 2D array.
To declare a 2D array, define the variable type, specify the name of the array followed by square brackets which specify how many elements the main array has, followed by another set of square brackets which indicates how many elements the sub-arrays have.
In case we have higher dimensional arrays, we simply keep on adding square brackets to generate more dimensions.
When to use multi-dimensional array?
It's natural to model concepts that are multi-dimensional, such as matrix, spreadsheets, and pixels.
And when we apply algorithm such as dynamic programming, knowing to to use an array becomes a must.
For example, we have to create an array to keep the scores for all 3 students in a class, where all students study the same 4 subjects. We can use a 2D (3x4 or 4x3) array to store the information.
Similar with ordinary arrays, we can initialize the values upon declaration. But since we have more dimensions, the curly bracket notation becomes embedded as well.
Accessing and changing values in a multi-dimensional array is conceptually same with the ordinary one. But instead of using one index, we have to use multiple index to refer to its location in different dimensions.
And to loop through the entire array, we normally need to use embedded loops.
Assignment 13
Create a C++ program named multi-score-keeper.cpp
that would prompt the number of players to begin with. Then it prompts the user for the number of scores it's going to keep for each player. Then it asks the user to enter numericals for all players' scores. Finally it displays the average of the scores for each player.
A sample run looks like the following.
g++ multi-score-keeper.cpp -o multi-score-keeper
./multi-score-keeper
Enter number of players: 2
Enter number of scores: 2
Play 1:
Enter score 1: 100
Enter score 2: 90
Play 2:
Enter score 1: 80
Enter score 2: 77
Average score of Player 1 is 95
Average score of Player 2 is 78.5
Sample Solution
Strings
A string
variable in C++ is a collection of characters. We can use the square bracket notation to access and modify the value in a string.
For example,
strings-demo.cpp | |
---|---|
For convenience, we can use length() or size() to get the length of a string.
The +
operator when used with strings, it operates as concatenation.
strings-concat.cpp | |
---|---|
In C++ a string
variable can store whitespaces. But cin
considers whitespaces as a terminating character. If we want the freeform text from terminal, use getline
instead.
strings-getline.cpp | |
---|---|
There are other functions that may be useful for string
.
For example,
Assignment 14
Let's use J3 from 2021 as an exercise.
Sample Solution
Dynamic Programming
We often use multi-dimensional arrays to solve dynamic programming problems. Let's solve Lattice paths.
Dynamic programming is a method to simplify a complex problem into simpler sub-problems recursively. In this manner we can forward propagate to find the result by solving easy problems.
Use the Lattice paths problem for example. Think about the scenarios for:
- 0x0 grid
- 1x1 grid
- 1x2 grid
- 2x1 grid
Assignment 15
Create a C++ program named edit-distance.cpp
that would prompt the user for two strings. Given two strings str1
and str2
and below operations that can be performed on str1
. Find minimum number of edits (operations) required to convert str1
into str2
.
- Insert
- Remove
- Replace
Here are a few examples.
Example 1
Input: str1 = "cat", str2 = "cut"
Output: 1
Explanation: We can convert str1 into str2 by replacing a
with u
.
Example 2
Input: str1 = "sunday", str2 = "saturday"
Output: 3
Explanation: Last three and first characters are same. We basically need to convert un
to atur
. This can be done using below three operations. Replace n
with r
, insert t, insert a
A sample run looks like the following.