Arrays in C++
Array is a data type like int, float, char, double etc. Note that int, float, char, double data types can store only one item of data. But array can store set of values (data items). Array stores same kind of data. It is important to note that array stores similar kind of data.
Example: suppose we want to store roll numbers of all the students. Remember that roll number cannot be float. Roll numbers are only integer values. So, we can declare an array of integers. Marks obtained by students can be float (can have decimal values).
Array of integers = Integer array
Array of floats = float array
Array of characters = String
Set of similar data types are known as Array
INTEGER ARRAY
#include<iostream> //all programs must include this line using namespace std; int main() //program begins here { //beginning of function main int x[5] = {2, 45, 6, 9, 200}; //array initialization. 5 elements for (int i=0; i<5; i++) //for loop to print array elements { //beginning of for loop cout<<x[i]<<endl; //this statement prints values of array } //end of for loop return 0; } //end of function main |
Note: here x is the name of the array. Individual elements of the array are accessed using index (or subscript). In above program variable ‘i’ is used as index. Note that array index always starts from ‘zero — 0’. For above program individual elements are: x[0], x[1], x[2], x[3] and x[4], because the size of the array is 5.
x[0] = first element ……. first element always starts at index ZERO
x[1] = second element
x[2] = third element
x[3] = fourth element
x[4] = fifth element = Last element
Note that variables can be initialised directly in the program or can be given using keyboard. The below program illustrates inputting values from the keyboard. Here cin statement is used to give input from the keyboard. Note that cin is always associated with keyboard and cout is always used with printing on the screen.
#include<iostream> //all programs must include this line using namespace std; int main() //program begins here { //beginning of function main int x[10]; //array declaration with 10 elements for (int i=0; i<5; i++) //for loop to input array elements. { //beginning of for loop cin>>x[i]; //this statement inputs values of array from keyboard } //end of for loop for (int i=0; i<5; i++) //for loop to print array elements { //beginning of for loop cout<<x[i]<<endl; //this statement prints values of array } //end of for loop return 0; } //end of function main
FLOAT ARRAY
Note that float means some real number, which can contain decimal (fraction) value.
#include<iostream>
using namespace std;
int main()
{
float x[5] = {2.6, 4.5, 6.49, 9.0, 20.7};// array initialization with 5 elements.
for (int i=0; i<5; i++) // for loop to print array elements
{
cout<<x[i]<<endl; // this statement prints values of array
}
return 0;
}
CHARACTER ARRAY
Remember that characters are initialised using single quotes. Suppose 8 is an integer data, but ‘8’ is a character data.
#include<iostream> using namespace std; int main() { char x[6] = {'A', '8', 'H', '
Note the following points carefully:
(a) Till the array elements are not given any specific values, they are supposed to contain garbage values.
(b) If the array is initialised where it is declared, mentioning the dimension of the array is optional as in the 2nd example above.
For example, suppose we wish to arrange the marks obtained by 100 students in ascending order. In such a case we have two options to store these marks in memory:
- Construct 100 variables to store marks obtained by 100 different students, i.e. each variable containing one student’s marks.
- Construct one variable (called array or subscripted variable) capable of storing all the hundred values.
Obviously, the second alternative is better. A simple reason for this is, it would be much easier to handle one variable than handling 100 different variables.
An array is a collective name given to a group of ‘similar quantities’. These similar quantities could be percentage marks of 100 students, or salaries of 300 employees, or ages of 50 employees. What is important is that the quantities must be ‘similar’. Each member in the group is referred to by its position in the group.
For example, assume the following group of numbers, which represent percentage marks obtained by five students.
per = { 48, 88, 34, 23, 96 }
Thus, an array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc. Usually, the array of characters is called a ‘string’, whereas an array of ints or floats is called simply an array.
A Simple Program Using Array
Q. Let us try to write a program to find average marks obtained by a class of 30 students in a test.
#include<iostream> using namespace std; int main( ) { int avg, sum = 0 ; int i ; int marks[30] ; //array declaration cout<< "\nEnter marks of 30 students"; for ( i = 0 ; i <= 29 ; i++ ) { cin>>marks[i] ; //store data in array } for ( i = 0 ; i <= 29 ; i++ ) { sum = sum + marks[i] ; // read data from an array*/ } avg = sum / 30 ; cout<< "\nAverage marks = "<<avg; }
Entering Data into an Array
Here is the section of code that places data into an array:
for ( i = 0 ; i <= 29 ; i++ )
{
cout<< “\nEnter marks “;
cin>>marks[i] ) ; //store data in array
}
The for loop causes the process of asking for and receiving a student’s marks from the user to be repeated 30 times. The first time through the loop, i has a value 0, so the scanf( ) function will cause the value typed to be stored in the array element marks[0], the first element of the array. This process will be repeated until i.
SUMMARY
- An array is a collection of similar elements. The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
- An array is also known as a subscripted variable.
- Before using an array its type (i.e., int or float of char or double etc.) and dimension must be declared.
- However big an array its elements are always stored in successive memory locations. This is a very important point.
- An array is similar to an ordinary variable except that it can store multiple elements of similar type.
In a 1-D array, zeroth element is a single value, whereas, in a 2-D array this element is a 1-D array. - On incrementing a pointer it points to the next location of its type.
- Array elements are stored in contiguous memory locations and so they can be accessed using pointers.
What would be the output of the following programs
Q1.
#include<iostream>
using namespace std;
int main( )
{
int num[26], temp ;
num[0] = 100 ;
num[25] = 200 ;
temp = num[25] ;
num[25] = num[0] ;
num[0] = temp ;
cout<<num[0]<<endl<<num[25];
}
Q2.
#include<iostream>
using namespace std;
int main( )
{
int array[26], i ;
for ( i = 0 ; i <= 25 ; i++ )
{
array[i] = ‘A’ + i ;
cout<<array[i]<<endl;
}
}
Q3.
#include<iostream>
using namespace std;
int main( )
{
int sub[50], i ;
for ( i = 0 ; i <= 48 ; i++ ) ;
{
sub[i] = i ;
cout<<sub[i];
}
}
Q4.
#include<iostream>
using namespace std;
int main()
{
char c[2] = “A” ;
cout<<c[0]<<endl;
cout<<c;
}
Q5.
#include<iostream>
using namespace std;
int main()
{
int b[ ] = { 10, 20, 30, 40, 50 } ;
int i ;
for ( i = 0 ; i <= 4 ; i++ )
cout<< *( b + i )<<endl;
}
Q6.
#include<iostream>
using namespace std;
int main()
{
int b[ ] = { 0, 20, 0, 40, 5 } ;
int i, *k ;
k = b ;
for ( i = 0 ; i <= 4 ; i++ )
{
cout<<*k<<endl;
k++ ;
}
}
Q7.
#include<iostream>
using namespace std;
int main()
{
int a[5] ;
int i ;
for ( i = 0 ; i <= 4 ; i++ )
cout<<a[i]<<endl;
}
Q8.
#include<iostream>
using namespace std;
int main()
{
int a[5] = { 5, 1, 15, 20, 25 } ;
int i, j, k = 1, m ;
i = ++a[1] ;
j = a[1]++ ;
m = a[i++] ;
cout<< i<<endl<<j<<endl<<m;
}
Q9.
#include<iostream>
using namespace std;
int main()
{
int a[ ] = { 10, 20, 30, 40, 50 } ;
int *j ;
j = &a[0] ; // store the address of zeroth element
j = j + 3 ;
cout<<*j;
}
Q10.
#include<iostream>
using namespace std;
int main()
{
float a[ ] = { 13.24, 1.5, 1.5, 5.4, 3.5 } ;
float *j ;
j = a ;
j = j + 4 ;
cout<<j<<endl<<*j<<endl<<a[4];
}
Fill in the blanks
- “A” is a ___________ while ’A’ is a ____________.
- A string is terminated by a ______ character, which is written as ______.
- The array char name[10] can consist of a maximum of ______ characters.
- The array elements are always stored in _________ memory locations.
- In an array int arr[12] the word arr represents the a_________ of the array
Answer the following
(a) An array is a collection of
1. Different data types scattered throughout memory
2. The same data type scattered throughout memory
3. The same data type placed next to each other in memory
4. Different data types placed next to each other in memory
(b) What is the difference between the 5’s in these two expressions? (Select the correct answer)
int num[5] ;
num[5] = 11 ;
1. First is particular element, second is type
2. First is array size, second is particular element
3. First is particular element, second is array size
4. Both specify array size
State whether the following statements are True or False
1. The array int num[26] has twenty-six elements.
2. The expression num[1] designates the first element in the array
3. It is necessary to initialize the array at the time of declaration.
4. The expression num[27] designates the twenty-eighth element in the array.
, 'T', 'x'};//array initialization with 6 elements. for (int i=0; i<5; i++) { cout<<x[i]<<endl; } } int num[6] = { 2, 4, 12, 5, 45, 5 } ; // array dimension is 6. Total array items = 6 int n[ ] = { 2, 4, 12, 5, 45, 5 } ; // array dimension can be empty. It is allowed. float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;
Note the following points carefully:
(a) Till the array elements are not given any specific values, they are supposed to contain garbage values.
(b) If the array is initialised where it is declared, mentioning the dimension of the array is optional as in the 2nd example above.
For example, suppose we wish to arrange the marks obtained by 100 students in ascending order. In such a case we have two options to store these marks in memory:
- Construct 100 variables to store marks obtained by 100 different students, i.e. each variable containing one student’s marks.
- Construct one variable (called array or subscripted variable) capable of storing all the hundred values.
Obviously, the second alternative is better. A simple reason for this is, it would be much easier to handle one variable than handling 100 different variables.
An array is a collective name given to a group of ‘similar quantities’. These similar quantities could be percentage marks of 100 students, or salaries of 300 employees, or ages of 50 employees. What is important is that the quantities must be ‘similar’. Each member in the group is referred to by its position in the group.
For example, assume the following group of numbers, which represent percentage marks obtained by five students.
per = { 48, 88, 34, 23, 96 }
Thus, an array is a collection of similar elements. These similar elements could be all ints, or all floats, or all chars, etc. Usually, the array of characters is called a ‘string’, whereas an array of ints or floats is called simply an array.
A Simple Program Using Array
Q. Let us try to write a program to find average marks obtained by a class of 30 students in a test.
Entering Data into an Array
Here is the section of code that places data into an array:
for ( i = 0 ; i <= 29 ; i++ )
{
cout<< “\nEnter marks “;
cin>>marks[i] ) ; //store data in array
}
The for loop causes the process of asking for and receiving a student’s marks from the user to be repeated 30 times. The first time through the loop, i has a value 0, so the scanf( ) function will cause the value typed to be stored in the array element marks[0], the first element of the array. This process will be repeated until i.
SUMMARY
- An array is a collection of similar elements. The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
- An array is also known as a subscripted variable.
- Before using an array its type (i.e., int or float of char or double etc.) and dimension must be declared.
- However big an array its elements are always stored in successive memory locations. This is a very important point.
- An array is similar to an ordinary variable except that it can store multiple elements of similar type.
In a 1-D array, zeroth element is a single value, whereas, in a 2-D array this element is a 1-D array. - On incrementing a pointer it points to the next location of its type.
- Array elements are stored in contiguous memory locations and so they can be accessed using pointers.
What would be the output of the following programs
Q1.
#include<iostream>
using namespace std;
int main( )
{
int num[26], temp ;
num[0] = 100 ;
num[25] = 200 ;
temp = num[25] ;
num[25] = num[0] ;
num[0] = temp ;
cout<<num[0]<<endl<<num[25];
}
Q2.
#include<iostream>
using namespace std;
int main( )
{
int array[26], i ;
for ( i = 0 ; i <= 25 ; i++ )
{
array[i] = ‘A’ + i ;
cout<<array[i]<<endl;
}
}
Q3.
#include<iostream>
using namespace std;
int main( )
{
int sub[50], i ;
for ( i = 0 ; i <= 48 ; i++ ) ;
{
sub[i] = i ;
cout<<sub[i];
}
}
Q4.
#include<iostream>
using namespace std;
int main()
{
char c[2] = “A” ;
cout<<c[0]<<endl;
cout<<c;
}
Q5.
#include<iostream>
using namespace std;
int main()
{
int b[ ] = { 10, 20, 30, 40, 50 } ;
int i ;
for ( i = 0 ; i <= 4 ; i++ )
cout<< *( b + i )<<endl;
}
Q6.
#include<iostream>
using namespace std;
int main()
{
int b[ ] = { 0, 20, 0, 40, 5 } ;
int i, *k ;
k = b ;
for ( i = 0 ; i <= 4 ; i++ )
{
cout<<*k<<endl;
k++ ;
}
}
Q7.
#include<iostream>
using namespace std;
int main()
{
int a[5] ;
int i ;
for ( i = 0 ; i <= 4 ; i++ )
cout<<a[i]<<endl;
}
Q8.
#include<iostream>
using namespace std;
int main()
{
int a[5] = { 5, 1, 15, 20, 25 } ;
int i, j, k = 1, m ;
i = ++a[1] ;
j = a[1]++ ;
m = a[i++] ;
cout<< i<<endl<<j<<endl<<m;
}
Q9.
#include<iostream>
using namespace std;
int main()
{
int a[ ] = { 10, 20, 30, 40, 50 } ;
int *j ;
j = &a[0] ; // store the address of zeroth element
j = j + 3 ;
cout<<*j;
}
Q10.
#include<iostream>
using namespace std;
int main()
{
float a[ ] = { 13.24, 1.5, 1.5, 5.4, 3.5 } ;
float *j ;
j = a ;
j = j + 4 ;
cout<<j<<endl<<*j<<endl<<a[4];
}
Fill in the blanks
- “A” is a ___________ while ’A’ is a ____________.
- A string is terminated by a ______ character, which is written as ______.
- The array char name[10] can consist of a maximum of ______ characters.
- The array elements are always stored in _________ memory locations.
- In an array int arr[12] the word arr represents the a_________ of the array
Answer the following
(a) An array is a collection of
1. Different data types scattered throughout memory
2. The same data type scattered throughout memory
3. The same data type placed next to each other in memory
4. Different data types placed next to each other in memory
(b) What is the difference between the 5’s in these two expressions? (Select the correct answer)
int num[5] ;
num[5] = 11 ;
1. First is particular element, second is type
2. First is array size, second is particular element
3. First is particular element, second is array size
4. Both specify array size
State whether the following statements are True or False
1. The array int num[26] has twenty-six elements.
2. The expression num[1] designates the first element in the array
3. It is necessary to initialize the array at the time of declaration.
4. The expression num[27] designates the twenty-eighth element in the array.