Reading history

Traverse an Array in Reverse Order in C

10 min read 3 views

Print or process a C array from the last element to the first with for loops, while loops and pointers, and avoid the unsigned index trap in reverse loops.

Reverse traversal visits an array from its last element back to the first. It is how you print a list backwards, undo actions in order, or scan from the right in many array problems.


Visiting Elements From Last to First

What is Array Reverse Traversal?

Reverse traversal (also called backward traversal) is visiting array elements starting from the last element (index n-1) and moving toward the first element (index 0). It is the opposite of forward traversal, which starts at index 0 and moves to index n-1.

Forward traversal:

Index:  0 → 1 → 2 → 3 → 4
Value:  10  20  30  40  50

Reverse traversal:

Index:  4 → 3 → 2 → 1 → 0
Value:  50  40  30  20  10

Why Reverse Traversal Matters

  • String reversal: Reversing characters in a string.
  • Palindrome checking: Compare elements from both ends.
  • Processing recent data first: LIFO-like operations on arrays.
  • Algorithm implementation: Some algorithms naturally process data backward.
  • Avoid shifting: Removing elements from the end is more efficient than from the front.

Array Indexing Recap

  • Arrays use zero-based indexing.
  • First element: arr[0]
  • Last element: arr[n-1] (where n is the array size)
  • Valid indices: 0 to n-1

Reverse Traversal Logic

For an array of size n:

for (int i = n - 1; i >= 0; i--) {
    // Process arr[i]
}

The loop starts at the last index and decrements until reaching index 0.


Index and Pointer Loops That Count Down

Basic Reverse Traversal

Syntax:

for (int i = size - 1; i >= 0; i--) {
    // Access arr[i]
}

Example:

#include <stdio.h>

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    printf("Reverse traversal: ");
    for (int i = n - 1; i >= 0; i--) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

Expected output:

Original array: 10 20 30 40 50
Reverse traversal: 50 40 30 20 10

Reverse Traversal with while Loop

#include <stdio.h>

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);
    int i = n - 1;

    printf("Reverse traversal (while loop): ");
    while (i >= 0) {
        printf("%d ", arr[i]);
        i--;
    }
    printf("\n");

    return 0;
}

Expected output:

Reverse traversal (while loop): 50 40 30 20 10

Reverse Traversal with Pointers

Using pointers for reverse traversal can be more efficient:

#include <stdio.h>

int main(void) {
    int arr[] = {10, 20, 30, 40, 50};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Reverse traversal (pointers): ");
    for (int *p = arr + n - 1; p >= arr; p--) {
        printf("%d ", *p);
    }
    printf("\n");

    return 0;
}

Expected output:

Reverse traversal (pointers): 50 40 30 20 10

Common Use Cases

1. Print Array in Reverse

#include <stdio.h>

void printReverse(const int arr[], int n) {
    for (int i = n - 1; i >= 0; i--) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main(void) {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    printReverse(arr, n);
    return 0;
}

2. Reverse a String

#include <stdio.h>
#include <string.h>

void reverseString(char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - 1 - i];
        str[len - 1 - i] = temp;
    }
}

int main(void) {
    char str[] = "Hello, World!";
    printf("Original: %s\n", str);
    reverseString(str);
    printf("Reversed: %s\n", str);
    return 0;
}

Expected output:

Original: Hello, World!
Reversed: !dlroW ,olleH

3. Check Palindrome

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isPalindrome(const char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        if (str[i] != str[len - 1 - i]) {
            return false;
        }
    }
    return true;
}

int main(void) {
    char str1[] = "racecar";
    char str2[] = "hello";

    printf("%s is %s\n", str1, isPalindrome(str1) ? "palindrome" : "not palindrome");
    printf("%s is %s\n", str2, isPalindrome(str2) ? "palindrome" : "not palindrome");
    return 0;
}

Expected output:

racecar is palindrome
hello is not palindrome

Reverse Traversal Programs in C

This program demonstrates array reverse traversal in a realistic scenario: a data logger that records temperature readings and displays them in reverse chronological order.

Program: Temperature Data Logger

/**
 * temperature_logger.c
 * Demonstrates array reverse traversal for displaying recent data first.
 * Records temperature readings and displays them in reverse order.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define MAX_READINGS 100
#define MAX_LINE_LEN 100

/* Function prototypes */
int readDouble(const char *prompt, double *outValue);
void displayForward(const double readings[], int count);
void displayReverse(const double readings[], int count);
void displayStatistics(const double readings[], int count);
void reverseArray(double arr[], int count);
int findPeakTemperature(const double readings[], int count);

