Lecture 8 CP Arrays 26092023 104704am
Lecture 8 CP Arrays 26092023 104704am
• Syntax:
array_name[i] = new_value;
Arr[0] = 12345;
Arrays - Traverse ()عبور
• Traversal is the process in which we visit every element of the
array. We use loops to iterate through each element of the array.
• Syntax:
for (int i=0; i<Size; i++) {
arr [i];
}
Arrays - How to use array
#include <iostream>
using namespace std;
int main() {
// array declaration and initialization
int arr[5] = { 10, 20, 30, 40, 50 };
// traversing array using for loop
for (int i=0; i<5; i++) {
cout << arr[i] << endl;
}
return 0;
}
Arrays - Types
• There are two types of arrays:
• One Dimensional Arrays
• Multidimensional Arrays
• Syntax:
array_name [size];
arr[5];
Arrays - Types - One Dimensional - Example
#include <iostream>
using namespace std;
int main() {
int arr[5]; // 1D array declaration
for (int i = 0; i < 5; i++) { // 1D array initialization using for loop
arr[i] = i * i - 2 * i + 1;
}
cout << "Elements of Array: " << endl;
// printing 1D array by traversing using for loop
for (int i = 0; i < 5; i++) {
cout << arr[i] << endl;
}
return 0;
}
Arrays - Types - Two Dimensional
• When an array has exactly two dimensions, it called 2D array.
• They can be visualized in the form of rows and columns organized
in a two-dimensional plane.
• Syntax:
array_name[size1] [size2];
arr[4][4];
Here,
• size1: Size of the first dimension.
• size2: Size of the second dimension.
Arrays - Types - Two Dimensional - Example
#include <iostream>
using namespace std;
int main() {
int arr[2][4] = { 10, 20, 30, 40, 50, 60, 70, 80 }; // declaring and initializing 2D array
// printing 2d array
for (int i=0; i<2; i++) {
for (int j=0; j<4; j++) {
cout << arr[i][j] << "\t";
}
cout << endl; // move to new row once one row is completed
}
return 0;
}
Arrays - Examples - Find Largest Number
#include <iostream>
using namespace std;
int main() {
int arr[10] = { 135, 165, 1, 16, 511, 65, 754, 654, 169, 4 };
int max = arr[0];
for (int i = 0; i < 10; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
cout << "Largest Number in the Array: " << max;
}
Arrays - Examples - Reverse a String
#include <iostream>
using namespace std;
int main() {
string s = "VeronikaDecidesToDie";
for (int i = 19; i >= 0; i--){
cout << s[i];
}
}
Note:
A standard way to find length of a string i = s.length()
// for (int i = str.length() - 1; i >= 0; i--)
Arrays - Properties
• It is very important to understand the properties of the array so
that we can avoid bugs while using it.
• The following are the main properties of an array in C++:
• 1. Fixed Size
• The array in C++ is fixed-size collection of elements.
• The size of the array must be known at the compile time and it cannot be
changed once it is declared.
• 2. Homogeneous Collection
• We can only store one type of element in an array.
• There is no restriction on the number of elements but the type of all of
these elements must be the same.
Arrays - Properties
• 3. Indexing in Array
• The array index always starts with 0 in C++ language.
• It means that the index of the first element of the array will be 0 and the last
element will be n – 1. (n is total number of elements)
• 4. Dimensions of an Array
• A dimension of an array is the number of indexes required to refer to an
element in the array.
• It is the number of directions in which you can grow the array size.
• 5. Contiguous Storage
• All the elements in the array are stored continuously one after another in the
memory.
• It is one of the defining properties of the array in C++ which is also the reason
why random access is possible in the array.
Arrays - Properties
• 6. Random Access
• The array in C++ provides random access to its element i.e we can get to
a random element at any index of the array in constant time complexity
just by using its index number.
• 7. No Index Out of Bounds Checking
• There is no index out-of-bounds checking in C++
• This is due to the fact that C++ does not do bounds checking.
• Languages like Java and python have bounds checking so if you try to
access an out of bounds element, they throw an error.
• C++ design principle was that it shouldn't be slower than the equivalent
C code, and C doesn't do array bounds checking.
Arrays - Advantages
• Random and fast access of elements using the array index.
• Use of fewer lines of code as it creates a single array of multiple
elements.
• Traversal through the array becomes easy using a single loop.
• Sorting becomes easy as it can be accomplished by writing fewer
lines of code.
Arrays - Disadvantages
• Allows a fixed number of elements to be entered which is decided
at the time of declaration.
• Unlike a linked list, an array in C++ is not dynamic.
• Insertion and deletion of elements can be costly since the
elements are needed to be rearranged after insertion and
deletion.