Array Manipulation and Search Algorithms in C

This article presents seven practical C programming exercises focused on array operations, sorting, searching, and matrix analysis. Each task emphasizes core algorithmic thinking and correct memory handling with one-dimensional and two-dimensional arrays.

Challenge 1: Descending Order Sort

Implement a bubble sort variant to arrange ten integers in descending order. The solution reads input values, applies adjacent comparisons and swaps, then prints the sorted sequence space-separated.

#include <stdio.h>
int main() {
    int nums[10], temp;
    for (int idx = 0; idx < 10; ++idx) {
        scanf("%d", &nums[idx]);
    }
    for (int pass = 0; pass < 9; ++pass) {
        for (int pos = 0; pos < 9 - pass; ++pos) {
            if (nums[pos] < nums[pos + 1]) {
                temp = nums[pos];
                nums[pos] = nums[pos + 1];
                nums[pos + 1] = temp;
            }
        }
    }
    for (int idx = 0; idx < 10; ++idx) {
        printf("%d%c", nums[idx], (idx == 9) ? '\n' : ' ');
    }
    return 0;
}

Challenge 2: First Occurrence Search

Given a integer array of size n and a target value a, locate the 1-based index of the first match. If not found, output -1. This uses linear traversal without early-exit optimizations like goto.

#include <stdio.h>
int main() {
    int n, target, arr[1000];
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &arr[i]);
    }
    scanf("%d", &target);
    int result = -1;
    for (int i = 0; i < n; ++i) {
        if (arr[i] == target) {
            result = i + 1;
            break;
        }
    }
    printf("%d\n", result);
    return 0;
}

Challenge 3: 2D Array Maximum Locator

Read an m × n matrix and identify the largest element along with its 1-based row and column indices. Input prompts are printed before reading dimensions and elements.

#include <stdio.h>
int main() {
    int rows, cols;
    int grid[10][10];
    scanf("%d,%d", &rows, &cols);
    printf("Input m, n:");
    printf("Input %d*%d array:\n", rows, cols);
    for (int r = 0; r < rows; ++r) {
        for (int c = 0; c < cols; ++c) {
            scanf("%d", &grid[r][c]);
        }
    }
    int maxVal = grid[0][0], maxRow = 0, maxCol = 0;
    for (int r = 0; r < rows; ++r) {
        for (int c = 0; c < cols; ++c) {
            if (grid[r][c] > maxVal) {
                maxVal = grid[r][c];
                maxRow = r;
                maxCol = c;
            }
        }
    }
    printf("max=%d, row=%d, col=%d\n", maxVal, maxRow + 1, maxCol + 1);
    return 0;
}

Challenge 4: Binary Search with 1-Based Indexing

Perform binary search on a sorted array of n integers. Return the smallest 1-based position where the target occurs, or None if absent. Handles edge cases including first-element matches.

#include <stdio.h>
int binarySearch(int arr[], int size, int key) {
    int left = 0, right = size - 1;
    while (left <= right) {
        int mid = left + ((right - left) >> 1);
        if (arr[mid] < key) {
            left = mid + 1;
        } else if (arr[mid] > key) {
            right = mid - 1;
        } else {
            // Found match; scan left for first occurrence
            while (mid > 0 && arr[mid - 1] == key) {
                --mid;
            }
            return mid;
        }
    }
    return -1;
}
int main() {
    int n, target;
    scanf("%d", &n);
    int data[1000000];
    for (int i = 0; i < n; ++i) {
        scanf("%d", &data[i]);
    }
    scanf("%d", &target);
    int pos = binarySearch(data, n, target);
    if (pos != -1) {
        printf("%d\n", pos + 1);
    } else {
        printf("None\n");
    }
    return 0;
}

Challenge 5: Saddle Point Detection

A saddle point is an element that is both the maximum in its row and minimum in its column. For an m × n matrix, iterate each row to find its maximum column index, then verify that same column’s minimum row index matches the current row.

#include <stdio.h>
#define MAX_DIM 10
int main() {
    int mat[MAX_DIM][MAX_DIM];
    int m, n;
    scanf("%d %d", &m, &n);
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            scanf("%d", &mat[i][j]);
        }
    }
    int found = 0;
    for (int r = 0; r < m && !found; ++r) {
        int colMax = 0;
        for (int c = 1; c < n; ++c) {
            if (mat[r][c] > mat[r][colMax]) {
                colMax = c;
            }
        }
        int rowMin = 0;
        for (int k = 1; k < m; ++k) {
            if (mat[k][colMax] < mat[rowMin][colMax]) {
                rowMin = k;
            }
        }
        if (rowMin == r) {
            printf("Array[%d][%d]=%d\n", r, colMax, mat[r][colMax]);
            found = 1;
        }
    }
    if (!found) {
        printf("None\n");
    }
    return 0;
}

Challenge 6: Remove Maximum Element

Given ten distinct integers, locate the index of the largest value and shift all subsequent elements left by one position. Output the resulting nine-element sequence.

#include <stdio.h>
#define SIZE 10
int main() {
    int values[SIZE];
    for (int i = 0; i < SIZE; ++i) {
        scanf("%d", &values[i]);
    }
    int maxIdx = 0;
    for (int i = 1; i < SIZE; ++i) {
        if (values[i] > values[maxIdx]) {
            maxIdx = i;
        }
    }
    for (int i = maxIdx; i < SIZE - 1; ++i) {
        values[i] = values[i + 1];
    }
    for (int i = 0; i < SIZE - 1; ++i) {
        printf("%d%c", values[i], (i == SIZE - 2) ? '\n' : ' ');
    }
    return 0;
}

Challenge 7: Pascal’s Triangle Generation

Construct and print the first 10 rows of Pascal’s triangle using dynamic programming: each interior element equals the sum of the two elements above it. Boundary value are always 1.

#include <stdio.h>
int main() {
    int triangle[10][10] = {0};
    for (int row = 0; row < 10; ++row) {
        for (int col = 0; col <= row; ++col) {
            if (col == 0 || col == row) {
                triangle[row][col] = 1;
            } else {
                triangle[row][col] = triangle[row-1][col-1] + triangle[row-1][col];
            }
        }
    }
    for (int row = 0; row < 10; ++row) {
        for (int col = 0; col <= row; ++col) {
            printf("%d", triangle[row][col]);
            if (col < row) printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Tags: c-programming Arrays Sorting searching matrix-algorithms

Posted on Sun, 06 Sep 2026 16:45:15 +0000 by mrskhris