1 of 13

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

2 of 13

Agenda

01

What is an Array?

02

Why need?

03

Types

04

Declaration & Definition

05

Input & Output

07

Advantages & Limitations

06

Examples

3 of 13

Understanding C++ Arrays: A Core Data Structure

DEFINITION

FUNDAMENTALS

What is an Array?

  • An array is a collection of variables of the same data type

  • An array allows us to store multiple values in a single variable.

Key Characteristics

  • Fixed Size: Once declared, an array's size cannot be changed.

  • Homogeneous: All elements must be of the same data type (e.g., all integers, all floats).
  • Indexed Access: Elements are accessed via an integer index, typically starting from 0.
  • All elements are stored in contiguous memory locations
  • All elements are stored in contiguous memory locations

4 of 13

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.

5 of 13

Exploring Array Types in C++

TYPES

CLASSIFICATION

  • One-Dimensional Array (1D)
  • Two-Dimensional Array (2D)
  • Multi-Dimensional Array

6 of 13

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

7 of 13

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

8 of 13

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;

}

9 of 13

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];

10 of 13

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;�}�

11 of 13

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];

12 of 13

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;

}

13 of 13

Advantages of Array

  • Easy to store multiple values
  • Fast data access
  • Reduces code size
  • Useful in sorting & searching

Limitations of Array

  • Fixed size
  • Cannot change size at runtime
  • Stores only same data type