You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
3.1 KiB
125 lines
3.1 KiB
#include <stdio.h>
|
|
#include <limits.h>
|
|
|
|
/**
|
|
* Calculate the sum of elements in an array with parameter validation
|
|
* @param arr Pointer to the array (must not be NULL)
|
|
* @param size Size of the array (must be non-negative)
|
|
* @return Sum of array elements, or 0 if parameters are invalid
|
|
*/
|
|
int safe_array_sum(int* arr, int size) {
|
|
if (arr == NULL || size <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
int sum = 0;
|
|
for (int i = 0; i < size; i++) {
|
|
sum += arr[i];
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
/**
|
|
* Calculate the average of array elements
|
|
* @param arr Pointer to the array (must not be NULL)
|
|
* @param size Size of the array (must be positive)
|
|
* @return Average value, or 0 if parameters are invalid
|
|
*/
|
|
double calculate_average(int* arr, int size) {
|
|
if (arr == NULL || size <= 0) {
|
|
return 0.0;
|
|
}
|
|
|
|
int sum = 0;
|
|
for (int i = 0; i < size; i++) {
|
|
sum += arr[i];
|
|
}
|
|
|
|
return (double)sum / size;
|
|
}
|
|
|
|
/**
|
|
* Find the minimum value in an array
|
|
* @param arr Pointer to the array (must not be NULL)
|
|
* @param size Size of the array (must be positive)
|
|
* @return Minimum value, or INT_MAX if parameters are invalid
|
|
*/
|
|
int find_minimum(int* arr, int size) {
|
|
if (arr == NULL || size <= 0) {
|
|
return INT_MAX;
|
|
}
|
|
|
|
int min = arr[0];
|
|
for (int i = 1; i < size; i++) {
|
|
if (arr[i] < min) {
|
|
min = arr[i];
|
|
}
|
|
}
|
|
return min;
|
|
}
|
|
|
|
/**
|
|
* Count occurrences of a specific value in array
|
|
* @param arr Pointer to the array (must not be NULL)
|
|
* @param size Size of the array (must be non-negative)
|
|
* @param target Value to count
|
|
* @return Number of occurrences, or 0 if parameters are invalid
|
|
*/
|
|
int count_occurrences(int* arr, int size, int target) {
|
|
if (arr == NULL || size < 0) {
|
|
return 0;
|
|
}
|
|
|
|
int count = 0;
|
|
for (int i = 0; i < size; i++) {
|
|
if (arr[i] == target) {
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/**
|
|
* Safe array copy function
|
|
* @param src Source array (must not be NULL)
|
|
* @param dest Destination array (must not be NULL)
|
|
* @param size Number of elements to copy (must be non-negative)
|
|
* @return 1 if successful, 0 if parameters are invalid
|
|
*/
|
|
int safe_array_copy(int* src, int* dest, int size) {
|
|
if (src == NULL || dest == NULL || size < 0) {
|
|
return 0;
|
|
}
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
dest[i] = src[i];
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int main() {
|
|
// Test data
|
|
int test_array[] = {5, 2, 8, 1, 9, 3};
|
|
int size = 6;
|
|
int target = 5;
|
|
|
|
// Test functions
|
|
int sum = safe_array_sum(test_array, size);
|
|
double avg = calculate_average(test_array, size);
|
|
int min = find_minimum(test_array, size);
|
|
int count = count_occurrences(test_array, size, target);
|
|
|
|
// Test array copy
|
|
int copy_array[6];
|
|
int copy_success = safe_array_copy(test_array, copy_array, size);
|
|
|
|
// Print results
|
|
printf("Array: [5, 2, 8, 1, 9, 3]\n");
|
|
printf("Sum: %d\n", sum);
|
|
printf("Average: %.2f\n", avg);
|
|
printf("Minimum: %d\n", min);
|
|
printf("Count of %d: %d\n", target, count);
|
|
printf("Copy successful: %d\n", copy_success);
|
|
|
|
return 0;
|
|
} |