Updated March 16, 2023
What is C++ Array Function?
Array function are the functions that are used to perform operations on set of array. To access array elements, C++ provides various array functions like at(), get(), front(), back(), size(), max_size(), and many more where at() will access the array element using array index, front() will return first array element, back() will return last array element, size() will return the number of array elements, max_size() is used to show the maximum number of elements.
Arrays are a very important data structure concept implemented by most of the programming languages. C++ also provides the array which is a collection of elements of the same type of a fixed size. The two basic things to keep in mind while declaring an array is the size of the array and the type of the array. Since these two things are to be declared at the very beginning while declaring an array, an array is static in nature. The size of array determines the number of elements it can store.
The General Syntax of Array Declaration is –
<Type of array> <array name> <[Size of array]>
Example –
int pincode_of_cities[50]
The size of the array must be an integer constant value which must be greater than 0. The type of array can be any data types valid in C++.
Initializing of C++ Array
There are two basic methods of initializing an array –
Method 1: At declaration time
int pincode[5] = {123, 000, 342, 678, 654};
OR
int pincode[] = {123, 000, 342, 678, 654};
</pre.
Method 2: Using a loop
int number[5];
for(int i = 0; i<sizeof(pincode); i++)
number = i;
This is a one-dimensional array or a 1D array. The second type of array is a multi-dimensional array which we will discuss little later.
First of all, let us look at how we can access values from an array. Few of the following methods are given below –
Accessing Values of an Array
Name of the array[index]: This will return the value at the index position mentioned.
#include <iostream>
using namespace std;
int arr[5] = {10,20,30,40,50};
int main ()
{
cout << arr[3];
return 0;
}
Output:
In order to print all the values in an array –
#include <iostream>
using namespace std;
int arr[5] = {10,20,30,40,50};
int i;
int main ()
{
for ( i=0 ; i<5 ; i++ )
{
cout << arr[i];
}
return 0;
}
Output:
Example:
#include <iostream>
using namespace std;
int main()
{
int num[5], sum = 0;
cout << "Enter 5 natural numbers: ";
// Store numbers
// Then find sum
for (int i = 0; i < 5; ++i)
{
cin >> num[i];
sum += num[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
Output:
Multidimensional Array
The above is the declaration for a single dimensional array. The second type of array is the multidimensional array and is also known as rectangular arrays in C++. Depending on the requirement, it can be a two-dimensional array or a three-dimensional array. The values are stored in a table format, also known as a matrix in the form of rows and columns.
The syntax to declare a multidimensional array is –
<data type> <name of array>[number of rows][number of columns]
int two_dim[2][2];
This means that the above array has –
- 2 rows
- 2 columns
The above array can be initialized in the following way –
Method 1
#include <iostream>
using namespace std;
int main()
{
int arr[3][3] =
{
{1, 5, 15},
{44, 0, 23},
{29, 41, 85} }; //declaring and initializing at the same time
//traversing through the array
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout<< arr[i][j]<<" ";
}
cout<<"\n"; //this will take to a new line once the all the columns of the //particular row has been traversed
}
return 0;
}
Output:
Method 2
#include <iostream>
using namespace std;
int main()
{
int arr[3][3]; //declaring a 2D array
arr[0][0]=5; //initializing the array
arr[0][1]=10;
arr[0][2]=15;
arr[1][0]=20;
arr[1][1]=30;
arr[1][2]=40;
arr[2][0]=50;
arr[2][1]=60;
arr[2][2]=70;
//traversing through the elements in the array
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{cout<< arr[i][j]<<” ” ;
}
cout<<"\n"; //this will take to a new line once the all the columns of the //particular row has been traversed
}
return 0;
}
Output:
Example:
#include <iostream>
using namespace std;
const int city = 2;
const int week = 2;
int main()
{
int temp[city][week];
cout << "Enter temp for city \n";
// Insert values
for (int i = 0; i < city; ++i)
{
for(int j = 0; j < week; ++j)
{
cout << "city " << i + 1 << ", Week Day " << j + 1 << " : ";
cin >> temp[i][j];
}
}
// Access values
for (int i = 0; i < city; ++i)
{
for(int j = 0; j < week; ++j)
{
cout << "city " << i + 1 << ", Week Day " << j + 1 << " = " << temp[i][j] << endl;
}
}
return 0;
}
Enter temp for city
city 1, Week Day 1 : 12
city 1, Week Day 2 : 24
city 2, Week Day 1 : 35
city 2, Week Day 2 : 47
</pre.
Output:
Recommended Articles
This is a guide to C++ Array Functions. Here we discuss the Initializing of C++ Array and Multidimensional Array with the Methods, Examples, and Output. You may also look at the following article to learn more –