


Khusboo Tayal
An array is a collection of elements of the same data type, stored in contiguous memory locations.
data_type array_name[size];
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // An array of 5 integers
// Accessing elements using index
cout << "First element: " << numbers[0] << endl;
cout << "Last element: " << numbers[4] << endl;
return 0;
}
First element: 10 Last element: 50
A 1D array is a list of elements stored in a single row.
#include <iostream>
using namespace std;
int main() {
int marks[4]; // Declaration of an array
// Storing values in the array
marks[0] = 85;
marks[1] = 90;
marks[2] = 78;
marks[3] = 88;
// Displaying array elements
for (int i = 0; i < 4; i++) {
cout << "Marks[" << i << "] = " << marks[i] << endl;
}
return 0;
}
Marks[0] = 85 Marks[1] = 90 Marks[2] = 78 Marks[3] = 88
A multi-dimensional array is an array of arrays.
A 2D array can be visualized as a table with rows and columns.
data_type array_name[rows][columns];
#include <iostream>
using namespace std;
int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
// Displaying 2D array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
1 2 3 4 5 6
Character arrays are used to store sequences of characters, also known as C-style strings.
char string_name[size];
#include <iostream>
using namespace std;
int main() {
char name[10] = "Hello";
cout << "String: " << name << endl;
return 0;
}String: Hello
You can initialize arrays in several ways:
int numbers[5] = {1, 2}; // Remaining elements will be 0
int values[] = {10, 20, 30}; // Size will be automatically set to 3
#include <iostream>
using namespace std;
int main() {
int arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = i * 10;
}
for (int i = 0; i < 5; i++) {
cout << "arr[" << i << "] = " << arr[i] << endl;
}
return 0;
}
arr[0] = 0 arr[1] = 10 arr[2] = 20 arr[3] = 30 arr[4] = 40
Strings are sequences of characters. C++ provides two main ways to work with strings:
These are arrays of characters ending with a null character ('\0').
#include <iostream>
using namespace std;
int main() {
char text[] = "Welcome to C++";
cout << "Message: " << text << endl;
return 0;
}Message: Welcome to C++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
strcat(str1, str2); // Concatenation
cout << "Concatenated String: " << str1 << endl;
cout << "Length of str1: " << strlen(str1) << endl;
return 0;
}Concatenated String: HelloWorld Length of str1: 10
C++ provides the string class in the <string> library, making string manipulation easier.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Khusboo";
cout << "Name: " << name << endl;
cout << "Length: " << name.length() << endl;
name += " Tayal";
cout << "Full Name: " << name << endl;
return 0;
}Name: Khusboo Length: 7 Full Name: Khusboo Tayal
Strings can also be stored in multi-dimensional arrays, useful for handling lists of strings.
#include <iostream>
#include <string>
using namespace std;
int main() {
string students[3] = {"Alice", "Bob", "Charlie"};
for (int i = 0; i < 3; i++) {
cout << "Student " << (i + 1) << ": " << students[i] << endl;
}
return 0;
}Student 1: Alice Student 2: Bob Student 3: Charlie
In this tutorial, you learned: