Array cpp: C plus plus is a popular programming language that consists of many data structures. Arrays are simple and traditional data structures used to store multiple values same type of elements in a single variable. It is a frequently used data structure used in various types of tasks.
For example, if we want to store a list of students who passed the examination we can do it easily by declaring a variable rankers_list and storing rankers name accordingly. We can also access rankers_name with their index value. Let us know the steps to declare and initialize an array in C plus plus.
What are Arrays CPP?
Arrays are simple data structures used to store a list of values of similar data types together. It stores the element in a contiguous memory space which means linearly storing data one by one. Array cpp is frequently used by programmers and developers worldwide and are effective means of storing and manipulating values easily.
We can easily insert, remove, search, and modify elements kept in an array. However, C plus plus arrays are statically typed and must be allocated during compile time. It also consists of a fixed size and cannot be changed after declaration. Let us learn how to declare and initialize an array in C++.
For example, check an array consisting of string value of list of top 5 rankers in a class. Consider the name of array as ‘topRankers’
string topRanker [5] = { “Ankit”, “Aditi”, “Shreyash”, “Riya”, “Ujjawal” }
Ankit | Aditi | Shreyash | Riya | Ujjawal |
We will check more about these topRanker array and learn how to initalize and declare this array. We will also learn how to access these elements easily.
Highlights:
- Arrays in C++: Arrays are fundamental data structures used to store multiple values of the same data type in a contiguous memory space. They are widely utilized by programmers worldwide for storing, accessing, and manipulating data efficiently.
- Initialization and Declaration: In C++, arrays must be declared with their data type, name, and size. They can be initialized during declaration by assigning values using curly braces, or values can be assigned manually or through loops after declaration.
- Properties of Arrays in C++: Arrays in C++ have several key properties, including storing elements at contiguous memory locations, starting indexing from zero, fixed size after declaration, requirement for elements to be of the same data type, and support for multi-dimensional arrays.
- Accessing and Updating Elements: Elements in arrays can be accessed using their index numbers, with indexing starting from zero. Updating elements involves assigning new values to specific indices using the assignment operator.
- Learning Opportunities: Individuals interested in software development can benefit from mastering C++ and understanding data structures and algorithms. Courses like “Decode C++ with DSA” offered by PW Skills provide comprehensive learning experiences with industry experts, real-world projects, practice material, access to labs, placement assistance, and job exposure.
Are Arrays still in Use?
Arrays are widely used fundamental data structures because of its ease of storing, accessing data, manipulating them effectively. We can easily store a list of items in a single name variable which can easily be accessed using their index number. They are inplace data structures and can store data in a more efficient way compared to other data structures. We will learn intializing arrays, declaring them using an example in this article.
Properties of Array Cpp
Arrays in C++ is one of the most widely used programming language by developers around the world. Let us know some of its major properties in this aritcle.
- Array stores collection of similar data types elements at a contiguous memory location.
- In C++ indexing elements start from zero which means our first element will be stored at 0th index.
- Elements in array can be accessed using their index value.
- The size of array after declaration can not be changed. Its size remain constant throughout the program.
- Array can be declared in multiple dimensions like 1D, 2D, and more.
- Number of elements can be found using the size of operator under square bracket.
- The elements in array are stored at contiguous memory location.
- All elements in an array must be of same data type. You cannot store elements of different types such as char and float together.
Initialization and Declaration of Array Cpp
In C++ array program is saved using cpp extension. Let us now learn how to initialize array in C plus plus.
The topRankers array consists of top five rankers from class. We will learn to intilaize and declare them using Array Cpp.
Array Cpp: Initialization and Declaration of Array in C++ |
Data_Type name_of_Array [Size of array]// string topRanker [5]
// Here, the data type of array is String (string). Hence, it consists of string elements only. // Name of the array is topRankers // Size of array inside square bracket, i.e. 5 |
string topRanker [5] = { “Ankit”, “Aditi”, “Shreyash”, “Riya”, “Ujjawal” ;
topRanker[0] = Ankit // Rank 1
topRanker[1] = Aditi // Rank 2
topRanker[2] = Shreyash //Rank 3
topRanker[3] = Riya //Rank 4
topRanker[4] = Ujjawal //Rank 5
In C++ array indexing start from zero and hence the total size say N is taken as N-1. Here, in this example also ranking starting from the 0th element. The first ranker “Ankit” is hence stored at 0th index of the array topRanker.
Ankit | Aditi | Shreyash | Riya | Ujjawal |
To declare arrays in C++ we need to keep three major things in mind given.
- Data type of array must be explicitly mentioned at the time of declaration.
- Name of the array.
- Size of array
For Example:
string topRanker [5];
In C plus plus, array must be intialized during declaration as they are not dynamic and must be intialized during compile time. We must assign the value just after declaring the array. There are two methods of declaring array cpp. We can assign the value during declaration or just after declaration.
Initaliziaton of Array During Declaration
Check the initialization of array inline during declaration of array below.
Array Cpp: Initialization with Declaration of Array in C++ |
string topRanker [5] = { “Ankit”, “Aditi”, “Shreyash”, “Riya”, “Ujjawal” } |
You can intilalize array in C++ by assigning elements in array using curly braces with separated by commas.
Initialization of Array After Declaration
In this method values in array are assigned after declaration manually or using loops.
Array Cpp: Initialization after Declaration of Array in C++ |
string topRanker [5];
topRanker = “Ankit”; topRanker = “Aditi”; topRanker = “Shreyash”; topRanker = “Riya”; topRanker = “Ujjawal”; |
Array Cpp: Intialize Array with Values and Without Size in C++
We can try some variation while declaring array. In C++ array we can assign value without mentioning the size in square brackets. C++ array automaticall picks up the size based on the number of elements in the array after curly bracket get closed.
Array Cpp |
string topRanker [ ] = { “Ankit”, “Aditi”, “Shreyash”, “Riya”, “Ujjawal” } |
Here, topRanker array will automatically pick up the size of array by elements stored in the array. The length of array will be equal to the number of elements stored in the array.
Array Cpp: Initalize Array using Loops
We can also initalize our array cpp using loops after declaration of array in C++. Let us understand it with the help of same example of “topRanker” below.
Array Cpp:Intialize Array using Loops in C++ |
#include <iostream>
#include <string> using namespace std; int main() { string topRanker[5]; int len = 5; // Assuming len is defined elsewhere or set to a specific value for (int i = 0; i < len; i++) { cout << “Enter the name of Ranker ” << (i + 1) << “: “; string ranker; cin >> ranker; topRanker[i] = ranker; } // Displaying the entered names cout << “Top 5 Rankers:” << endl; for (int i = 0; i < len; i++) { cout << topRanker[i] << endl; } |
Array Cpp: Intialize Array Cpp with Zero in C++
If we have not yet decided with which elements to fill in our array then we can also intialize our array cpp using zeroes and assign the values later on.
Array Cpp: Intilization |
string topRanker [ ] = {0}; |
Here in the array topRanker array elements at all index will be stored with element zero. We can assign values at all index later on.
Array Cpp: Accessing Elements in Array C++
We can easily elements in array in C++ using their index number. We can square operator to access elements in array. Using the above example array ‘topRanker’ to access the elements in array in C++.
Array Cpp: Accessing Elements in Array |
//Accessing elements from array topRanker using their index number below.
cout<<”Ranker”<<(i+1)<<topRanker[0]<<endl; cout<<”Ranker”<<(i+1)<<topRanker[2]<<endl; cout<<”Ranker”<<(i+1)<<topRanker[4]<<endl; //output Ankit Shreyash Ujjawal |
Array Cpp: Update Element of Array
We can easily update array cpp with the help of their index number. We can assign new values on the right hand side of assign operator. Check the syntax below to update element of array.
Array Cpp: Update Element of Array |
Data_type name_of_Array [index number] = new value |
In the table, index number number is the position on which new value has to be updated. Also ‘new value’ is the value which will replace the original value of array.
Learn Data Structure with PW Skills
If you want to start your career as software developer then you must master one programming language and have knowledge of data structures and algorithm. Enrol in our Decode C++ with DSA Course to get an all in package at a very affordable cost to make you job ready.
Throughout the course top industry experts will guide you and help you learn with real time industry level projects. The course is fully loaded with courses and study material to help you pracitce thoroughly. Get free access to PW Skills Lab. Get 100% placement assistance and job exposure after completing the course. There is much more to this course get it at @pwskills.com.
Array CPP FAQs
What are array in Cpp?
Arrays are simple data structures used to store a list of values of similar data types together. It stores the element in a contiguous memory space which means linearly storing data one by one.
How to declare an array in cpp?
Arrays in array is declared using data type, name of array and size of the array. However, there are many more ways of declaring and initializing array in cpp, check them in this article.
Why do we use array?
Array is used to store multiple similar data type elements in a single variable. It is used to store significant amount of similar data type.
What is called array?
Array is a group of similar elements together in a single variable at contiguous memory location.