It may be convenient to store a collection of similar data elements in different separate variables. For example, if the age of 100 persons were to be stored in variables with unique names, it certainly would be difficult, that means 100 variables needed for assigning the individual values. So an array certainly solves this problem because basically the variable name is the same. We differentiate among the values in an array by its unique subscripts with defined size.
Characteristics of Array
- All the array elements share the common name.
- The elements of the array are stored in contiguous memory locations.
- By declaring or using an array, the program becomes short and simple which handles a large volume of similar kinds of data items.
- We put the array size of fixed length as required that means once the size is declared then the size will be fixed at the execution time of the program, so called static type.
- We can randomly access to every element using numeric index, index starts from 0 and ends at size-1.
Advantages of Array
- It is easier for handling similar types of data in a program.
- It is efficient for solving problems like sorting, searching, indexing etc
- It is very close to matrix, therefore it is easy for solving matrix related problems
- Graphic is an array of pixels, so graphics manipulation can be easily done using an array.
Disadvantages of Array
- It is not possible to hold a dissimilar type of data in an array.
- It is difficult to visualize the multi-dimensional array.
- It is static in nature so it is difficult to define the size of array during running time.
There are two types of array which are as follows.
- One Dimensional Array: An array which has only one subscript is named as one dimensional array, a subscript is a number of large brackets in which we put the size of the array.
- Multi Dimensional Array: An array which has more than one subscript is named as a multi dimensional array, subscripts define each dimension of the array.
Like any other variable, we have to declare array before they are used. The general form of one dimensional array declaration is,
As in above syntax, the data_type refers to the type of array elements, array the name of the array variable and the array size indicates number of elements that can be stored inside the array, Hence if the size of array is n, for example: a[n], a[0] is the first element and a[n-1] is the last element of the array.
int a[5];
In this example, the variable a is an array of integer with size five elements.
float weight[100];
In this example, the variable weight is an array of floating point numbers having size 100.
char temp[10];
In this example, temp variable is an array of characters with size 10 and it is also known as string.
We can initialize arrays at the time of declarations. The initial value must appear which will be assigned to the individual array elements, enclosed within the brace and separated by commas.
Here val1 is the value for the first array element, val2 is the value for the second array element, val3 is the value for the three array element and valn is the value for the last array element.
Here the number of subscripts determines the dimension of array ; that is one dimensional array means here is only one subscript. The value in the bracket is the size of the array. marks[0] is the first element of the array where 0 is the starting index of array.
marks[0] | marks[1] | marks[2] | marks[3] |
50 | 60 | 65 | 70 |
index 0 | index 1 | index 2 | index 3 |
The values to the array elements can be assigned as follows:
marks[0]=50;marks[1]=60;
marks[2]=65;
marks[3]=70;
int a[]={6,7,8,9,10}; //default size is 5 float x[5]={70.5,45.75,85.97,78.87,66.33}; char name[10]={‘n’,‘e’,‘p’,‘a’, ‘l’};
Multi dimensional arrays are defined the same as one-dimensional arrays, except that it consists of more than one pair of square brackets that are subscripts. Thus a two dimensional array will require two square brackets, one for row size and other for column size. The maximum capacity of elements of an array is the product of row size and column size. A three dimensional array will require three square brackets, and so on.
In general terms, a multidimensional array definition can be written as data_type array_name [expression1] [expression2] ..... [expression n];
As in above syntax, the data_type refers to the type of array elements, array_name is the name of the array variable and row_size, column_size are the number of array elements associated with each subscript that can be stored inside the array.
For example, a[m][n] is a 2 dimensional array in which the starting element of array is a[0][0] and the last element of array is a[m-1][n-1]. Here, in a[m][n], m denotes the row size and n denotes the column size in array.
If we have m*n array, it will have m*n elements and will require m*n element size bytes of storage. To allocate storage for an array you must reserve this amount of memory. The elements of a two dimensional array are stored row wise.
In two dimensional array (suppose 3x3 matrix), data items are stored in memory below:
Column 0 [0][0] |
Column 1 [0][1] |
Column 2 [0][2] |
|
---|---|---|---|
Row 0 | 100 | 200 | 300 |
[1][0] | [1][1] | [1][2] | |
Row 1 | 200 | 150 | 50 |
[2][0] | [2][1] | [2][2] | |
Row 2 | 350 | 500 | 600 |
As in above example, two dimensional array consist of 3 rows and 3 columns, the starting array element is 100 that is stored in position matrix[0][0] and the last array element is 600 that is stored in position matrix[2][2].
This means,
int matrix[3][3]={100, 200, 300, 200, 150, 50, 350, 500, 600};matrix[0][0]= 100; matrix[0][1]= 200; matrix[0][2]= 300; matrix[1][0]= 200; matrix[1][1]= 150; matrix[1][2]= 50; matrix[2][0]= 350; matrix[2][1]= 500; matrix[2][2]= 600;
Differentiate between one dimensional and two dimensional array:
One Dimensional Array | Two Dimensional Array |
---|---|
(i) It consists of only one subscript. | (i) It consists of two subscripts. |
(ii) Maximum size will be the size of the array which we define in the program. | (ii) Maximum size will be the product of row and column size of array which we define in the program. |
(iii) It stores data either row wise or column wise. | (iii) It stores data in matrix form that is row wise and column wise. |
(iv) Syntax: Data_type array_name[array_size]; |
(iv) Syntax: Data_type array_name[row_size][column_size]; |
(v) Example: int num[5]; | (v) Example: int matrix[2][2]; |
(vi) Program:
#include <stdio.h>
int main()
{
int num[4]={1,2,3,4},i;
for(i=0;i<4;i++)
{
printf("%3d",num[i]);
}
}
|
(vi) Program:
#include <stdio.h>
main()
{
int matrix[2][2]={1,2,3,4},i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%3d",matrix[i][j]);
}
printf("\n");
}
}
|
Algorithm to input marks of five subjects and display the total marks and average marks
- [Initialize] int marks[5],total=0, average=0
- for i = 0 to 4 {
Store marks[i]
} - for i = 0 to 4 {
total=total+marks[i]
} - average=total/5
- Display total and average
C Code to input marks of five subjects and display the total marks and average marks
Output:
Enter mark[1] : 50 Enter mark[2] : 60 Enter mark[3] : 70 Enter mark[4] : 80 Enter mark[5] : 90 Total marks = 350 and Average marks=70Algorithm to input ‘n’ numbers and display the sum of even numbers from the array
- [Initialize] int num[100],sum_of_even=0
- Input n
- for i= 0 to n-1 {
Store num[i]
} - for i= 0 to n-1 {
If num[i]%2==0 then
sum_of_even=sum_of_even+num[i]
} - Display sum_of_even
C Code to input ‘n’ numbers and display the sum of even numbers from the array
Output
Enter array size not more than 100 : 4 Enter num[1] : 1 Enter num[2] : 2 Enter num[3] : 3 Enter num[4] : 4 Sum of even numbers are 6Algorithm to find the position of the key number from given set of numbers which are stored in an array
- [Initialize] int num[100]
- Input n
- for i= 0 to n-1 {
Store num[i]
} - Input searchnum
- for i= 0 to n-1 {
If searchnum==num[i] then
Print num found and location=i
} - if (n==i) then print num not in list
C Code to find the position of the key number from given set of numbers which are stored in an array
Output
Enter array size not more than 100 : 4 Enter num[0] : 1 Enter num[1] : 2 Enter num[2] : 3 Enter num[3] : 4 Enter a number to be searched : 2 2 is in the list in location [1]Enter array size not more than 100 : 2 Enter num[0] : 1 Enter num[1] : 2 Enter a number to be searched : 3 3 is not in the list
Algorithm to read the age of 100 persons and count the number of persons in the age group between 50 to 60 year
- [Initialize] age[100],count=0
- for i=0 to 99 {
Store age[i]
} - for i=0 to 99 {
if(age is between 50 to 60 years)
count++;
} - Display count
C Code to read the age of 100 persons and count the number of persons in the age group between 50 to 60 year
Output
Disclaimer: For sample taken three data only Enter age[0] : 50 Enter age[1] : 45 Enter age[2] : 56 Total numbers having age between 5o to 60 years are 2- [Initialize] num[100],great=small=num[0]
- Input n
- for i=0 to n-1 {
Store num[i]
} - for i=1 to n-1 {
if num[i]>great
great=num[i]
if num[i]<small
small=num[i]
} - Print great and small
C Code to input ‘n’ numbers and find out the greatest and smallest number
Output
Enter array size not more than 100 : 3 Enter num[0]: 1 Enter num[1]: 2 Enter num[2]: 3 The smallest element is 1 The greatest element is 3Algorithm for a program that takes the salary of 100 employees and print the salary of the employees in ascending order
- [Initialize] salary[100]
- for i=0 to 99 {
Store salary[i]
} - for i= 0 to 99 {
for j=i+1 to 99 {
if(salary[i]>salary[j])
exchange (salary[i],salary[j])
} } - for i=0 to 99 {
Print salary[i]
}
C Code for a program that takes the salary of 100 employees and print the salary of the employees in ascending order
Output
Disclaimer: For sample taken three data only Enter salary[0] : 15000 Enter salary[1] : 9000 Enter salary[2] : 6000 Salary sorted in Ascending order 6000 9000 15000Algorithm to transpose matrix of size 3x3
- [Initialize] m[3][3],i,j
- for i=0 to 2 {
for j=0 to 2 {
Store m[i][j]
} } - Print a message “Transpose of a matrix”
- for i=0 to 2 {
for j=0 to 2 {
Store m[j][i]
} }
C Code to transpose matrix of size 3x3
Output
Enter m[0][0] : 1 Enter m[0][1] : 2 Enter m[0][2] : 3 Enter m[1][0] : 4 Enter m[1][1] : 5 Enter m[1][2] : 6 Enter m[2][0] : 7 Enter m[2][1] : 8 Enter m[2][2] : 9 Transpose of matrix is 1 4 7 2 5 8 3 6 9Algorithm to calculate the sum of all elements of a matrix with size 3x3
- [Initialize] m[3][3],i,j
- for i=0 to 2 {
for j=0 to 2 {
Store m[i][j]
} } - for i=0 to 2 {
for j=0 to 2 {
sum=sum+m[i][j]
} } - Print sum
C Code to calculate the sum of all elements of a matrix with size 3x3
Output
Enter m[0][0] : 1 Enter m[0][1] : 2 Enter m[0][2] : 3 Enter m[1][0] : 4 Enter m[1][1] : 5 Enter m[1][2] : 6 Enter m[2][0] : 7 Enter m[2][1] : 8 Enter m[2][2] : 9 sum of all elements of a matrix is 45Algorithm to calculate the sum of diagonal matrix with size 3x3
- [Initialize] m[3][3],i,j
- for i=0 to 2 {
for j=0 to 2 {
Store m[i][j]
} } - for i=0 to 2 {
for j=0 to 2 {
if(i==j)
sum=sum+m[i][j]
} } - Print sum
C Code to calculate the sum of diagonal matrix with size 3x3
Output
Enter m[0][0] : 1 Enter m[0][1] : 0 Enter m[0][2] : 0 Enter m[1][0] : 0 Enter m[1][1] : 2 Enter m[1][2] : 0 Enter m[2][0] : 0 Enter m[2][1] : 0 Enter m[2][2] : 3 sum of all elements of a matrix is 6Algorithm to add two matrices with size 2x2
- [Initialize] m1[2][2],m2[2][2],m3[2][2],i,j
- for i=0 to 1 {
for j=0 to 1 {
Store m1[i][j]
} } - for i=0 to 1 {
for j=0 to 1 {
Store m2[i][j]
} } - for i=0 to 1{
for j=0 to 1 {
m3[i][j]=m1[i][j]+m2[i][j]
} } - for i=0 to 1 {
for j=0 to 1 {
Print m3[i][j]
} }