Array in C++ Definition, Need and Advantages
- In a program language when programmer need several variable of same type then instead of defining several variables, it is possible to define a single variable with several subscripts.
- This subscripted variable is called array.
- Array in C++ have many subscripts.
- As per google, an array is a data structure consisting of a collection of elements (values or variables), of same memory size, each identified by at least one array index or key.
- A subscript in array represents the dimension of matrix.
- Array in C++ is defined as a finite set of homogeneous data elements stored in successive memory locations.
- Array in C++ may have one or many dimensions.
- If there is only subscript then it is called single subscripted variables or one dimensional array and it represent one dimensional matrix i.e. either row matrix or column matrix.
- If there are two subscripts then it is called double subscripted variable or two-dimensional array and it represent two dimensional matrix.
- Array in C++ are convenient to solve the matrix algebra.
- Single subscripted variable represents one dimensional matrix, double subscripted variable represents two dimensional matrix and so on.
- Array is like a colony of data where every element of same type i.e. every element consumes same amount of memory.
- In case of integer array every element consumes 2 bytes, in case of float array every element consumes 4 bytes and so on.
- We can also define array of user defined data types i.e. array of structure, array of objects, array of array, array of pointers etc.
- In case of array of user defined data type every array element consumes same memory as per the defined definition.
- In array, single variable has many subscripts and hence no need to remember name of several variables.
- As the array in c++ elements are stored in successive locations, the access is fast and convenient.
- In case of pointers the base address is sufficient to access all elements i.e. only one pointer is sufficient for complete array.
Syntax of Array in C++
<data_type> Variable_name [Size]
where,
Size is integer constant.
- Example:
int age[10];
array age can contain maximum 10 integer values.
float percentage[15];
array percentage can contain maximum 15 real numbers (floating point values).
char city[10];
array city can hold maximum 10 characters.
int marks[3] [5];
array marks can have maximum 3*5 =15 integer values.
array marks contain marks obtained by 5 students in 3 subjects.
Use of Subscripts in the Array in C++
- We know that array is finite set of homogeneous data elements stored in successive memory location.
- When we require to access the array, we have to access using their position value.
- These position values are also called as index number subscripts.
- This means subscripts indicates the position of element in array and always in square bracket.
- Note that array in C++, subscripts starts at zero(0).
- For example, to store age of 100 persons. Here whenever declare 100 variable with different name but a single variable with different subscripts like age1, age2, age3…., age100.
- Here age is a variable and number 1,2,…100 are called subscripts.
- Above subscripted variables are declared in C++as array as int age[100];
i.e. age is an integer array that can hold maximum 100 values. - The elements of above array are referred as age[0], age[1], age[2], …. age[99]
Difference between Array in C++ and Ordinary Variable
- The difference between the definition of array and ordinary variable is that array is always declared, initialized and accessed using subscript whereas ordinary variable do not have any subscript.
- Ordinary variable do not hold single value at a time whereas an array can hold several values.
- Number of subscripts in array can be decided at the time of declaration or can also be defined at run time.
- In case of array we do not require separate pointer for each value. A single pointer variable is sufficient to access complete array.
- The syntax for ordinary variable definition is
data_type v1, v2 …;
Example:
int marks; //marks of a student
int marks =98; - And the syntax for array variable is
data_type v1[N1], v2[N2],…;
where v1, v2 are name of variables and N1, N2 are the integer constants indicating the maximum size of array. - Example:
int marks[5]; //marks of 5 students
int marks[5] = {98, 52, 61, 89, 70};
int a[2] [3];
int a[2] [3] = { 4, 5, 3, 2, 1, 7 };
int a[2] [3] ={ { 4, 5 ,3 },
{ 2 ,1, 7 }
};
Program to Read a Array in C++, Print it and Also to Calculate Sum of Array Element
#include <iostream>
int main() {
int n;
// Read the size of the array
std::cout << "Enter the size of the array: ";
std::cin >> n;
// Declare an array of the given size
int arr[n];
// Read elements into the array
std::cout << "Enter " << n << " integers:" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
// Print the array elements
std::cout << "Array elements: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
// Calculate the sum of array elements
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
std::cout << "Sum of array elements: " << sum << std::endl;
return 0;
}
Output:
Enter the size of the array: 20
Enter 20 integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Array elements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Sum of array elements: 210
Program to Read a Array in C++, Print it and Also Print it in Reverse Order
#include <iostream>
int main() {
int n;
// Read the size of the array
std::cout << "Enter the size of the array: ";
std::cin >> n;
// Declare an array of the given size
int arr[n];
// Read elements into the array
std::cout << "Enter " << n << " integers:" << std::endl;
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
// Print the array elements in original order
std::cout << "Array elements in original order: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
// Print the array elements in reverse order
std::cout << "Array elements in reverse order: ";
for (int i = n - 1; i >= 0; i--) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Enter the size of the array: 20
Enter 20 integers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Array elements in original order: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Array elements in reverse order: 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
2 thoughts on “Array in C++: Essential Tips for Success in 2024”