Properties of Array
Properties of Array
By using the array, we can access the elements easily. Only a few lines of
code are required to access the elements of the array.
Properties of Array
o Each element of an array is of same data type and carries the same
size, i.e., int = 4 bytes.
o Elements of the array are stored at contiguous memory locations
where the first element is stored at the smallest memory location.
o Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given
base address and the size of the data element.
Advantage of C Array
2) Ease of traversing: By using the for loop, we can retrieve the elements
of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines
of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1. data_type array_name[array_size];
1. int marks[5];
Initialization of C Array
1. marks[0]=80;//initialization of array
2. marks[1]=60;
3. marks[2]=70;
4. marks[3]=85;
5. marks[4]=75;
C array example
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. int marks[5];//declaration of array
5. marks[0]=80;//initialization of array
6. marks[1]=60;
7. marks[2]=70;
8. marks[3]=85;
9. marks[4]=75;
10. //traversal of array
11. for(i=0;i<5;i++){
12. printf("%d \n",marks[i]);
13. }//end of for loop
14. return 0;
15. }
Output
80
60
70
85
75
We can initialize the c array at the time of declaration. Let's see the code.
1. int marks[5]={20,30,40,50,60};
1. int marks[]={20,30,40,50,60};
1. #include<stdio.h>
2. int main(){
3. int i=0;
4. int marks[5]={20,30,40,50,60};//declaration and initialization of array
5. //traversal of array
6. for(i=0;i<5;i++){
7. printf("%d \n",marks[i]);
8. }
9. return 0;
10. }
Output
20
30
40
50
60
C Array Example: Sorting an array
In the following program, we are using bubble sort method to sort the
array in ascending order.
1. #include<stdio.h>
2. void main ()
3. {
4. int i, j,temp;
5. int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
6. for(i = 0; i<10; i++)
7. {
8. for(j = i+1; j<10; j++)
9. {
10. if(a[j] > a[i])
11. {
12. temp = a[i];
13. a[i] = a[j];
14. a[j] = temp;
15. }
16. }
17. }
18. printf("Printing Sorted Element List ...\n");
19. for(i = 0; i<10; i++)
20. {
21. printf("%d\n",a[i]);
22. }
23. }
Program to print the largest and second largest element of the array.
1. #include<stdio.h>
2. void main ()
3. {
4. int arr[100],i,n,largest,sec_largest;
5. printf("Enter the size of the array?");
6. scanf("%d",&n);
7. printf("Enter the elements of the array?");
8. for(i = 0; i<n; i++)
9. {
10. scanf("%d",&arr[i]);
11. }
12. largest = arr[0];
13. sec_largest = arr[1];
14. for(i=0;i<n;i++)
15. {
16. if(arr[i]>largest)
17. {
18. sec_largest = largest;
19. largest = arr[i];
20. }
21. else if (arr[i]>sec_largest && arr[i]!=largest)
22. {
23. sec_largest=arr[i];
24. }
25. }
26. printf("largest = %d, second largest = %d",largest,sec_largest);
27.
28. }
Types of Arrays
The various types of arrays are as follows.
One dimensional array
Multi-dimensional array
One-Dimensional Array
A one-dimensional array is also called a single dimensional array where
the elements will be accessed in sequential order. This type of array will
be accessed by the subscript of either a column or row index.
Syntax: data-type arr_name[array_size];
Multi-Dimensional Array
When the number of dimensions specified is more than one, then it is
called as a multi-dimensional array. Multidimensional arrays include 2D
arrays and 3D arrays.
Two-Dimensional Array
The Two Dimensional array is used for representing the elements of the
array in the form of the rows and columns and these are used for
representing the Matrix. A Two Dimensional Array uses the two subscripts
for declaring the elements of the Array.
Syntax: data_type array_name[num_of_rows][num_of_column];
#include <stdio.h>
#include <stdlib.h>
int main()
{
const int ROW = 2;
const int COLUMN = 3;
int i,j;
int m[ROW][COLUMN];
return 0;
}