Theory of Array
Definition of Array
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.
An array is a collection of similar types of data items treated as a single unit. It acts to store related data under the same name with an index, also known as a subscript which helps to access individual array elements. Array data type may be int, float, char etc, depending upon the nature of problems.
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.
Types of Array
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.
Declaration of One Dimensional Array:
Like any other variable, we have to declare array before they are used. The general form of one dimensional array declaration is,
Syntax:
Data_type array_name[array_size];
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.
Example 1
int a[5];
In this example, the variable a is an array of integer with size five elements.
Example 2
float weight[100];
In this example, the variable weight is an array of floating point numbers having size 100.
Example 3
char temp[10];
In this example, temp variable is an array of characters with size 10 and it is also known as string.
Initialization of an Array
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.
Syntax
data_type array name [size] = {val1, val2, val3,..valn};
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.
Example of an Array Initialization:
int marks[4]={50, 60, 65, 70};
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;
Other Examples:
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 Array
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];
In two dimensional array declaration we write,
data_type array_name [row_size] [column_size];
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.
Initialization of 2-D 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");
}
}
|
One program per page [ Write in Single Side ONLY]
1. Write C program to input 10 elements and print them.
#include<stdio.h>
int main()
{
int a[10],i;
for(i=0;i<10;i++)
{
printf("Enter a[%d] : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
Output
Enter a[1] : 6
Enter a[2] : 41
Enter a[3] : 52
Enter a[4] : 56
Enter a[5] : 56
Enter a[6] : 78
Enter a[7] : 7
Enter a[8] : 45
Enter a[9] : 4
Enter a[10] : 55
6
41
52
56
56
78
7
45
4
55
2. Let an array of size 200. Input any 10 elements and find total of even numbers and their average.
#include<stdio.h>
int main()
{
int a[200],i,sum=0;
for(i=0;i<10;i++)
{
printf("Enter a[%d] : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
if(a[i]%2==0)
sum=sum+a[i];
}
printf("Sum of even numbers are %d",sum);
return 0;
}
Output
Enter a[1] : 5
Enter a[2] : 74
Enter a[3] : 2
Enter a[4] : 8
Enter a[5] : 12
Enter a[6] : 5
Enter a[7] : 7
Enter a[8] : 9
Enter a[9] : 123
Enter a[10] : 44
Sum of even numbers are 140
3. Let an array of size 200. Write C Program to input 10 numbers and find maximum and minimum values.
#include<stdio.h>
int main()
{
int n[200],i,max,min;
for(i=0;i<10;i++)
{
printf("Enter a[%d] : ",i+1);
scanf("%d",&n[i]);
}
max=min=n[0];
for(i=0;i<10;i++)
{
if(n[i]>max)
max=n[i];
if(n[i]<min)
min=n[i];
}
printf(" The maximum value = %d\n",max);
printf(" The minimum value = %d\n",min);
return 0;
}
Output
Enter a[1] : 65
Enter a[2] : 2
Enter a[3] : 5454
Enter a[4] : 6
Enter a[5] : 8
Enter a[6] : 95
Enter a[7] : 23
Enter a[8] : 31
Enter a[9] : 456
Enter a[10] : 88
The maximum value = 5454
The minimum value = 2
4. An array contains some elements. Search an element in that array.
#include<stdio.h>
int main()
{
int n,num[100],i,searchnum;
printf(" Enter array size not more than 100 : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter num[%d] : ",i+1);
scanf("%d", &num[i]);
}
printf(" Enter a number to be searched : ");
scanf("%d",&searchnum);
for(i=0;i<n;i++)
{
if(searchnum==num[i])
{
printf(" %d is in the list", searchnum);
break;
}
}
if(n==i)
printf(" %d is not in the list", searchnum);
return 0;
}
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
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
5. You are given an array age[200]. Input any ten ages of persons and count total number of persons who have age i) <=20 ii)>20 and <100
#include<stdio.h>
int main()
{
int age[100],i,c1=0,c2=0;
for(i=0;i<10;i++)
{
printf(" Enter age[%d] : ",i+1);
scanf("%d", &age[i]);
}
for(i=0;i<10;i++)
{
if(age[i]<=20)
c1=c1+1;
if(age[i]>20&&age[i]<100)
c2=c2+1;
}
printf(" Total person of age upto 20 is %d\n", c1);
printf(" Total person of age between 20 and 100 is %d", c2);
return 0;
}
Output
Enter age[1] : 5
Enter age[2] : 15
Enter age[3] : 52
Enter age[4] : 42
Enter age[5] : 2
Enter age[6] : 65
Enter age[7] : 14
Enter age[8] : 9
Enter age[9] : 51
Enter age[10] : 1
Total person of age upto 20 is 6
Total person of age between 20 and 100 is 4
6. You are given an array age[200]. Input any ten ages of persons and print them in sorted (Ascending and descending order) form.
For Ascending Order
#include<stdio.h>
int main()
{
int age[100],i,j,temp;
for(i=0;i<10;i++)
{
printf(" Enter age[%d] : ",i+1);
scanf("%d", &age[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
if(age[i]>age[j])
{
temp=age[i];
age[i]=age[j];
age[j]=temp;
}
}
printf("\n Sorted age in Ascending order \n");
for(i=0;i<10;i++)
{
printf("%d\n",age[i]);
}
return 0;
}
Output
Enter age[1] : 6
Enter age[2] : 4
Enter age[3] : 15
Enter age[4] : 32
Enter age[5] : 42
Enter age[6] : 47
Enter age[7] : 8
Enter age[8] : 5
Enter age[9] : 6
Enter age[10] : 2
Sorted age in Ascending order
2
4
5
6
6
8
15
32
42
47For Descending Order
#include<stdio.h>
int main()
{
int age[100],i,j,temp;
for(i=0;i<10;i++)
{
printf(" Enter age[%d] : ",i+1);
scanf("%d", &age[i]);
}
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
if(age[i]<age[j])
{
temp=age[i];
age[i]=age[j];
age[j]=temp;
}
}
printf("\n Sorted age in Descending order \n");
for(i=0;i<10;i++)
{
printf("%d\n",age[i]);
}
return 0;
}
Output
Enter age[1] : 36
Enter age[2] : 2
Enter age[3] : 47
Enter age[4] : 5
Enter age[5] : 6
Enter age[6] : 24
Enter age[7] : 1
Enter age[8] : 5
Enter age[9] : 6
Enter age[10] : 2
Sorted age in Descending order
47
36
24
6
6
5
5
2
2
1
7. Write C Program to input elements of matrix 2x2 and print them.
#include<stdio.h>
int main()
{
int i,j,matrix[2][2];
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Enter matrix[%d][%d] : ",i,j);
scanf("%d",&matrix[i][j]);
}
}
printf("\n Given matrix is : \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter matrix[0][0] : 1
Enter matrix[0][1] : 2
Enter matrix[1][0] : 3
Enter matrix[1][1] : 4
Given matrix is :
1 2
3 4
8. We have two matrices of order 3x3. Find their sum.
#include<stdio.h>
int main()
{
int i,j,a[3][3],b[3][3],c[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter a[%d][%d] : ",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n sum of two 3x3 matrices : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter a[0][0] : 1
Enter a[0][1] : 2
Enter a[0][2] : 3
Enter a[1][0] : 4
Enter a[1][1] : 5
Enter a[1][2] : 6
Enter a[2][0] : 7
Enter a[2][1] : 8
Enter a[2][2] : 9
Enter a[0][0] : 1
Enter a[0][1] : 2
Enter a[0][2] : 3
Enter a[1][0] : 4
Enter a[1][1] : 5
Enter a[1][2] : 6
Enter a[2][0] : 7
Enter a[2][1] : 8
Enter a[2][2] : 9
sum of two 3x3 matrices :
2 4 6
8 10 12
14 16 18
9. We have a matrix of order 3x4. Input its elements and print its elements in transpose form.
#include<stdio.h>
int main()
{
int i,j,matrix[3][4];
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("Enter matrix[%d][%d] : ",i,j);
scanf("%d",&matrix[i][j]);
}
}
printf("\n Transpose of given matrix is : \n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",matrix[j][i]);
}
printf("\n");
}
return 0;
}
Output
Enter matrix[0][0] : 1
Enter matrix[0][1] : 2
Enter matrix[0][2] : 3
Enter matrix[0][3] : 4
Enter matrix[1][0] : 5
Enter matrix[1][1] : 6
Enter matrix[1][2] : 7
Enter matrix[1][3] : 8
Enter matrix[2][0] : 9
Enter matrix[2][1] : 10
Enter matrix[2][2] : 11
Enter matrix[2][3] : 12
Transpose of given matrix is :
1 5 9
2 6 10
3 7 11
4 8 12
10. Suppose we have a matrix of order 3x3. Now write a program to find sum of all diagonal elements.
#include<stdio.h>
int main()
{
int i,j,matrix[3][3],sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter matrix[%d][%d] : ",i,j);
scanf("%d",&matrix[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
sum=sum+matrix[j][i];
}
}
printf("sum of all diagonal elements is %d",sum);
return 0;
}
Output
Enter matrix[0][0] : 1
Enter matrix[0][1] : 2
Enter matrix[0][2] : 3
Enter matrix[1][0] : 4
Enter matrix[1][1] : 5
Enter matrix[1][2] : 6
Enter matrix[2][0] : 7
Enter matrix[2][1] : 8
Enter matrix[2][2] : 9
sum of all diagonal elements is 15