1/36
Information about c++
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
What is an array?
An array is a block of contiguous memory locations to hold values of a single specified type.
What type of values can an array hold?
Array can hold values of ANY type.
What is an arrays index base?
Arrays are 0 based, this means that the first element has index 0.
What structure is used when declaring an array?
To declare an array, use the following structure
*type variable_name[kSize];
*Ex. Int my_array[5]
Name each part of this declared array
Int my_array[5]
Int- the data type
my_array- the variable name
[5]- the size of the array
Explain what this array is doing
Int my_array[5]
Explanation: declares an array named my-array that can hold 5 integers 0, 1, 2, 3, 4.
What type of arrays are we using?
Static Arrays
What must be declared when using a static array and is it allowed to be changed during the program?
The size must be set when the array is declared and cannot be changed during the course of the program.
What kind of variables should be used for the array size?
Use constant variables for the array’s size so that modifications to the size can be made in one place.
In c++ what does the name of the array refer to?
The name of the array gives the address of the first element (i.e., a pointer to the beginning of the array).
int x[5] = {3, 7, 2};
cout << x[4]; // what is the output?
cout << x; // what is the output?
The out would be 0 since there are only 3 elements in the array, so there fore the rest of the values will be set to 0.
x prints out the memory address. So it would look something like “0x7ffee4c37000”.
What should be done in order to access and element in an array?
In order to access an element in the array, follow the name of the array with square brackets containing the elements index.
True or False: Declaring an array automatically initializes the values within the array
False. Declaring an array does not automatically initialize the values in an array the programmer has to initialize the values.
How can we initialize an array with values?
We can initialize our array in the declaration statement by assigning it a list of values, the list must be held in braces { }. The values in the list must be separated by commas.
Example: int my_array[5] = {14, 3, 7, 0, 1};
Note: You can only initialize the array with a list in the array declaration.
What will happen to any values that are left out of the list?
*If we are initializing an array of numeric values, any values left out of the list will be set to 0.
Example: int my_array[5] = {14};
14 0 0 0 0
Can you leave out the size of an array and if so what will this print?
int my_array[ ] = {14, 3, 6, 9, 2};
You can leave out the size of the array when using a list to initialize the array in the declaration. The array’s size will be the number of values in the initialization list.
Example: int my_array[ ] = {14, 3, 6, 9, 2};
14 3 6 9 2
Example:
for( int i = 0; i < 5; ++i ) {
cout << “Enter next number”;
cin >> my_array[ i ];
}
What does this code do and what will be its output?
To read values into a non-character array, use a counter-controlled loop to move through the array indices.
Example:
for ( int i = 0; i < 5; ++i ) {
cout << my_array[ i ] << “ “;
}
What does this code do and what will be its output?
To output the values in a non-character array, use a counter-controlled loop to move through the array.
What does it mean to overstep an array?
Overstepping an arrays bounds can attempt to modify values in memory locations being used by other variables or programs.
*Your compiler will not produce an error if an index is used outside of the 0 to size – 1 range.
Example:
int my_array[] = {1,2,3,4,5};
for ( int i = 0; i <=5; ++i ) {
my_array[i] *= 2;
}
Before: 1 2 3 4 5
After: 2 4 6 8 10 ???
How do you specify an array argument?
To specify an array argument,
place a set of empty brackets
beside the array’s data type in the
function’s parameter list (for
single-subscripted arrays).
Example:
void DoubleValues( int [], int );
When calling the function, use the
name of the array (no brackets) as
an argument of the function.
Write out how this would look?
Example:
int my_array[8]={1,2,3,4,5,6,7,8};
DoubleValues( my_array, 8 );
When passing an array argument to a function what must be sent?
When passing an array argument
to a function we’re sending the
address of the first element in the
array, allowing the function direct
access to the array elements.
• You can make array and reference
parameters constant.
principle of least privilege
void PrintArray( const int [], int );
What is Linear Search regarding arrays?
Start at one end of the array.
If the current element contains the
search value, return its array index
If not, move to the next element.
Continue until the element is found
or there are no more elements to
examine.
What is a binary search regarding arrays?
If the middle element contains
the value you’re searching for
return its array index.
Else, if the middle element is
larger than the value you’re
searching for, search the left
sub-array.
Else search the right sub-array.
What is a bubble sort regarding arrays?
Start at the end of the array swapping
all pairs of unordered elements until
you reach the beginning of the array.
Now the smallest element is at the
front.
Repeat the process until all of the
elements are in order.
What is a selection sort regarding arrays?
front = position 0
Find the smallest element and swap it
to the front. Increment the front
Repeat until front = size - 1
What is insertion sort regarding arrays?
sorted = 1
insert element the next element into
its correct position within the first sorted elements
Repeat until sorted = size
How do you access individual arrays?
You use the square brackets [] to access individual elements of an array by index.
Example:
int x = numbers[0]; // x = 10
numbers[2] = 99; // changes 3rd element (30 → 99)
Why is an array like a constant pointer?
An array name acts like a pointer to its first element.
But unlike a pointer variable, the array name cannot be changed (you can’t do x = new int[10];).
That’s why we say an array is like a constant pointer.
What would be the output of this multi dimensional array?
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
You need to fix all dimensions at compile-time.
You can initialize using nested lists (list of lists).
Elements are stored in row-major order.
Look at photo for break down.

What are character arrays?
Character arrays are just arrays of type char. They are commonly used to store C-style strings.
In c++ what does a null character do?
In C++, strings end with a null character: '\0'
It marks the end of the string.
It’s automatically added when you initialize a string with double quotes ("...").
Example: Look at photo

What happens when you initialize to c-string using double quotes?
Allocates space for all the characters plus one extra for the null character (\0)
char carray[] = "hi there";
So "hi there" has 8 characters, plus 1 null, totaling:
✅ Size of carray = 9
What does the overloaded << operator do?
The << operator is overloaded in C++ to print strings
It will print characters until it hits the null terminator \0
What are array function parameters? (Empty for first dimension)
1. Empty [] for the First Dimension
When defining a function that takes an array, you can leave the first dimension empty, because it's just a pointer:
void printArray(int arr[], int size); // Same as: void printArray(int* arr, int size);
✅ The compiler only needs the
second argument (size) to know how many elements to access.
What are array functions?
2. [constant] for Additional Dimensions
For multidimensional arrays, all dimensions except the first must be specified (i.e., not left empty), because the compiler needs this info to calculate element positions in memory.
void printMatrix(int matrix[][3], int rows);
Here:
The first dimension (rows) can be empty.
The second (columns) must be fixed, so the compiler knows the width of each row in memory.
Whats passed in array arguments?
