Eeb 334-315 c++ Lecture 5 Arrays (3)
Eeb 334-315 c++ Lecture 5 Arrays (3)
By
Dr. Ebenezer Esenogho
ARRAYS
• Introduction to Arrays
• Syntax of Arrays
• Multidimensional Arrays
• Dynamic Arrays
• Pointers
• Alternatives to Arrays,
Introduction to Arrays
Before now, we have used variables to store a single value. C++
offers the flexibility to store multiple values of the same type, and
address them with a single name. The mechanism that enables this is
arrays.
Points to Note:
• Arrays in C++ have a fixed size that you need to specify at the
time of declaration
Syntax of Arrays
Let’s say you want to create an array that holds 3 integer values;
following is the definition:
Arrays Indexing
Each array element has an array index. You can use the array index to
reference each element individually. You can think of an array as a set of
boxes. Each box has a label and contains an article. The label on the box is
the array index and the article in the box is the array element.
int arr[5];
In the above example, the name of the array is “arr”, its data type is integer
and size is “5”
int main() {
// Declare an array of integers with 5 elements
int arr[5];
// Accessing elements
int firstElement = arrInit[0]; // Access the first element (1)
int thirdElement = arrInit[2]; // Access the third element (3)
• When you initialize an array in C++ with fewer elements than its
size, the remaining elements are automatically initialized to 0.
• This is a feature of C++ where any unspecified elements in the array
will default to 0.
• For the statement int arr[5] = {1, 2};, here's what happens:
• The array arr has a size of 5.
• The first element arr[0] is initialized to 1.
• The second element arr[1] is initialized to 2.
• The remaining elements (arr[2], arr[3], arr[4]) are automatically
initialized to 0.
#include <iostream> // Include iostream for console input/output
int main() {
// Declare and partially initialize an array of 5 integers
int arr[5] = {1, 2};
You can imagine of it as box that contains other smaller boxes in it.
A two-dimensional array is similar to a table or a grid.
It contains two arrays; the first array is used to reference the rows
and the second array the columns. Similar to a grid, you can access
the elements of an array using the row and column coordinates.
arrayName[rowIndex][columnIndex];
int newarr[2][3];
In the above statement , you have declared an array “newarr” that has
two rows and three columns. Now, let’s initialize the elements of the
array “newarr”:
Let’s say you want to print the value “4”. The element is in the row
“0”, and column “2”. The C++ statement to print the element “4” is as
follows:
cout<<newarr[0][2]<<endl;
0 1 2
0
1
#include <iostream> // Include iostream for console input/output
int main() {
// Declare and initialize a 2D array with 2 rows and 3 columns
int newarr[2][3] = {{2, 3, 4}, {8, 9, 10}};
int main() {
// Declare and initialize a 2D array with 2 rows and 3 columns
int newarr[2][3] = {
{2, 3, 4},
{8, 9, 10}
};
int main() {
// Declare and initialize a 3D array (2x2x2)
int cube[2][2][2] = {
{
{1, 2},
{3, 4}
},
{
{5, 6},
{7, 8}
}
};
In C++, the name of an array acts as a pointer to its first element. This
means you can use pointers to access and manipulate array elements.
Therefore, there is a relationship between Pointers and Arrays
Syntax
int* array = new int[size];
#include <iostream> // Include iostream for console input/output
int main() {
// Dynamically create an array of 5 integers
int* numbers = new int[5];
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
return 0;
}
Array Boundary Issues Out-of-Bounds Access
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
When you declare a variable, memory is kept aside (allocated) for that
variable. The size of the memory kept aside, depends on the data type
of the variable. We can consider of the memory of the computer, as a
succession of memory cells, where each cell has a unique address.
Syntax
int x =5;
In the above example, the value “5” is stored in the memory allocated
to the variable “x”. See a diagrammatic representation
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an array of integers
int numbers[4] = {10, 20, 30, 40};
return 0;
A pointer however, is a variable that stores the memory address as its value.
#include <iostream>
#include <string>
using namespace std;
int main() {
string food = "Pizza";
int main() {
// Declare and initialize the array
int arr[5] = {10, 20, 30, 40, 50};
// You can also use the pointer directly in a loop to traverse the entire array
p = arr; // Reset pointer to the beginning of the array
cout << "Traversing the array using the pointer:" << endl;
for (int i = 0; i < 5; i++) {
cout << *(p + i) << " "; // Outputs 10 20 30 40 50
}
cout << endl;
return 0;
}
Passing Arrays to Functions
When you pass an array to a function, you actually pass a pointer to the first element of the array, not
a copy of the array. This means changes to the array inside the function affect the original array.
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
return 0;
}
Alternatives to Arrays
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
// Access elements
cout << "First element: " << vec[0] << endl;
cout << "Last element: " << vec.back() << endl;
return 0;
}
1. Write a C++ program that uses the “dynamic array” to store student
scores if the number of students is determined at runtime?