
Array in C++ programming is used to store multiple values in a single variable of similar kind in a contiguous manner. The memory locations in the array are next to each other. For example, the names of boys and girls can be listed in an array together. Consider the array as names. Let us understand arrays in C++ programming in detail.
| Array in C++ Programming |
| int my_Array [5]; //Here, the square bracket holds the size of array // int is the data type //my_Array is the name of array. //We can assign values to our array during or after the declaration. |
Highlights:
- Understanding Arrays in C++: Arrays in C++ are introduced as data structures used to store and manipulate multiple values of similar data types in a single variable. They are stored in contiguous memory locations, making it efficient to access and manipulate array elements.
- Declaration of Arrays: The process of declaring arrays in C++ is explained, emphasizing the use of square brackets to specify the size of the array. Arrays must be declared with a constant expression for their size, evaluated at compile time.
- Types of Arrays in C++:
- Single-Dimensional Array: Single-dimensional arrays are highlighted as simple structures used to store a list of values of the same data type. An example code snippet demonstrates the declaration and initialization of a single-dimensional array to store names of top rankers in a class.
- Multi-Dimensional Array: Multi-dimensional arrays, particularly 2D arrays, are explained as structures to represent data in multiple dimensions. An example code snippet showcases the declaration, initialization, and printing of elements in a 2D array.
- Practical Examples: The content provides practical examples of both single-dimensional and multi-dimensional arrays in C++, offering code snippets and explanations to demonstrate their usage and functionality.
- Learning Opportunities: The content concludes by highlighting learning opportunities through a C++ with DSA Course offered by PW Skills. The course covers programming with C++, data structures, algorithms, and STLs, providing learners with real-time projects, assessments, quizzes, exercises, and course completion certificates. It emphasizes frequent doubt support, 100% placement assistance, and encourages learners to visit the official website for more information.
| One Dimensional Array in C++ Programming |
| Data_type name_of_Array [ Size of array] |
| Two Dimensional Array in C++ Programming |
| Data_type name_of_Array [ Number of rows] [ Number of columns] |
| Array in C++ Programming |
| #include <iostream> #include <conio.h> using namespace std; void main() { string rankers_Name[4]; //names array used to store the name of top 3 rankers in a class rankers_Name[0] = “Ankit”; // manually entering in array rankers name rankers_Name[1] = “Priya”; rankers_Name[2] = “Shubham”; //using a loop to display the names of three rankers in the class. for (int i = 0; i<rankers_Name.size(); i++) { cout<< “Student at Rank”, [i+1], “ is:”, rankers_Name[i]<<endl; } |
| Array in C++ Programming |
| #include <iostream> using namespace std; int main() { // Declaring and initialising a 2D array int multi_Array[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Accessing and printing elements of the 2D array cout << "The elements in the matrix are as follows:" << endl; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { std::cout << multi_Array[i][j] << " "; } std::cout << std::endl; } return 0; } |
Output
| The elements in the matrix are as follows: 1 2 3 4 5 6 7 8 9 |