0% found this document useful (0 votes)
34 views26 pages

Lecture 8 CP Arrays 26092023 104704am

This document provides a summary of arrays in C++. It defines arrays as fixed-size collections of similar data items stored contiguously in memory. Arrays allow for fast random access of elements using indexes and can store primitive and user-defined data types. The document discusses array declaration, initialization, accessing and updating elements, and one-dimensional and multi-dimensional arrays. It also covers array properties and advantages/disadvantages of the data structure.

Uploaded by

armarking654
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views26 pages

Lecture 8 CP Arrays 26092023 104704am

This document provides a summary of arrays in C++. It defines arrays as fixed-size collections of similar data items stored contiguously in memory. Arrays allow for fast random access of elements using indexes and can store primitive and user-defined data types. The document discusses array declaration, initialization, accessing and updating elements, and one-dimensional and multi-dimensional arrays. It also covers array properties and advantages/disadvantages of the data structure.

Uploaded by

armarking654
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Lecture – 8

Lecture Slides for (Computer Programming CSC-113)


Taught By: Junaid Shah
Arrays - Meaning
• ‫صف آرائی‬/‫صف‬

• An impressive display or range of a particular type of thing.


• "there is a vast array of literature on the topic"

• An ordered series or arrangement.


• "several arrays of solar panels will help provide power"
Arrays
• It is a fixed-size collection of similar data items stored in
contiguous memory locations.

• It is one of the most used data structures in C++ programming.

• It is a simple and fast way of storing multiple values under a single


name.
Arrays
• It can be used to store the collection of primitive data types:
• int, char, float, etc.
• As well as derived and user-defined data types:
• pointers, structures, etc.
Arrays - Declaration
• Arrays are declared like any other variable before using it.
• We can declare an array by specifying its name, the type of its
elements, and the size of its dimensions.
• When we declare an array in C++, the compiler allocates the
memory block of the specified size to the array name.
• Syntax:
data_type array_name [size];
int Arr [5];
OR/
int Arr[]={};
Arrays - Initialization
• When array is declared or allocated memory, the elements of the
array contain some garbage value.
• There are multiple ways in which we can initialize an array in C++.
Arrays - Initialization
• There are multiple ways in which we can initialize an array in C++.

• 1. Array Initialization with Declaration:


• At the time of initialization, array is declared.
• Initializer list is the list of values enclosed within braces { },
separated by a comma.

• data_type array_name [size] = {value1, value2, ... valueN};


• int Arr[5] = {3, 9, 2, 11, 5}
Arrays - Initialization
• 2. Array Initialization without Size
• We can initialize array without providing fixed size of the array.
• In this case the compiler automatically assign size of array by
counting total number of elements in the array.

• data_type array_name[] = {1,2,3,4,5};

• The size of the above arrays is 5 which is automatically deduced by


the compiler.
Arrays - Initialization
• 3. Array Initialization after Declaration (Using Loops)
• We initialize the array after the declaration by assigning the initial
value to each element individually.
• We can use for loop, while loop, or do-while loop to assign the
value to each element of the array.

for (int i=0; i<n; i++) {


array_name[i] = i;
}
Arrays - Initialization - Example
#include <iostream>
using namespace std;
int main(){
int arr_1[5] = { 10, 20, 30, 40, 50 }; // array initialization using initializer list
int arr_2[] = { 1, 2, 3, 4, 5 }; // initializer list without specifying size
float arr_3[]={}; // array declaration as null
// array initialization/filling using for loop
for (int i=0; i<5; i++) {
arr_3[i] = i * 2.5;
cout << arr_3[i] << endl;
}
return 0;
}
Arrays - Access Elements
• We can access any element of an array using:
• array subscript operator [ ]
• and index value i of the element
• Note: indexing in the array always starts with 0, i.e., the first element is at
index 0 and the last element is at N – 1 where N is the number of elements
in the array.
• int Arr[5] = {15,25,35,45,55};
• array_name [index];
• Arr[0];
Arrays - Access Elements - Example
#include <iostream>
using namespace std;
int main(){
int arr[5] = { 15, 25, 35, 45, 55 }; // array declaration and initialization
// accessing element at index 2 i.e 3rd element
cout << "Element at Arr[2]: " << Arr[2] << endl;
// accessing element at index 4 i.e last element
cout << "Element at Arr[4]: " << Arr[4] << endl;
// accessing element at index 0 i.e first element
cout << "Element at Arr[0]: " << Arr[0] << endl;
return 0;
}
Arrays - Update Array Element
• We can update the value of an element at the given index i
• It is similar to accessing an element by using the array subscript
operator [ ]
• And assignment operator =

• 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

• 1. One Dimensional Array


• The One-dimensional arrays, also known as 1-D arrays in C++ are those
arrays that have only one dimension.

• 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy