Skip to main content

15. Arrays

 

C Array

An array is defined as the collection of similar type of data items stored at continuous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structures etc.
The array is the simplest data structure where each data element can be randomly accessed by using its index number. A 1-D array is like a list and a 2-D array is like a table. Some texts refer to 1-D arrays as vectors, 2-D arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

Properties of Array

  • Each element of an array is of same data type and carries the same size.
  • Elements of the array are stored at continuous memory locations where the first element is stored at the smallest memory location.
  • 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.

Advantages of C Array

  • Code Optimization: Less code to access the data.
  • Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
  • Ease of sorting: To sort the elements of the array, we need a few lines of code only.
  • Random Access: We can access any element randomly using the array.

Disadvantages of C Array

  • Poor time complexity of insertion and deletion operation.
  • Wastage of memory since arrays are fixed in size.
  • If there is enough space present in the memory but not in continuous form, we will not be able to initialize our array.
  • It is not possible to increase the size of the array, once we have declared the array.

Declaration and Initialization of C Array

#include <stdio.h>

int main()
{
   int i = 0;
   int marks[5];     /* Declaration of Array */
   marks[0] = 80;     /* Initialization of Array */
   marks[1] = 60;
   marks[2] = 70;
   marks[3] = 85;
   marks[4] = 75;
   for (i = 0; i < 5; i++){
      printf("%d ", marks[i]);
   }
   return 0;
}

Output :

80 60 70 85 75

#include <stdio.h>

int main()
{
   int j = 0;
   int score[5] = {80, 60, 70, 85, 75};     /* Declaration and Initialization */
   for (j = 0; j < 5; j++){
      printf("%d ", score[j]);    }
   return 0;
}

Output :

80 60 70 85 75

Program to find the largest and second largest element of the array

#include <stdio.h>

int main()
{
   int arr[100], x, n, largest, sec_largest;
   printf("Enter the size of the array : ");
   scanf("%d", &n);
   printf("Enter the elements of the array : ");
   for (x = 0; x < n; x++){
      scanf("%d", &arr[x]);
   }
   largest = arr[0];
   sec_largest = arr[1];
   for (x = 0; x < n; x++){
      if (arr[x] > largest){
         sec_largest = largest;
         largest = arr[x];
      }
      if ((arr[x] > sec_largest) && (arr[x] != largest)){
         sec_largest = arr[x];
      }
   }
   printf("Largest = %d \nSecond Largest = %d", largest, sec_largest);
   return 0;
}

Output :

Enter the elements of the array : 34 76 99 98 78 79
Largest = 99
Second Largest = 98

2-D Array in C

The two-dimensional array can be defined as an array of arrays. The 2-D array is organized as matrices which can be represented as the collection of rows and columns. However, 2-D arrays are created to implement a relational database look alike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions whenever required.

Declaration and Initialization of Array

#include <stdio.h>

int main()
{
   int i = 0, j = 0;
   int arr[4][3] = {{1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5 ,6}};
   for (i = 0; i < 4; i++){
      for (j = 0; j < 3; j++){
         printf("arr[%d] [%d] = %d\n", i, j, arr[i][j]);
      }
   }
   return 0;
}

Output :

arr[0] [0] = 1
arr[0] [1] = 2
arr[0] [2] = 3
arr[1] [0] = 2
arr[1] [1] = 3
arr[1] [2] = 4
arr[2] [0] = 3
arr[2] [1] = 4
arr[2] [2] = 5
arr[3] [0] = 4
arr[3] [1] = 5
arr[3] [2] = 6

Storing elements in a matrix and printing it

#include <stdio.h>

int main()
{
   int arr[3][3], x, y;
   for (x = 0; x < 3; x++){
      for (y = 0; y < 3; y++){
         printf("Enter a[%d][%d] : ", x ,y);
         scanf("%d", &arr[x][y]);
      }
   }
   printf("\nPrinting the elements ......\n");
   for (x = 0; x < 3; x++){
      printf("\n");
      for (y = 0; y < 3; y++){
         printf("%d\t", arr[x][y]);
      }
   }
   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

Printing the elements ......

1            2            3
4            5            6
7            8            9

Comments