Aral Code

Arrays & Functions Lesson 9 of 16

C Arrays and 2D Arrays

Store many values in C arrays, loop through them, find the sum, average, highest and lowest, sort with bubble sort, and use 2D arrays for tables.

An array stores several values of the same type in consecutive memory locations. Instead of grade1, grade2, grade3, you write one array grades[3].

Declaring arrays

int grades[5] = {88, 92, 79, 95, 84};
double prices[] = {25.0, 8.0, 9.0};     /* size counted automatically: 3 */
int scores[10] = {0};                   /* all ten set to 0 */

Indexes start at 0, so grades[0] is 88 and grades[4] is 84.

Sum, average, highest, lowest

#include <stdio.h>

int main(void) {
    int grades[] = {88, 92, 79, 95, 84};
    int n = sizeof grades / sizeof grades[0];
    int sum = 0, high = grades[0], low = grades[0];

    for (int i = 0; i < n; i++) {
        sum += grades[i];
        if (grades[i] > high) high = grades[i];
        if (grades[i] < low) low = grades[i];
    }
    printf("Count: %d\n", n);
    printf("Average: %.2f\n", (double) sum / n);
    printf("Highest: %d, Lowest: %d\n", high, low);
    return 0;
}

Output

Count: 5
Average: 87.60
Highest: 95, Lowest: 79

C arrays don't know their own length. sizeof grades / sizeof grades[0] (total bytes ÷ bytes per item) computes it, but this only works where the array was declared, not inside a function that receives it.

Reading values into an array

#include <stdio.h>

int main(void) {
    double prices[3];
    for (int i = 0; i < 3; i++) {
        printf("Price of item %d: ", i + 1);
        scanf("%lf", &prices[i]);
    }
    double total = 0;
    for (int i = 0; i < 3; i++) total += prices[i];
    printf("Total: %.2f\n", total);
    return 0;
}

Output

Price of item 1: 25
Price of item 2: 8.50
Price of item 3: 15
Total: 48.50

Bubble sort

A classic way to sort: repeatedly compare neighbors and swap them if they're in the wrong order. The biggest values "bubble" to the end:

#include <stdio.h>

int main(void) {
    int a[] = {64, 25, 12, 90, 33};
    int n = 5;
    for (int pass = 0; pass < n - 1; pass++) {
        for (int i = 0; i < n - 1 - pass; i++) {
            if (a[i] > a[i + 1]) {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }
    }
    for (int i = 0; i < n; i++) printf("%d ", a[i]);
    printf("\n");
    return 0;
}

Output

12 25 33 64 90

The three-line swap with temp is a pattern worth memorizing. For real programs, C's library has qsort in <stdlib.h>, which is much faster for big arrays.

Searching

#include <stdio.h>

int main(void) {
    int ids[] = {1021, 1045, 1098, 1102, 1150};
    int target = 1098, found = -1;
    for (int i = 0; i < 5; i++) {
        if (ids[i] == target) {
            found = i;
            break;
        }
    }
    if (found >= 0) printf("ID %d found at index %d\n", target, found);
    else printf("ID %d not found\n", target);
    return 0;
}

Output

ID 1098 found at index 2

2D arrays

#include <stdio.h>

int main(void) {
    /* 3 students x 3 subjects */
    int scores[3][3] = {
        {88, 91, 85},
        {76, 82, 79},
        {95, 93, 97}
    };
    const char *names[] = {"Ana", "Ben", "Carla"};

    for (int s = 0; s < 3; s++) {
        int sum = 0;
        for (int j = 0; j < 3; j++) sum += scores[s][j];
        printf("%-6s average: %.2f\n", names[s], sum / 3.0);
    }
    return 0;
}

Output

Ana    average: 88.00
Ben    average: 79.00
Carla  average: 95.00

scores[row][column]. Here each row is a student and each column a subject. const char *names[] is an array of strings, explained in the Strings and Pointers lessons.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What is the index of the last item in int a[10]?
2. What happens if you access a[20] in a 10-item array?
3. What does sizeof a / sizeof a[0] compute?
4. In bubble sort, what moves to the end after the first pass?

By Sir Reyel · Last updated 2026-12-04