Documente Academic
Documente Profesional
Documente Cultură
Declaring Arrays:
To declare an array in C++, the programmer specifies the type of the elements
and the number of elements required by an array as follows:
double balance[10];
Initializing Arrays:
You can initialize C++ array elements either one by one or using a single
statement as follows:
The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ]. Following is
an example to assign a single element of the array:
You will create exactly the same array as you did in the previous example.
balance[4] = 50.0;
The above statement assigns element number 5th in the array a value of 50.0.
Array with 4th index will be 5th, i.e., last element because all arrays have 0 as
the index of their first element which is also called base index. Following is the
pictorial representation of the same array we discussed above:
An element is accessed by indexing the array name. This is done by placing the
index of the element within square brackets after the name of the array. For
example:
The above statement will take 10th element from the array and assign the value
to salary variable. Following is an example, which will use all the above-
mentioned three concepts viz. declaration, assignment and accessing arrays:
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main ()
{
int n[ 10 ]; // n is an array of 10 integers
return 0;
}
This program makes use of setw() function to format the output. When the
above code is compiled and executed, it produces the following result:
Element Value
0 100
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109
Multidimensional Array
type name[size1][size2]...[sizeN];
Two-Dimensional Arrays:
type arrayName [ x ][ y ];
Where type can be any valid C++ data type and arrayName will be a valid
C++ identifier.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
int main ()
{
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
return 0;
}
When the above code is compiled and executed, it produces the following
result:
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
Prof. Mandeep Kaur,
Dept. of Elect & IT, S.D. College, Ambala Cantt
Mob: 09729871160
Email: mandeepmalhotra28@gmail.com Page 5
BSIT-502, UNIT-2
Array, Strings, Structures, Union and Pointers
a[4][0]: 4
a[4][1]: 8
#include <iostream.h>
int main()
{
int n[5];
cout<<"Enter 5 numbers: ";
/* Storing 5 number entered by user in an array using for loop. */
for (int i = 0; i < 5; ++i)
{
cin>>n[i];
}
#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;
Output
Entered Matrix:
1 2 9
0 4 7
Transpose of Matrix:
1 0
2 4
9 7
/* If colum of first matrix in not equal to row of second matrix, asking user to
enter the size of matrix again. */
while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}
Output
Output Matrix:
24 29
6 25
C++ Structures:
Structure is the collection of variables of different types under a single name for
better visualisation of problem. Arrays is also collection of data but arrays can
hold data of only one type whereas structure can hold data of one or more types.
For example:
struct person
{
char name[50];
int age;
float salary;
};
Here a structure person is defined which has three members: name, age and
salary.
Unions in c++:
Unions in C++ is a user defined data type that uses the same memory as other
objects from a list of objects. At an instance it contains only a single object.
#include <iostream.h>
union Emp
{
int num;
double sal;
};