Arrays in C

1D Arrays • 2D Arrays • Storage • Traversal • Applications

1. Introduction to Arrays

An array is a collection of elements of the same data type, stored in contiguous memory locations.

Arrays allow storing multiple values using a single variable name with an index.

  • Fast access using index
  • Stored sequentially in memory
  • Useful for loops, searching, sorting, and matrix operations

2. Declaring & Initializing Arrays

General syntax:

#include <stdio.h>

int main() {
    int array[5];
    return 0;
}
Example
#include <stdio.h>

int main() {
    int marks[5];
    int numbers[5] = {10,20,30,40,50};

    printf("numbers[2] = %d", numbers[2]);
    return 0;
}

Indexes start at 0 and end at size − 1.

3. Accessing Elements

You can read or modify elements using their index:

#include <stdio.h>

int main() {
    int numbers[5] = {10,20,30,40,50};

    printf("%d", numbers[2]);   // prints 30
    numbers[4] = 100;

    printf("\nUpdated: %d", numbers[4]);
    return 0;
}

4. Traversing a 1D Array

#include <stdio.h>

int main() {
    int arr[5] = {1,2,3,4,5};

    for (int i = 0; i < 5; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

5. 2D Arrays (Matrices)

#include <stdio.h>

int main() {
    int matrix[2][3] = {
        {1, 2, 3},
        {4, 5, 6}
    };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

6. How Arrays Are Stored in Memory

+--------+--------+--------+--------+--------+
| a[0]   | a[1]   | a[2]   | a[3]   | a[4]   |
+--------+--------+--------+--------+--------+

7. Common Mistakes

#include <stdio.h>

int main() {
    int a[3] = {10, 20, 30};

    printf("%d", a[2]);   // correct
    // printf("%d", a[3]);  // OUT OF RANGE

    return 0;
}

8. Read & Print 5 Numbers

#include <stdio.h>

int main() {
    int a[5];

    printf("Enter 5 numbers: ");
    for (int i = 0; i < 5; i++) {
        scanf("%d", &a[i]);
    }

    printf("You entered: ");
    for (int i = 0; i < 5; i++) {
        printf("%d ", a[i]);
    }

    return 0;
}

9. Largest, Smallest & Average

#include <stdio.h>

int main() {
    int a[5], max, min;
    float avg = 0;

    printf("Enter 5 numbers: ");
    for(int i=0; i<5; i++) {
        scanf("%d", &a[i]);
        avg += a[i];
    }

    max = min = a[0];

    for(int i=1; i<5; i++) {
        if(a[i] > max) max = a[i];
        if(a[i] < min) min = a[i];
    }

    printf("Max = %d\n", max);
    printf("Min = %d\n", min);
    printf("Average = %.2f\n", avg / 5);

    return 0;
}

10. Searching in an Array

#include <stdio.h>

int main() {
    int a[5] = {10, 20, 30, 40, 50};
    int key, found = 0;

    printf("Enter number to search: ");
    scanf("%d", &key);

    for (int i = 0; i < 5; i++) {
        if (a[i] == key) {
            printf("Found at index %d\n", i);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Not found\n");
    }

    return 0;
}

11. Practice Questions

  • Print array elements in reverse.
  • Count even and odd numbers.
  • Print only positive numbers from 10 inputs.
  • Store 7 days & print the 3rd day.
  • Find the second largest number.

Try Code in Online Compiler