C Programming: Array Manipulation and Algorithm Implementation
Memory Layout of Arrays
Examining the memory allocation and addressing of one-dimensional and two-dimensional arrays.
#include <stdio.h>
#define DIM1 4
#define DIM2 2
void display_1d_array(int arr[], int len) {
printf("Array size in bytes: %lu\n", sizeof(arr));
for (int idx = 0; idx < len; ++idx) {
printf("[%p]: %d\n", (void*)&arr[idx], arr[idx]);
}
printf("Array name address: %p\n", (void*)arr);
}
void display_2d_array(int mat[][DIM1], int rows, int cols) {
printf("2D array size in bytes: %lu\n", sizeof(mat));
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
printf("[%p]: %d\n", (void*)&mat[r][c], mat[r][c]);
}
}
printf("Array name: %p\n", (void*)mat);
printf("First row: %p\n", (void*)mat[0]);
printf("Second row: %p\n", (void*)mat[1]);
}
int main() {
int single_dim[DIM1] = {10, 20, 30, 40};
int double_dim[DIM2][DIM1] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
display_1d_array(single_dim, DIM1);
display_2d_array(double_dim, DIM2, DIM1);
return 0;
}
Note: The sizeof operator on an array paarmeter in a function returns the size of a pointer, not the array.
Function with Array Parameters
Passing arrays to functions for processing.
Compute Trimmed Mean
Calcluate the average of an array after removing the maximum and minimum values.
#include <stdio.h>
#define MAX_SIZE 100
void read_values(int arr[], int count) {
for (int i = 0; i < count; ++i) {
scanf("%d", &arr[i]);
}
}
double calculate_trimmed_average(int data[], int size) {
if (size <= 2) return 0.0;
int max_val = data[0];
int min_val = data[0];
double total = 0.0;
for (int i = 0; i < size; ++i) {
total += data[i];
if (data[i] > max_val) max_val = data[i];
if (data[i] < min_val) min_val = data[i];
}
return (total - max_val - min_val) / (size - 2);
}
int main() {
int dataset[MAX_SIZE];
int num_elements;
while (scanf("%d", &num_elements) != EOF) {
read_values(dataset, num_elements);
double result = calculate_trimmed_average(dataset, num_elements);
printf("Result: %.2f\n", result);
}
return 0;
}
Matrix Initialization and Printing
Fill a square matrix with a given value and print its contents.
#include <stdio.h>
#define MAX_DIM 100
void fill_matrix(int mat[][MAX_DIM], int dim, int val) {
for (int r = 0; r < dim; ++r) {
for (int c = 0; c < dim; ++c) {
mat[r][c] = val;
}
}
}
void print_matrix(int mat[][MAX_DIM], int dim) {
for (int r = 0; r < dim; ++r) {
for (int c = 0; c < dim; ++c) {
printf("%d ", mat[r][c]);
}
printf("\n");
}
}
Median Calculation
Sort an array and find its median value.
#include <stdio.h>
#define MAX_LEN 100
void get_input(int arr[], int len) {
for (int i = 0; i < len; ++i) {
scanf("%d", &arr[i]);
}
}
double find_median(int arr[], int len) {
for (int i = 0; i < len - 1; ++i) {
for (int j = 0; j < len - i - 1; ++j) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
if (len % 2 == 1) {
return (double)arr[len/2];
} else {
return ((double)arr[len/2 - 1] + (double)arr[len/2]) / 2.0;
}
}
Matrix Operations
Column Cyclic Shift
Shift all columns of a square matrix to the right by one position, with the last column wrapping around to the first.
#include <stdio.h>
#define MAX_SZ 100
void populate_matrix(int mat[][MAX_SZ], int sz) {
for (int i = 0; i < sz; ++i) {
for (int j = 0; j < sz; ++j) {
scanf("%d", &mat[i][j]);
}
}
}
void display_matrix(int mat[][MAX_SZ], int sz) {
for (int i = 0; i < sz; ++i) {
for (int j = 0; j < sz; ++j) {
printf("%4d", mat[i][j]);
}
printf("\n");
}
}
void shift_columns_right(int mat[][MAX_SZ], int sz) {
int temp_col[MAX_SZ];
for (int i = 0; i < sz; ++i) {
temp_col[i] = mat[i][sz-1];
}
for (int j = sz-1; j > 0; --j) {
for (int i = 0; i < sz; ++i) {
mat[i][j] = mat[i][j-1];
}
}
for (int i = 0; i < sz; ++i) {
mat[i][0] = temp_col[i];
}
}
Number Base Conversion
Convert a decimal integer to binary, octal, or hexadecimal representation.
#include <stdio.h>
#define BUF_SIZE 100
void convert_base(int decimal, int base) {
if (decimal == 0) {
printf("0");
return;
}
char buffer[BUF_SIZE];
int idx = 0;
int num = decimal;
while (num > 0) {
int remainder = num % base;
if (base == 16 && remainder >= 10) {
buffer[idx++] = 'A' + remainder - 10;
} else {
buffer[idx++] = '0' + remainder;
}
num /= base;
}
for (int j = idx - 1; j >= 0; --j) {
printf("%c", buffer[j]);
}
}