Arrays In C++ Programming Language


Arrays In C++ Programming Language

In todays blog post we are going to cover a basic data structure used in C++ programming language, which is known as arrays. So what is arrays and what is the use of arrays in C++ programming language. Array is a basic linear data structure. And it is a collection of elements of same data type and these elements or values are stored at the contiguous memory locations or addresses. And we can access the elements of array by using the index of any specific element or value with in the array body. If at this point things are not clear to you, don't need to worry we will cover the examples in C++ programming language that will help you all in understanding the concept of arrays C++ programming.

If we talk about the main use of arrays in C++ programming language, which is, instead of declaring separate or multiple variables for each value, we can use arrays to store multiple related values under a single variable name. We can say that arrays are collection of related data items that are stored in adjacent or contiguous memory locations. And by using arrays we can store collection of  data of any specific primitive data type, like int, float, double, char, etc. We can also store more complex data types in an array, lke structures, pointers, etc. And before diving deep into array keep an important point in mind that the starting index of arrays is always 0 (zero).


 


Basic syntax and working of arrays in C++ programming language

Now we are going to look at the different ways of declaring and initializing arrays in C++ programming language, and also the syntax of array declaration.

1) Default declaration (Uninitialized)

int arr[5]; // Array of 5 integers.


2) Full initialization with size

int arr[5] = { 10, 20, 30, 40, 50 };


3) Implicit size (Compiler determined)

int arr[] = { 1, 2, 3, 4 };


4) Partial initialization

int arr[5] = { 10, 20 }; // Equal to { 10, 20, 0, 0, 0 }


5) Initialize all elements to zero

int arr[5] = { 0 };


6) C++11 Uniform Initialization (List initialization)

int arr[3]{ 1, 2, 3 };


7) Element by element index assignment

int arr[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;


Working of arrays in C++ programming language

Now we will look at the basic working of arrays and why we need arrays. In C++ programming language we usually declare variables of type int, float, string, etc. And these variables usually store single values in them. Now assume if we want to store the marks of student, studying 10 subjects then we can either create 10 integer variables and store the values in them. So in such situations we use arrays to store the value of 10 subjects in an array variable of size 10 and data type int, and thus values of 10 subjects will be stored in single  array variable of size 10. And just keep this important point in mind that this single array variable will store values of same data type. And the starting index of array variable always starts with 0 index.


Simple coding examples of arrays in C++ programming language

C++ coding example 1

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int numbers[4] = { 13, 34, 56, 76 };
	cout << "Traversing values of array using for loop" << endl;
	for (int i = 0; i < 4; i++)
	{
		cout << numbers[i] << endl;
	}
	_getch();
}


Explanation of code 1

In the above C++ coding example we have implemented the concept of arrays. In the main() method we have declared and initialized an integer array of size 4 like this, 

int numbers[4] = { 13, 34, 56, 76 };

we simply write the int key word and after that we write the name of array variable as numbers followed by opening and closing square brackets and the size of the array between these square brackets. After that we have initialized four values with in the opening and closing curly brackets, and assigned these values to numbers variable.

After that we have simply used the for loop to traverse through the array numbers with the help of for loop variable i with starting value as 0 (zero). It is important to know that we pick the value 0 for the i variable in the for loop. It is because of the reason that the starting index of numbers variable is 0 because it is an array variable. And with in the for loop body we simply print the numbers to console screen using cout<< statement like this, 

for (int i = 0; i < 4; i++)
{
	cout << numbers[i] << endl;
}

And remember in order to show the values of array variable, we write array name followed by opening and closing square brackets and put variable i between the square brackets. As the values of i will change, it will print the values with in numbers variable. 


C++ coding example 2

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	string cars[3] = { "Honda", "Toyota", "Suzuki" };
	cout << "Traversing values of array using for loop" << endl;
	for (int i = 0; i < 2; i++)
	{
		cout << cars[i] << endl;
	}
	_getch();
}


Explanation of code 2

In the second coding example we have used array variable of type string and named it as cars with array size equal to 3 like this,

string cars[3] = { "Honda", "Toyota", "Suzuki" };

In this coding example we simply declared and initialized the cars variable with string values and after that we again used the for loop to traverse through the cars variable which is array of strings like this, 

for (int i = 0; i < 2; i++)
{
	cout << cars[i] << endl;
}

And the rest of the programming logic is same as that explained in the previous code explanation.

Congratulations! like always we have successfully implemented arrays in C++ programming language.


Comments

Popular posts from this blog

How to Download and Install Microsoft visual studio community 2022 for free

For Loop In C++ Programming Language

Hello World Program In C Programming Language