int main(void) {
    double temperatures[MAX_READINGS];
    int readingCount = 0;
    int choice;
    char buffer[MAX_LINE_LEN];

    printf("=== Temperature Data Logger ===\n");

    while (1) {
        printf("\n--- Menu ---\n");
        printf("1. Add Temperature Reading\n");
        printf("2. Display Readings (Oldest First)\n");
        printf("3. Display Readings (Newest First - Reverse)\n");
        printf("4. Display Statistics\n");
        printf("5. Reverse the Array In-Place\n");
        printf("6. Exit\n");
        printf("Enter choice: ");
        fflush(stdout);

        if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
            fprintf(stderr, "Error reading choice.\n");
            return EXIT_FAILURE;
        }

        choice = atoi(buffer);

        switch (choice) {
            case 1: {
                if (readingCount >= MAX_READINGS) {
                    printf("Maximum readings reached.\n");
                    break;
                }

                double temp;
                if (readDouble("Enter temperature (°C): ", &temp) != 0) {
                    fprintf(stderr, "Invalid temperature.\n");
                    break;
                }

                temperatures[readingCount++] = temp;
                printf("Temperature recorded: %.2f°C\n", temp);
                break;
            }

            case 2:
                displayForward(temperatures, readingCount);
                break;

            case 3:
                displayReverse(temperatures, readingCount);
                break;

            case 4:
                displayStatistics(temperatures, readingCount);
                break;

            case 5:
                reverseArray(temperatures, readingCount);
                printf("Array reversed in-place.\n");
                displayForward(temperatures, readingCount);
                break;

            case 6:
                printf("Exiting.\n");
                return EXIT_SUCCESS;

            default:
                printf("Invalid choice. Please try again.\n");
                break;
        }
    }

    return EXIT_SUCCESS;
}

/* Displays readings in forward (oldest first) order */
void displayForward(const double readings[], int count) {
    if (count == 0) {
        printf("No readings to display.\n");
        return;
    }

    printf("\n=== Readings (Oldest First) ===\n");
    for (int i = 0; i < count; i++) {
        printf("Reading %d: %.2f°C\n", i + 1, readings[i]);
    }
}

/* Displays readings in reverse (newest first) order */
void displayReverse(const double readings[], int count) {
    if (count == 0) {
        printf("No readings to display.\n");
        return;
    }

    printf("\n=== Readings (Newest First - Reverse) ===\n");
    for (int i = count - 1; i >= 0; i--) {
        printf("Reading %d: %.2f°C\n", i + 1, readings[i]);
    }
}

/* Displays statistics: min, max, average */
void displayStatistics(const double readings[], int count) {
    if (count == 0) {
        printf("No readings to analyze.\n");
        return;
    }

    double min = readings[0];
    double max = readings[0];
    double sum = 0.0;

    for (int i = 0; i < count; i++) {
        if (readings[i] < min) min = readings[i];
        if (readings[i] > max) max = readings[i];
        sum += readings[i];
    }

    double average = sum / count;

    printf("\n=== Temperature Statistics ===\n");
    printf("Total readings: %d\n", count);
    printf("Minimum: %.2f°C\n", min);
    printf("Maximum: %.2f°C\n", max);
    printf("Average: %.2f°C\n", average);

    /* Find peak temperature using reverse traversal */
    int peakIndex = findPeakTemperature(readings, count);
    if (peakIndex >= 0) {
        printf("Peak temperature: %.2f°C at reading %d\n",
               readings[peakIndex], peakIndex + 1);
    }
}

/* Reverses an array in-place */
void reverseArray(double arr[], int count) {
    for (int i = 0; i < count / 2; i++) {
        double temp = arr[i];
        arr[i] = arr[count - 1 - i];
        arr[count - 1 - i] = temp;
    }
}

/* Finds the index of the highest temperature */
int findPeakTemperature(const double readings[], int count) {
    if (count == 0) {
        return -1;
    }

    int peakIndex = 0;
    for (int i = 1; i < count; i++) {
        if (readings[i] > readings[peakIndex]) {
            peakIndex = i;
        }
    }
    return peakIndex;
}

/* Reads a double safely */
int readDouble(const char *prompt, double *outValue) {
    char buffer[MAX_LINE_LEN];
    char *endPtr;
    double val;

    printf("%s", prompt);
    fflush(stdout);

    if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
        return -1;
    }

    errno = 0;
    val = strtod(buffer, &endPtr);

    if (endPtr == buffer || (*endPtr != '\n' && *endPtr != '\0')) {
        return -1;
    }

    if (errno == ERANGE) {
        return -1;
    }

    *outValue = val;
    return 0;
}

Explanation of Reverse Traversal Usage

  1. displayReverse: Uses for (int i = count - 1; i >= 0; i--) to display newest readings first.
  2. reverseArray: Swaps elements from both ends to reverse the array in-place.
  3. findPeakTemperature: Forward traversal, but reverse traversal could find the last peak.
  4. Pointer-based traversal: Could be used for efficiency (not shown for simplicity).

Expected Output (sample run)

=== Temperature Data Logger ===

--- Menu ---
1. Add Temperature Reading
2. Display Readings (Oldest First)
3. Display Readings (Newest First - Reverse)
4. Display Statistics
5. Reverse the Array In-Place
6. Exit
Enter choice: 1
Enter temperature (°C): 22.5
Temperature recorded: 22.50°C

--- Menu ---
...
Enter choice: 1
Enter temperature (°C): 25.3
Temperature recorded: 25.30°C

--- Menu ---
...
Enter choice: 1
Enter temperature (°C): 19.8
Temperature recorded: 19.80°C

