Topic: Array in C++ Programming
Course:
Programming with C++
�Audience:
Beginners / HSC / Diploma / University Intro
�Prepared by:
MD. SAKIB HOSSAIN, DEPT. OF CSE,
VARENDRA UNIVERSITY
Agenda
01
What is an Array?
02
Why need?
03
Types
04
Declaration & Definition
05
Input & Output
07
Advantages & Limitations
06
Examples
Understanding C++ Arrays: A Core Data Structure
DEFINITION
FUNDAMENTALS
What is an Array?
Key Characteristics
Why Use Arrays in C++?
PURPOSE
EFFICIENCY
Efficient Data Storage
A teacher needs to store the marks of 50 students after an exam. Using an array, all students’ marks can be saved in one place and accessed easily. This helps in calculating total, average, and highest marks quickly.
Fast Access Times
Direct access to elements by index (O(1) time complexity) makes arrays ideal for scenarios requiring quick data retrieval or modification.
Structured Data Organization
They enable logical grouping of similar data, simplifying code and making it more readable and maintainable.
Exploring Array Types in C++
TYPES
CLASSIFICATION
Single-Dimensional Arrays: The Basics
1D ARRAY
Linear Array
01
Declaration
Declare a 1D array by specifying its data type, name, and size.
For example: int numbers[5];
02
Initialization
Initialize elements during declaration:
int arr[] = {1, 2, 3}; or assign values later: arr[0] = 10;
03
Accessing Elements
Access elements using their index within square brackets:
cout << arr[0]; (prints 10).
Example: Working with a 1D Integer Array
CODE EXAMPLE
1D ARRAY
This simple C++ code demonstrates how to declare, initialize, and iterate through a one-dimensional integer array.
#include <iostream>��int main() {� int ages[5] = {22, 35, 19, 48, 29};�� std::cout << "Ages of people:" << std::endl;� for (int i = 0; i < 5; ++i) {� std::cout << "Age at index " << i << ": " << ages[i] << std::endl;� }� return 0;�}�
Output:
Ages of people:�Age at index 0: 22�Age at index 1: 35�Age at index 2: 19�Age at index 3: 48�Age at index 4: 29�
Code:
#include <iostream>
using namespace std;
int main() {
int marks[5];
// Input marks using loop
for(int i = 0; i < 5; i++) {
cout << "Enter marks of student " << i + 1 << ": ";
cin >> marks[i];
}
// Output marks using loop
for(int i = 0; i < 5; i++) {
cout << "Marks of student " << i + 1 << ": " << marks[i] << endl;
}
return 0;
}
Two-Dimensional Arrays: The Basics
2D ARRAY
R & C
01
Declaration
Declare a 2D array by specifying its data type, name, and size(row & column).
For example: int numbers[3][3];
02
Initialization
Initialize elements during declaration:
int a[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
03
Accessing Elements
Access elements using their index within square brackets:
cout << a[0][1];
Example: Handling a 2D Array (Matrix)
CODE EXAMPLE
2D ARRAY
This example demonstrates the declaration, initialization, and traversal of a two-dimensional array representing a 3x3 matrix.
#include <iostream>��int main() {� int matrix[3][3] = {� {1, 2, 3},� {4, 5, 6},� {7, 8, 9}� };�� std::cout << "Matrix elements:" << std::endl;� for (int i = 0; i < 3; ++i) {� for (int j = 0; j < 3; ++j) {� std::cout << matrix[i][j] << " ";� }� std::cout << std::endl;� }� return 0;�}�
While C++ supports arrays with more than two dimensions (e.g., 3D arrays), they are less common and can become complex to manage.
2D array input using loop:
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
cin >> array[i][j];
2D array output using loop:
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
cout<< array[i][j];
This simple C++ code demonstrates how to declare, initialize, and iterate through a two-dimensional integer array.
�
#include <iostream>
using namespace std;
int main() {
int a[3][3];
// Input using nested loop
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cin >> a[i][j];
}
}
// Output using nested loop
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
Advantages of Array
Limitations of Array