3. 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 unique name and type. But an
array is a collection of different adjacent memory
locations.
These memory locations in the array are called
elements of array.
The total number of elements in the array is called its
length.
4. Advantages of array:
o Arrays can store a large number of values with
single name.
o Arrays are used to process many values
quickly and easily.
o The values stored in an array can be sorted
easily.
o A search process can be applied on arrays
easily.
5. Declaring One dimensional
Array:
The syntax of declaring one-dimensional array is:
Data_type Identifier[length];
Data_type Indicates data type of values.
Identifier Indicates the name of array.
Length Indicates total number of elements in
array.
7. Array initialization
The syntax of array initialization is :
Data_type Identifier[length]={list of values};
List of values It indicates the values to initialize the array. These
values must b constant.
9. Accessing individual elements in an
array:
The syntax for accessing element is as follows :
Array_Name[index];
Array_Name Indicates the name of array.
Index Indicates the index element
to be accessed.
11. A simple program:
Write a program that inputs five integers
from user and stores them in an array. It
then displays all values in array without
using loop.
17. cout<<“The values in array are : n
for(i=0 ; i<5 ; i++)
cout<<arr[i]<<endl;
return 0;
}
18. Output:
Enter an integer : 32
Enter an integer : 46
Enter an integer : 50
Enter an integer : 75
Enter an integer : 84
The values in array are :
32
46
50
75
84
19. Write a program that input five number
from user in array and display maximum
number.
20. #include<iostream>
using namespace std;
main()
{
int a[5],i,max;
for(i=0;i<5;i++)
{
cout<<"Enter number : ";
cin>>a[i];
}
max=a[0];
for(i=0;i<5;i++)
if(max<a[i])
max=a[i];
cout<<"Maximum Number is : "<<a[i];
return 0;
}
21. Output:
Enter number : 8
Enter number : 13
Enter number : 5
Enter number : 15
Enter number : 14
Maximum Number is 15
22. Two-Dimensional Arrays
Two-dimensional array can be considered as a table that
consists of rows and column. Each element in 2-D array is
referred with the help of two indexes. One index is used to
indicate the row and the second index indicates the column of
the element.
25. Accessing elements of 2-D array
The array name and indexes of row and column are used to access
individual element of a 2-D array. For example, the following statement will
store 20 in the second column of first row.
Int Arr[0][1]=100;
OR
By assigning variables to indexes:
R=0;
C=1;
Arr[R][C]=100;
26. Entering Data in 2-D array
You can enter data in any element of array using array name and index of
element. For example:
27. Nested loops are frequently used to enter data in 2-D arrays.
The outer loop are used to refer to the rows in array.
Inner loop are used to refer to the columns in array.
28. Write a program that stores integer values
in an array of 2 rows and 4 columns.
31. Initialization 2-D Arrays
The process of initialization is performed by assigning the initial values in
braces separated by commas at the time of declaration.
For example:
Int array[3][4]={ {12,5,22,84},
{95,3,41,59},
{77,6,53,62} };
OR
Int array[3][4]={ {12,5,22,84}, {95,3,41,59}, {77,6,53,62} };
32. Initialization can also be performed without using inner braces.
Int array[3][4]={ 12,5,22,84,
95,3,41,59,
77,6,53,62 }
OR
Int array[3][4]= { 12,5,22,84,95,3,41,59,77,6,53,62 }
33. Write a program that initializes a 2-D
array of 2 rows and 3 columns and then
display its values.