Array Notation in C++
In C++, array notation refers to the way we declare, access, and use arrays using square brackets [].
In C++, array notation refers to the way we declare, access, and use arrays using square brackets [].
1. Declaration of an Array
data_type array_name[size];
Example:
int marks[5];
Here:
int → data type
marks → array name
5 → number of elements
2. Initialization of an Array
(a) At the time of declaration
int a[5] = {10, 20, 30, 40, 50};
(b) Without specifying size
int a[] = {1, 2, 3, 4};
(c) Partial initialization
int a[5] = {1, 2};
Remaining elements become 0.
3. Accessing Array Elements (Array Notation)
Array elements are accessed using index notation:
array_name[index];
Example:
cout << a[0]; // prints first element
Note: Index starts from 0 and ends at size - 1.
4. Storing Values Using Array Notation
for(int i = 0; i < 5; i++)
{
cin >> marks[i];
}
5. Printing Array Elements
for(int i = 0; i < 5; i++)
{
cout << marks[i] << " ";
}
6. One-Dimensional Array Example
#include <iostream>
using namespace std;
int main()
{
int a[3] = {10, 20, 30};
cout << a[1]; // Output: 20
return 0;
}
7. Two-Dimensional Array Notation
int mat[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Access element:
mat[1][2]; // value is 6
Array Declaration in C++
An array in C++ is a collection of elements of the same data type stored in contiguous memory locations.
General Syntax
data_type array_name[size];
Examples of Array Declaration
1. Integer Array
int a[5];
Declares an array a that can store 5 integers.
2. Float Array
float marks[10];
Declares an array to store 10 float values.
3. Character Array
char name[20];
Declares an array to store 20 characters.
4. Declaration with Initialization
int num[4] = {10, 20, 30, 40};
5. Declaration Without Size
int num[] = {1, 2, 3, 4, 5};
The compiler automatically decides the size.
6. Partial Initialization
int num[5] = {1, 2};
Remaining elements are initialized to 0.
7. Two-Dimensional Array Declaration
int matrix[3][3];
With initialization:
int matrix[2][2] = {{1, 2}, {3, 4}};
Array Initialization in C++
Array initialization in C++ means assigning values to array elements at the time of declaration or later in the program.
1. Initialization at the Time of Declaration
int a[5] = {10, 20, 30, 40, 50};
2. Initialization Without Specifying Size
int a[] = {1, 2, 3, 4};
Compiler automatically calculates the size.
3. Partial Initialization
int a[5] = {10, 20};
Remaining elements are initialized to 0.
4. Initialization Using Loop
int a[5];
for(int i = 0; i < 5; i++)
{
a[i] = i * 2;
}
5. Character Array Initialization
char name[6] = {'I', 'N', 'D', 'I', 'A', '\0'};
Or using string literal:
char name[] = "INDIA";
6. Two-Dimensional Array Initialization
int mat[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
7. Initializing All Elements with Zero
int a[10] = {0};
Processing with Array in C++
Processing with arrays in C++ means performing operations on array elements such as input, output, searching, sorting, updating, and calculations using loops.
1. Input (Reading) Elements into an Array
int a[5];
for(int i = 0; i < 5; i++)
{
cin >> a[i];
}
2. Output (Displaying) Array Elements
for(int i = 0; i < 5; i++)
{
cout << a[i] << " ";
}
3. Finding Sum of Array Elements
int sum = 0;
for(int i = 0; i < 5; i++)
{
sum += a[i];
}
4. Finding Maximum and Minimum Element
int max = a[0], min = a[0];
for(int i = 1; i < 5; i++)
{
if(a[i] > max) max = a[i];
if(a[i] < min) min = a[i];
}
5. Searching an Element (Linear Search)
int key, pos = -1;
cin >> key;
for(int i = 0; i < 5; i++)
{
if(a[i] == key)
{
pos = i;
break;
}
}
6. Sorting Array (Ascending Order – Bubble Sort)
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4 - i; j++)
{
if(a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
7. Updating Array Elements
a[2] = 100; // Change value at index 2
8. Counting Even and Odd Numbers
int even = 0, odd = 0;
for(int i = 0; i < 5; i++)
{
if(a[i] % 2 == 0)
even++;
else
odd++;
}
Arrays and Functions in C++
In C++, arrays can be passed to functions so that operations can be performed on multiple values efficiently.
1. Passing Array to a Function
When an array is passed to a function, its base address is passed, not a copy of the array.
Syntax
return_type function_name(data_type array_name[], int size);
2. Example: Display Array Elements
#include <iostream>
using namespace std;
void display(int a[], int n)
{
for(int i = 0; i < n; i++)
cout << a[i] << " ";
}
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
display(arr, 5);
return 0;
}
3. Example: Sum of Array Elements
int sum(int a[], int n)
{
int s = 0;
for(int i = 0; i < n; i++)
s += a[i];
return s;
}
4. Passing Two-Dimensional Array to Function
For 2D arrays, the number of columns must be specified.
void show(int a[][3], int r)
{
for(int i = 0; i < r; i++)
for(int j = 0; j < 3; j++)
cout << a[i][j] << " ";
}
5. Modifying Array Inside Function
Changes made inside the function affect the original array.
void update(int a[], int n)
{
for(int i = 0; i < n; i++)
a[i] *= 2;
}
6. Array as Function Return Type
A function cannot directly return an array, but it can return:
- Pointer to array
- vector
- Dynamically allocated array
Example using vector:
#include <vector>
vector<int> getArray()
{
return {1, 2, 3, 4};
}
Important Points
- Array name acts as a pointer when passed to function.
- Size of array should be passed separately.
- No copy of array is created.
- Efficient for handling large data.
Multidimensional Arrays in C++
A multidimensional array in C++ is an array that contains more than one dimension. The most commonly used multidimensional array is the two-dimensional array, which is used to store data in row–column (table or matrix) form.
1. Declaration of Multidimensional Array
Two-Dimensional Array
data_type array_name[rows][columns];
Example:
int a[3][4];
2. Initialization of Two-Dimensional Array
(a) Row-wise Initialization
int a[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
(b) Single-line Initialization
int a[2][3] = {1, 2, 3, 4, 5, 6};
3. Accessing Elements
a[row_index][column_index];
Example:
cout << a[1][2]; // Output: 6
4. Input and Output of 2D Array
Input
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
cin >> a[i][j];
}
Output
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 3; j++)
cout << a[i][j] << " ";
cout << endl;
}
5. Passing Multidimensional Array to Function
void display(int a[][3], int rows)
{
for(int i = 0; i < rows; i++)
for(int j = 0; j < 3; j++)
cout << a[i][j] << " ";
}
Note: Number of columns must be specified.
6. Example Program: Matrix Addition
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int c[2][2];
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
c[i][j] = a[i][j] + b[i][j];
}
7. Three-Dimensional Array (Brief)
int a[2][3][4];
- Used in advanced applications like graphics and simulations.
Important Points
- Stores data in tabular form.
- Index starts from 0.
- Uses nested loops.
- Memory is allocated contiguously row-wise.
Character Array in C++
A character array in C++ is an array of characters used to store strings. It ends with a null character '\0', which marks the end of the string.
1. Declaration of Character Array
char name[20];
2. Initialization of Character Array
(a) Character-by-Character Initialization
char city[6] = {'D', 'E', 'L', 'H', 'I', '\0'};
(b) String Literal Initialization
char city[] = "DELHI";
Note:- Compiler automatically adds '\0'.
3. Input of Character Array
Using cin
char name[20];
cin >> name;
Note:- Reads input only up to space.
Using cin.getline()
cin.getline(name, 20);
Note:- Reads full line including spaces.
4. Output of Character Array
cout << name;
5. String Handling Functions (<cstring>)
Function Purpose
strlen(str) Finds length
strcpy(a, b) Copies string
strcat(a, b) Concatenates
strcmp(a, b) Compares
Example:
#include <cstring>
cout << strlen(name);
6. Example Program
#include <iostream>
using namespace std;
int main()
{
char name[20];
cout << "Enter name: ";
cin.getline(name, 20);
cout << "Name: " << name;
return 0;
}
7. Difference: Character Array vs String Class
Character Array string Class
Fixed size Dynamic size
Needs '\0' Automatic
Uses <cstring> Uses <string>
Important Points
- Last character must be '\0'.
- Size should be one more than number of characters.
- Suitable for low-level string handling.

0 comments
Post a Comment