Arrays
Arrays
In this article, you will learn to work with arrays. You will learn to
declare, initialize and, access array elements in C++ programming.
To solve this problem in C++, you can create an integer array having 100
elements.
An array is a collection of data that holds fixed number of values of same
type. For example:
int age[100];
Here, the age array can hold maximum of 100 elements of integer type.
The size and type of arrays cannot be changed after its declaration.
What is an ARRAY?
Arrays in C/C++
age[0], age[2]---age[n-1] last array element –age[99]
An array in C or C++ is a collection of items stored at contiguous
memory locations and elements can be accessed randomly using indices
of an array. They are used to store similar type of elements as in the
data type must be the same for all elements. They can be used to store
collection of primitive/basic data types such as int, float, double, char,
etc of any particular type. To add to it, an array in C or C++ can store
derived data types such as the structures, pointers etc. Given below is
the picturesque representation of an array.
Why do we need arrays?
We can use normal variables (v1, v2, v3, ..) when we have a small
number of objects, but if we want to store a large number of instances, it
becomes difficult to manage them with normal variables. The idea of an
array is to represent many instances in one variable.
There are various ways in which we can declare an array. It can be done
by specifying its type and size, by initializing it or both.
Array declaration by specifying size
// Array declaration by specifying size int arr1[10];
// With recent C/C++ versions, we can also
// declare an array of user specified size
int n = 10;
int arr2[n];
int arr[n];
cout<<”enter the value of n”;
cin>>n;
Not valid
Array declaration by initializing elements
// Array declaration by initializing elements
int arr[ ] = { 10, 20, 30, 40 } // Compiler creates an array of size 4.
// above is same as "int arr[4] = {10, 20, 30, 40}"
Here,
mark[0] is equal to 19
mark[1] is equal to 10
mark[2] is equal to 8
mark[3] is equal to 17
mark[4] is equal to 9
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
return 0;
}
Output
Enter 5 numbers: 3
4
5
4
2
Sum = 18
Better way to initialise this array with same array elements as above.
int test[2][3] = { {2, 4, 5}, {9, 0 0}};
int test[5]
Example 1: Two Dimensional Array
C++ Program to display all elements of an initialised two
dimensional array.
#include <iostream>
using namespace std;
int main()
{
int test[3][2] ;
return 0;
}
Output
test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1
int main()
{
int temperature[CITY][WEEK];
cout << "Enter all temperature for a week of first city and then second city. \n";
return 0;
}
Output
Enter all temperature for a week of first city and then second city.
City 1, Day 1 : 32
City 1, Day 2 : 33
City 1, Day 3 : 32
City 1, Day 4 : 34
City 1, Day 5 : 35
City 1, Day 6 : 36
City 1, Day 7 : 38
City 2, Day 1 : 23
City 2, Day 2 : 24
City 2, Day 3 : 26
City 2, Day 4 : 22
City 2, Day 5 : 29
City 2, Day 6 : 27
City 2, Day 7 : 23
int main()
{
// This array can store upto 12 elements (2x3x2)
int test[2][3][2];
return 0;
}
Output
Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream.h>
int main()
{ int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r; 2
cout << "Enter number of columns (between 1 and 100): ";
cin >> c; 2
cout << endl << "Enter elements of 1st matrix: " << endl;
// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j]; }
//Displaying the elements of first matrix
cout<<” Display the elements of Matrix a”;
for(i = 0; i < r; i++)
{ for(j = 0; j < c; j++)
{ cout<<a[i][j] <<”\t”;
}
cout<<”\n”;
}
// Storing elements of second matrix entered by user.
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j]; }
//Displaying the elements of second matrix
cout<<” Display the elements of Matrix b”;
for(i = 0; i < r; i++)
{ for(j = 0; j < c; j++)
{ cout<<b[i][j] <<”\t”;
}
cout<<”\n”;
}
// Adding Two matrices
cout << endl << "Sum of two matrix is: " << endl;
// Displaying the resultant sum matrix.
for(i = 0; i < r; ++i)
{ for(j = 0; j < c; ++j)
return 0;
}
Output
int main()
{
int i, n;
float arr[100];
Then, the program computes the transpose of the matrix and displays it
on the screen.
#include <iostream>
int main()
{ int a[10][10], trans[10][10], r, c, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> r >> c; // Storing element of matrix entered by user in array
a[][].
cout << endl << "Enter elements of matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout << "Enter elements a" << i + 1 << j + 1 << ": ";
cin >> a[i][j]; } // Displaying the matrix a[ ][ ]
cout << endl << "Entered Matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ cout << " " << a[i][j];
if(j == c - 1)
cout << endl << endl; } // Finding transpose of matrix a[][]
and storing it in array trans[][].
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{ trans[j][i]=a[i][j]; }
// Displaying the transpose,i.e, Displaying array trans[][].
cout << endl << "Transpose of Matrix: " << endl;
for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{ cout << " " << trans[i][j];
if(j == r - 1)
cout << endl << endl; }
return 0; }
Output
This program takes 10 words from the user and sort them in
lexicographical order
#include <iostream.h>
int main()
{ string str[10], temp;
cout << "Enter 10 words: " << endl;
for(int i = 0; i < 10; ++i)
{ getline(cin, str[i]); }
\\sort words
for(int i = 0; i < 9; ++i)
for( int j = i+1; j < 10; ++j)
{ if(str[i] > str[j])
{ temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
cout << "In lexicographical order: " << endl;
for(int i = 0; i < 10; ++i)
{ cout << str[i] << endl; }
return 0; }
Output
Enter 10 words:
C
C++
Java
Python
Perl
R
Matlab
Ruby
JavaScript
PHP
In lexicographical order: C
C++
Java
JavaScript
Matlab
PHP
Perl
Python
R
Ruby
Then, the array is sorted in lexicographical order using nested for loop
and displayed on the screen.
#include<iostream>
#include<string.h>
int main()
{
char str[5][20], t[20];
int i, j;
cout<<"\n Enter Any Five Names : \n\n";
for(i=0; i<5; i++)
{
cout<<" ";
cin>>str[i];
}
for(i=1; i<5; i++)
{
for(j=1; j<5; j++)
{
if(strcmp(str[j-1], str[j])>0)
{
strcpy(t, str[j-1]);
strcpy(str[j-1], str[j]);
strcpy(str[j], t);
}
}
}
cout<<"\n Names Sorted in Alphabetical Order : \n\n";
for(i=0; i<5; i++)
{
cout<<" ";
cout<<str[i]<<"\n";
}
return 0;
}
Output: