Arrays in C++
What is array?
An array is a group of consecutive memory locations with same name and type. Simple variable is a single memory location with a unique name and a type. But an array is a collection of different adjacent memory locations. All these memory locations have one collective name and type. The memory location is the array are known as elements of array. The total number of elements in the array is called its length.
Each element in the array is accessed with reference to tis position of location in the array. This position is called index or subscript. Each element in the array has a unique index. The index of first element is 0 and the index of last element in length -1. The value of the index is written in brackets along with the name of array.
Advantages / Uses of Arrays
Some advantages of arrays are as follows:
- Arrays can store a large number of values with single name.
- Arrays are used to process many values easily and quickly.
- The values stored in array can be stored easily.
- A search process can be applied on arrays easily.
Declaring One-Dimensional Array
A type of array in which all elements are arranged in the form of a list is known as one-dimensional array. It is also called single dimensional array or linear list. It consist of one column or one row. The process of specifying array name, length and data type is called array declaration.
Syntax:
The syntax of declaring one dimensional array is as follows:
Data_Type identifier[Length];
Data_Type It indicates the data types of the values to be stored in the array.
Identifier It indicates the name of the array.
Length It indicates total number of elements in the array. It must be a literal constant or symbolic constant.
Example:
int marks[5];
The above example declares an integer array marks of five elements. It allocates five consecutive in memory. The index of first elements is 0 and index of last element is 4.
Array Initialization
The process of assigning values to array elements at the time of array declaration is called array initialization. The initialization process provides a list of initial values for array elements. The values are separated with sommas and enclosed within braces. There must be at least one initial value between bracers. A syntax error occurs if the values in braces are more than the length of array. If the number of initial values is less than the array size, the remaining array elements are initialized too zero.
Syntax:
The syntax to initialize array is as follows:
Data_Type identifier[Length] = { List of values };
Data_Type It indicates the data types of the values to be stored in the array.
Identifier It indicates the name of the array.
Length It indicates total number of elements in the array. It must be a literal constant or symbolic constant.
List of values It indicates the values to initialize the array. These values must be constant.
Example:
int marks[5] = {70,23,50,60,89};
The above statement declares an integer array marks with five elements. It also initialize the array with values given in the braces. In this declaration, marks[0] is initialized to 70, marks[1] is initialized to 23 and so on.
Array using loops
Write a program that inputs five values from the user, stores them in an array and displays the sum and average of these values.
using namespace std;
int main()
{
int arr[5], i, sum=0;
float avg=0.0;
for(i=0; i<5; i++)
{
cout<<"Enter value: ";
cin>>arr[i];
sum=sum+arr[i];
}
avg=sum/5.0;
cout<<"Sum is "<<sum<<"\n";
cout<<"Average is "<<avg;
return 0;
}