Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C Programming Exercises on Arrays and Matrices

Tech May 11 2

Task 1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE 4
#define ROWS 2

void demo1() {
    int arr[SIZE] = {1, 9, 8, 4};
    int i;
    // Output memory size of array arr
    printf("sizeof(arr) = %zu\n", sizeof(arr));
    // Output address and value of each element
    for (i = 0; i < SIZE; ++i)
        printf("%p: %d\n", &arr[i], arr[i]);
    // Output the value of the array name arr
    printf("arr = %p\n", arr);
}

void demo2() {
    int arr[ROWS][SIZE] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
    int i, j;
    // Output memory size of 2D array arr
    printf("sizeof(arr) = %zu\n", sizeof(arr));
    // Output address and value of each element
    for (i = 0; i < ROWS; ++i)
        for (j = 0; j < SIZE; ++j)
            printf("%p: %d\n", &arr[i][j], arr[i][j]);
    printf("\n");
    // Output values of arr, arr[0], arr[1]
    printf("arr = %p\n", arr);
    printf("arr[0] = %p\n", arr[0]);
    printf("arr[1] = %p\n", arr[1]);
    printf("\n");
}

int main() {
    printf("Test 1: 1D array of int\n");
    demo1();
    printf("\nTest 2: 2D array of int\n");
    demo2();
    return 0;
}

Results: ![Screenshot 2025-11-10 225125]

Quession 1: Elements are stored contiguously, each separated by 4 bytes; the array name and the address of the first element are the same.

Question 2: Yes; they are the same; the difference is 16 bytes, representing one row storing 4 integer variables.

Task 2

#include <stdio.h>
#define MAX 100

// Function declarations
void input(int scores[], int count);
double compute(int scores[], int count);

int main() {
    int scores[MAX];
    int count, i;
    double result;
    while (printf("Enter count: "), scanf("%d", &count) != EOF) {
        input(scores, count);
        result = compute(scores, count);
        printf("result = %.2f\n\n", result);
    }
    return 0;
}

// Function definitions
void input(int scores[], int count) {
    for (int i = 0; i < count; ++i)
        scanf("%d", &scores[i]);
}

double compute(int scores[], int count) {
    int i, high, low;
    double sum = 0;
    high = low = scores[0];
    for (i = 0; i < count; ++i) {
        sum += scores[i];
        if (scores[i] > high)
            high = scores[i];
        else if (scores[i] < low)
            low = scores[i];
    }
    return (sum - high - low) / (count - 2);
}

Results: ![Screenshot 2025-11-10 230406]

Question 1: In function input, the formal parameter for the 1D array is int scores[], and the actual parameter is scores.

Question 2: input reads input data and stores it in the array; compute returns the average after dropping the highest and lowest values.

Task 3

#include <stdio.h>
#define MAX 100

// Function declarations
void output(int matrix[][MAX], int size);
void init(int matrix[][MAX], int size, int value);

int main() {
    int matrix[MAX][MAX];
    int size, value;
    while (printf("Enter size and value: "), scanf("%d%d", &size, &value) != EOF) {
        init(matrix, size, value);
        output(matrix, size);
        printf("\n");
    }
    return 0;
}

void output(int matrix[][MAX], int size) {
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j)
            printf("%d ", matrix[i][j]);
        printf("\n");
    }
}

void init(int matrix[][MAX], int size, int value) {
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j)
            matrix[i][j] = value;
}

Results: ![Screenshot 2025-11-11 003937]

Question 1: Formal parameter: int matrix[][MAX]; actual parameter: matrix.

Quession 2: No, it will cause an error (the second dimension must be specified).

![Screenshot 2025-11-11 093016]

Task 4

#include <stdio.h>
#define MAX 100

// Function declarations
void input(int numbers[], int count);
double median(int numbers[], int count);

int main() {
    int numbers[MAX];
    int count;
    double result;
    while (printf("Enter count: "), scanf("%d", &count) != EOF) {
        input(numbers, count);
        result = median(numbers, count);
        printf("result = %g\n\n", result);
    }
    return 0;
}

void input(int numbers[], int count) {
    for (int i = 0; i < count; i++) {
        scanf("%d", &numbers[i]);
    }
}

double median(int numbers[], int count) {
    // Bubble sort in descending order
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < count - i - 1; j++) {
            if (numbers[j + 1] > numbers[j]) {
                int tmp = numbers[j];
                numbers[j] = numbers[j + 1];
                numbers[j + 1] = tmp;
            }
        }
    }
    if (count % 2 == 1)
        return numbers[count / 2];
    else
        return (numbers[count / 2] + numbers[count / 2 - 1]) / 2.0;
}

Results: ![Screenshot 2025-11-11 094333]

Task 5

#include <stdio.h>
#define MAX 100

// Function declarations
void input(int matrix[][MAX], int size);
void output(int matrix[][MAX], int size);
void rotate_right(int matrix[][MAX], int size);