--- Menu ---
...
Enter choice: 3

=== Readings (Newest First - Reverse) ===
Reading 3: 19.80°C
Reading 2: 25.30°C
Reading 1: 22.50°C

--- Menu ---
...
Enter choice: 4

=== Temperature Statistics ===
Total readings: 3
Minimum: 19.80°C
Maximum: 25.30°C
Average: 22.53°C
Peak temperature: 25.30°C at reading 2

--- Menu ---
...
Enter choice: 5
Array reversed in-place.

=== Readings (Oldest First) ===
Reading 1: 19.80°C
Reading 2: 25.30°C
Reading 3: 22.50°C

The Unsigned Index and Off-by-One Traps

1. Off-by-One Error in Loop Condition

Using i >= 0 with size_t (unsigned) causes infinite loop because size_t cannot be negative.

size_t n = 5;
for (size_t i = n - 1; i >= 0; i--) {   // BUG: i is unsigned, infinite loop
    printf("%d\n", arr[i]);
}

Solution: Use int for the loop variable, or use a different loop structure:

for (size_t i = n; i > 0; i--) {
    printf("%d\n", arr[i - 1]);
}

2. Accessing Before Array Start

Decrementing a pointer before the array start is undefined behavior.

int *p = arr + n - 1;
while (p >= arr) {
    printf("%d ", *p);
    p--;   // After last iteration, p points before arr
}

Solution: Ensure the pointer doesn’t go before the array. Use index-based loops for safety.

3. Empty Array Handling

Reverse traversal of an empty array (count = 0) causes count - 1 to be -1, leading to out-of-bounds access.

int arr[5];
int count = 0;
for (int i = count - 1; i >= 0; i--) {   // BUG: i starts at -1
    printf("%d\n", arr[i]);
}

Solution: Check for count > 0 before reverse traversal.

4. Reversing Strings with strlen

Using strlen inside the loop condition repeatedly is inefficient.

for (int i = strlen(str) - 1; i >= 0; i--) {   // strlen called each iteration
    printf("%c", str[i]);
}

Solution: Store the length in a variable: int len = strlen(str);

5. In-Place Reversal Loop Bound

Using count instead of count / 2 reverses the array twice.

void reverseArray(int arr[], int count) {
    for (int i = 0; i < count; i++) {   // BUG: reverses twice
        int temp = arr[i];
        arr[i] = arr[count - 1 - i];
        arr[count - 1 - i] = temp;
    }
}

Solution: Loop to count / 2 only.

6. Read-Only Arrays

Attempting to reverse a const array is a compile error.

void reverseArray(const int arr[], int count) {
    for (int i = 0; i < count / 2; i++) {
        int temp = arr[i];
        arr[i] = arr[count - 1 - i];   // Error: assignment to read-only location
        arr[count - 1 - i] = temp;
    }
}

Solution: Remove const if modification is intended, or create a new array.

7. Null Pointer

Passing NULL to a reverse traversal function causes a crash.

int *arr = NULL;
reverseArray(arr, 5);   // BUG: arr is NULL

Solution: Check for NULL before traversing.

8. Memory Considerations

  • Reverse traversal does not allocate extra memory.
  • In-place reversal swaps elements, using O(1) extra space.
  • Creating a reversed copy requires O(n) extra space.
  • Pointer-based traversal may be slightly faster than index-based.

9. Multidimensional Arrays

Reverse traversal of 2D arrays requires nested loops with both dimensions reversed.

for (int i = rows - 1; i >= 0; i--) {
    for (int j = cols - 1; j >= 0; j--) {
        printf("%d ", matrix[i][j]);
    }
}

10. Caching Performance

Reverse traversal may have slightly worse cache performance for large arrays because it accesses memory in the opposite direction of typical allocation.

Solution: Use reverse traversal only when needed; forward traversal is generally cache-friendly.


Reverse Traversal Tips

Array reverse traversal is a fundamental technique for processing array elements from last to first. It is essential for string reversal, palindrome checking, and displaying recent data first.

Key Takeaways

  • Reverse traversal starts at index n-1 and moves to 0.
  • Use for (int i = n - 1; i >= 0; i--) with int loop variable.
  • Pointer-based traversal: for (int *p = arr + n - 1; p >= arr; p--).
  • In-place reversal swaps elements from both ends.
  • Check for empty arrays and null pointers.
  • Use count / 2 for in-place reversal loops.

Best Practices

  1. Use int for loop variable in reverse traversal to avoid unsigned integer issues.
  2. Check for empty arrays (count > 0) before reverse traversal.
  3. Store strlen result in a variable for efficiency.
  4. Use count / 2 for in-place reversal loops.
  5. Document the direction of traversal in comments.
  6. Use const for read-only traversals to prevent accidental modification.
  7. Prefer index-based loops over pointer arithmetic for readability.
  8. Consider caching effects for large arrays.
  9. Test with edge cases: empty array, single element, two elements.
  10. Use memmove or memcpy for bulk data reversal if available.

By mastering array reverse traversal, you’ll write efficient and correct C programs for a wide range of applications, from string manipulation to data processing.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *