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.
What is Array in C++ Programming?
Array is a data structure in C++ that is used to store and manipulate multiple values of similar data types in a single variable at contiguous memory location. The easiest way to declare an array is to use the data type with square bracket and name of the array. Check the declaration of array in C++ below.
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. |
The size of the array must be declared in a constant expression and evaluated at compile time rather than during run time. The maximum size of the array depends on the available memory on your system. The memory locations are arranged one next to the other rather than randomly, like a linked list.
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.
Declaring Array in C++ Programming
Arrays in C++ are declared using square brackets, with size of the array given inside the bracket. The maximum size of array is limited based on the memory space available in the system. Check the syntax below to declare one dimensional array and two dimensional array in C++
One Dimensional Array in C++ Programming |
Data_type name_of_Array [ Size of array] |
“Data_type” includes the data type of the elements to be stored inside the array.
“name_of_Array” holds the name of the given array. It is user defined.
“size of array” holds the length of the array
Two Dimensional Array in C++ Programming |
Data_type name_of_Array [ Number of rows] [ Number of columns] |
The square brackets in the 2D array hold the number of rows and columns in the array. If the value of each row and column is one, then the array consists of a single element. For example: int my_Array [1][1] = {2}. The array ‘my_Array’ consists of only single element i,e. 2
Types of Array in C++ Programming
There are two types of array in C++ programming, as given below.
1. Single-Dimensional Array
The single dimension arrays in C++ are used to store the same data type values in the form of a list. It is one of the simplest forms of an array and is easy to declare and implement. Check out an easy example of a single dimension array in the table below.
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; } |
The program given above consists of three rankers from a class. The names of the rankers are stored inside an array named “ranker_Name”. The names of the rankers are stored at a particular index, which is displayed using a loop. A Single array is also known as a one-dimensional array, where elements are arranged in a linear manner.
2. Multi-dimensional Array
A multi dimensional array in C++ is considered a way to represent multiple dimension data in 2 dimensional, 3 dimensional, and so on. The most frequently used multi dimensional array in C++ is 2D array. Let us understand a multi-dimensional array with the help of an array.
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 |
The given array consists of a total of 3 rows and 3 columns. It is an example of a 2D array. Working in a simple one dimensional array or 2D array is considerably easier. The complexity increases with the dimension of the array.
Learn C++ With PW Skills
Enrol in our special C++ with DSA Course to learn programming with C++. Learn Data Structures, algorithms, and STLs to step into the world of programming. The course is covered by experts who learn with real time projects, self assessments, quizzes, exercises and earning course completion certificate after completing the course.
We will guide you throughout the course with frequent doubt support, 100% placement assistance and much more. If you want to learn more about the course, go to our official website, @pwskills.com
Array in C++ Programming FAQs
What are the types of arrays in C++?
There are two types of arrays in C++ are single dimensional arrays and multidimensional arrays.
What is a 2D array in C++?
A two dimensional array in C++ programming stores values in rows and columns. It is one of the simplest forms of multidimensional arrays in C++. It is also known as matrix.
What is the syntax of an array in C++?
Arrays in C++ are declared using square brackets, with the size of the array given in the bracket. The syntax of a 2D array is int my_Array[m][n].
What is a one dimensional array?
One dimensional array in C++ is used to store the same data type at a contiguous memory location. It stores values in a linear manner under a single variable.