int main() {
    int matrix[MAX][MAX];
    int size;
    printf("Enter size: ");
    scanf("%d", &size);
    input(matrix, size);
    printf("Original matrix:\n");
    output(matrix, size);
    rotate_right(matrix, size);
    printf("Matrix after rotation:\n");
    output(matrix, size);
    return 0;
}

void input(int matrix[][MAX], int size) {
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j)
            scanf("%d", &matrix[i][j]);
}

void output(int matrix[][MAX], int size) {
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j)
            printf("%4d", matrix[i][j]);
        printf("\n");
    }
}

void rotate_right(int matrix[][MAX], int size) {
    int temp[MAX][MAX];
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size - 1; j++) {
            temp[i][j + 1] = matrix[i][j];
        }
        temp[i][0] = matrix[i][size - 1];
    }
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            matrix[i][j] = temp[i][j];
}

Results: ![Screenshot 2025-11-11 115646]

Task 6

#include <stdio.h>
#define MAX 100

void dec_to_n(int x, int n);

int main() {
    int x;
    while (printf("Enter a decimal integer: "), scanf("%d", &x) != EOF) {
        dec_to_n(x, 2);
        dec_to_n(x, 8);
        dec_to_n(x, 16);
        printf("\n");
    }
    return 0;
}

void dec_to_n(int x, int n) {
    char digits[] = "0123456789ABCDEF";
    char result[32];
    int index = 0;
    if (x == 0) {
        printf("0\n");
        return;
    }
    while (x > 0) {
        result[index++] = digits[x % n];
        x /= n;
    }
    for (int i = index - 1; i >= 0; i--)
        printf("%c", result[i]);
    printf("\n");
}

Results: ![Screenshot 2025-11-11 161055]

Task 7

#include <stdio.h>
#define MAX 100

void input(int matrix[][MAX], int size);
void output(int matrix[][MAX], int size);
int is_magic(int matrix[][MAX], int size);

int main() {
    int matrix[MAX][MAX];
    int size;
    while (printf("Enter size: "), scanf("%d", &size) != EOF) {
        printf("Enter matrix:\n");
        input(matrix, size);
        printf("Matrix:\n");
        output(matrix, size);
        if (is_magic(matrix, size))
            printf("It is a magic square\n\n");
        else
            printf("It is not a magic square\n\n");
    }
    return 0;
}

void input(int matrix[][MAX], int size) {
    for (int i = 0; i < size; ++i)
        for (int j = 0; j < size; ++j)
            scanf("%d", &matrix[i][j]);
}

void output(int matrix[][MAX], int size) {
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j)
            printf("%4d", matrix[i][j]);
        printf("\n");
    }
}

int is_magic(int matrix[][MAX], int size) {
    int total = size * size;
    int seen[total];
    for (int i = 0; i < total; i++) seen[i] = 0;

    // Check that numbers are 1..total and no duplicates
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            int val = matrix[i][j];
            if (val < 1 || val > total) return 0;
            if (seen[val - 1]) return 0;
            seen[val - 1] = 1;
        }
    }

    // Compute reference sum from first row
    int target = 0;
    for (int j = 0; j < size; j++) target += matrix[0][j];

    // Check rows
    for (int i = 0; i < size; i++) {
        int sum = 0;
        for (int j = 0; j < size; j++) sum += matrix[i][j];
        if (sum != target) return 0;
    }

    // Check columns
    for (int j = 0; j < size; j++) {
        int sum = 0;
        for (int i = 0; i < size; i++) sum += matrix[i][j];
        if (sum != target) return 0;
    }

    // Check main diagonal
    int sum_diag1 = 0;
    for (int i = 0; i < size; i++) sum_diag1 += matrix[i][i];
    if (sum_diag1 != target) return 0;

    // Check anti-diagonal
    int sum_diag2 = 0;
    for (int i = 0; i < size; i++) sum_diag2 += matrix[i][size - 1 - i];
    if (sum_diag2 != target) return 0;

    return 1;
}

Results: ![Screenshot 2025-11-11 165416] ![Screenshot 2025-11-11 165654] ![Screenshot 2025-11-11 165709]

Task 8

#include <stdio.h>

int check(int x) {
    int used[10] = {0};
    int square = x * x;
    int cube = x * x * x;
    
    // Extract digits from square
    while (square > 0) {
        int d = square % 10;
        if (used[d]) return 0;
        used[d] = 1;
        square /= 10;
    }
    
    // Extract digits from cube
    while (cube > 0) {
        int d = cube % 10;
        if (used[d]) return 0;
        used[d] = 1;
        cube /= 10;
    }
    
    // Check that all digits 0-9 are used exactly once
    for (int i = 0; i < 10; i++) {
        if (!used[i]) return 0;
    }
    return 1;
}

int main() {
    for (int i = 12; ; i++) {
        if (check(i)) {
            printf("%d\n", i);
            return 0;
        }
    }
}

Results: ![Screenshot 2025-11-11 171702]

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.