EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Arrays in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Arrays in C++

Arrays in C++

Introduction to Arrays in C++

Arrays in C++ is a collection of elements of similar data type like int, char, float, double, etc, that are stored using the index value and can easily be accessed by index value only. It implements to store all the instances of variables into one single variable. In C++, an array can be declared using three various methods – by specifying the size of an array, by initializing array elements directly and by specifying arrays size with its elements.

To let the data processed using any application, we first need to bring the data into the application. This means there should be some space in the application where the value should be stored until the program runs. In order to serve the purpose of storing the values, the programming language offers us variable. Variables are used to store the values so that they could be used by the application to generate the expected outcome. As variables store, some value, occupy space in the memory allocated to the application. So the optimal approach of coding is to ensure the usage of the variable should be as low as possible. In order to mitigate the memory allocation issue due to the creation of a high number of variables, the concept of array came into existence. The array can be considered as the list of values that belongs to the same datatype. In this article, we are going to learn about the array using the C++ programming language.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How Do Arrays work in C++?

Below is the explanation of how arrays work:

  • The role of the array is to store the values of the same datatype. It is supposed to work the same way as that of the variable and the only additional point it has over variable is, it is capable of holding several values at the same time. When we create the array in C++ or any of the programming language, we have to state the number of variables that we want to store in the array.
  • It has to be noted that the size of the array remains fixed throughout the application runtime and cannot be changed dynamically. Once the size of the array is defined, we can store the same count of values in it. If the data type of the array is defined as an integer, it will not accept any value that is not an integer. In order to locate the value held by the array one will need to use the index.
  • For instance, if the array is capable of holding two values then the second value will be stored at the one index of array as the index of array begins with zero. In the next section, we will learn array creation.

How to Create Arrays in C++?

Below explanation says that how to create arrays in c++:

The approach of creating the array is exactly similar to variable creation. The first step is to declare the array. Once the array is declared, we can either initialize the array at the same time or it could be initialized later. While declaring the array we have to mention three things: datatype of the array, the name of the array and its size. Below is the syntax that shows how to merely declare the array.

Syntax:

Datatype array_name[size];
Ex. int first_array[10];

The array defined here can ten integer values. The name of the array is first_array and the number defined inside the large bracket states the size of the array. Now let’s see how to declare and initialize the variable at the same time.

Code:

Int first_array[4] = { 1,2,3,4}
Int first_array[]= {1,2,3,4,5,6}

In the above example, we have seen that the array that has defined the size 4 has accepted the 4 values. If one will try to submit more than 4 values, it will throw an error. Also, if you do not specify the size of the variable you can store as much value as you want.

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,899 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Types of Arrays in C++

 

typres of arrayin c++

In C++ programming language we do have mainly two types of variables: Single Dimensional Arrays and multidimensional Arrays. The single-dimensional stores the values hold the values in the form of the list while the multidimensional array store the value in the matrix. Below we will see each of the types using an example.

1. Single Dimensional Array

The single-dimensional array may be defined as the type of array that is capable to hold the values of the same datatype in the form of a list. It is the simplest form of array as it doesn’t require much effort to define and initialize such array. It can be defined as int a[10], where int is the data type is the array name and the size of the array is 10. The example below will make things more clear.

Code:

#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int val_array[3];
int int_val=1,counter;
cout<<"Please enter three numbers that you want to multiply"<<endl;
for(counter=0;counter<3;counter++)
{
cin>>val_array[counter];
int_val = int_val*val_array[counter];
}
cout<<"The multiplication of these three numbers is = "<<int_val;
getch();
}

The above program is written to accept three values from the user and then those values will be processed to generate the multiplication value of all of them.  The array user here is name val_array and the array is capable to hold three values. The loop is used to intake the value in the array and then they were multiplied. The end result of the multiplication has been stored in the int_val variable. As the function is void in nature, it will not return any value.

Output:

single-dimensional array (array in c++)

2. Multidimensional array

The multidimensional array may be defined as the array that holds the values in the way a matrix does. The two-dimensional array is used very often and with the increase in the size of dimension, the array gets complicated. For instance, it is easy to work with a two-dimensional array rather than working with a three-dimensional array. The two-dimensional array needs two sizes to be defined for one dimension each. The two-dimensional array can be in the program as int a[5][3]. This array will be able to hold the value in the form of a matrix that has 5 rows and three columns. Let us understand this with the help of an example.

Code:

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int val_array[5][5];
int count_rows,count_cols,counter1,counter2;
cout<<"Please enter the size of the rows and columns that you wnant to input: ";
cin>>count_rows>>count_cols;
cout<<"PLease enter the values for matrics in row wise manner"<<endl;
for(counter1=0;counter1<count_rows;counter1++)
for(counter2=0;counter2<count_cols;counter2++)
cin>>val_array[counter1][counter2];
cout<<"The matrix will be as follows"<<endl;
for(counter1=0;counter1<count_rows;counter1++)
{
for(counter2=0;counter2<count_cols;counter2++)
cout<<val_array[counter1][counter2]<<"  ";
cout<<endl;
}
getch();
return 0;
}

In this program, we have used a two-dimensional array. The way array was defined using two sizes, it states that the array used is two dimensional. If there would have been three sizes then the array would be three dimensional. The user is asked to input the number of rows and columns they want in the matrix. Once the numbers are specified by the user, they are asked to input the values that they want in the rows and columns of the matrix. Here the user has submitted 2 3 which means they want two rows and three columns in the matrix. Know they had to submit six values as the matrix with two rows and three columns have six values. When all the values have been submitted, they have been represented in the form of a matrix. The entire program is implemented using the two-dimensional array.

Output:

multidimensional array 1

Method of Passing Array

Methods are shown below:

To pass the variable in any method as a parameter, what all it needs to accept the value from where it is called is datatype and the name of the variable that will be holding the Value. The approach of passing the array to the method is similar to the way any normal variable is passed. The only difference is, rather than mentioning a single variable, we will need to use the array with a specific size in the place of array. Let’s understand this using syntax.

Syntax:

//defining method that accepts an array as a parameter.
int handle_array(int a[6]);

Here the method name is handle_array and it has an array as a parameter. The name of array is a and the array is capable of holding six values. Now let’s see how the argument can be passed to the method handle_array.

Syntax:

int arrr[6] = {1,2,3,4,5,6};
handle_array(arr) ;

First, we need to assign the values to other variables that we have to pass to the handle_array method. Once the values are assigned, we need to pass the array as an argument while calling the handle_array function. In the above snippet, it is clear that how the array has been assigned with the values and passed as an argument so that the values could be passed where handle_array function has been defined.

Conclusion

The array in C++ is considered very essential feature as it helps in memory management and also improves the efficiency of the program. It can be used in several algorithms to die to its ability to offer multidimensional data storage. It is always optimal to use an array when there is a need to store values of the same datatype. It does not just help in conserving resources but also reduces the program execution timing.

Recommended Articles

This has been a guide to Arrays in C++. Here we discuss how Arrays work in C++, how to create, types of Arrays, method of passing, with codes, syntax, and outputs, etc. You can also go through our given articles to learn more-

  1. What is C++
  2. C++ Operators
  3. Arrays in C#
  4. Arrays in PHP

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

2 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More