×
>
<

Aptitude

C Arrays | CrackEase

Arrays

Introduction to arrays

An array is a collection of elements of the same data type stored at contiguous memory locations. Arrays let you group many values under one name and access them using an index.

Arrays are useful for storing lists of values (numbers, characters, etc.), performing iterative operations and implementing data structures. In C, array memory is contiguous and indexing starts at 0.

arrays introduction
Array
All elements of an array are stored sequentially in memory and accessed by an index. Important points:
  • Array indices in C start at 0.
  • The array size is fixed at compile time for static arrays — you cannot change the size of a static array at runtime.
  • All elements must be of the same data type.

Creating an array in C

Declare an array by specifying its type, name and size:
  
data_type array_name[size];
  
Example:
  
int sum[5]; 
  
This declares an array named sum that can hold 5 integers (indices 0..4).

Initializing an array

You can assign each element individually:
  
sum[0] = 30;  // initialization of array elements
sum[1] = 50;
sum[2] = 24;
sum[3] = 60;
sum[4] = 78;
  
Or initialize at declaration:
int sum[5] = {30, 50, 24, 60, 78};
C Array Example
  
#include <stdio.h>
int main(void)
{
     int i = 0;
     int sum[5];  /* declaration of array */
     sum[0] = 30; /* initialization of array */
     sum[1] = 50;
     sum[2] = 24;
     sum[3] = 60;
     sum[4] = 78;

     /* traversal of array */
     for (i = 0; i < 5; i++)
     {
         printf("%d\n", sum[i]);
     }

     return 0;
}
  
output
  
30
50
24
60
78
  
array initialization
Single Dimensional Array

In a single dimensional (1-D) array you store elements in a single linear sequence. You declare the array once and then use index positions to read/write elements.

  
#include <stdio.h>
int main(void)
{
   int arr[5];       /* declaring the array */
   int i;
   for (i = 0; i < 5; i++)    /* loop to take input in the array */
   {
       scanf("%d", &arr[i]);
   }
   for (i = 0; i < 5; i++)    /* loop to print the elements */
   {
       printf("%d\n", arr[i]);
   }
   return 0;
}
  
example input/output
  
input: 1 2 3 4 5
output:
1
2
3
4
5
  

Note : An array of size 5 has indices 0..4. The first element is at index 0.

Initializing one dimensional array
1d array

Ways to initialize a 1-D array:

  
int marks[5];
marks[0] = 19;
marks[1] = 11;
marks[2] = 14;
marks[3] = 8;
marks[4] = 45;
  
OR
  
int marks[5] = {19, 11, 14, 8, 45};
  

Both forms create the same array. This is a one-dimensional array (1-D).

Multi Dimensional Array

A multi-dimensional array (commonly 2-D) stores data in a grid (rows and columns). It is useful for matrices, tables and many algorithms.

The simplest multi-dimensional array is a 2-D array.

Initializing a 2-D Array

Examples:

  
int arr[3][4] = {1,2,3,4, 2,3,4,5, 3,4,5,6}; 
/* or equivalently */
int arr[3][4] = {
  {1,2,3,4},
  {2,3,4,5},
  {3,4,5,6}
};
  
  
Matrix view:
1 2 3 4
2 3 4 5
3 4 5 6
  
Multi Dimensional Array (continued)
2d array

Multi Dimensional Array

A 2-D array is accessed using two indices: arr[row][col]. Typical loops to traverse a 2-D array use nested loops (one for rows and one for columns).

Example of Multi-dimensional array
  
#include <stdio.h>

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