Include
Include
//SUM:
double sum_1d(double arr[], int size) {
double total = 0;
for (int i = 0; i < size; i++) {
total += arr[i];
}
return total;
}
//AVERAGE:
double average_1d(double arr[], int size) {
if (size == 0) return 0;
return sum_1d(arr, size) / size;
}
//MAXIMUM:
double max_1d(double arr[], int size) {
if (size == 0) return 0;
double max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max) max = arr[i];
}
return max;
}
array.c: Source file implementing all algorithms
//MINIMUM:
double min_1d(double arr[], int size) {
if (size == 0) return 0;
double min = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < min) min = arr[i];
}
return min;
}
//REVERSE:
void reverse_1d(double arr[], int size) {
for (int i = 0; i < size / 2; i++) {
double temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
}
//ROTATION:
void rotate_1d(double arr[], int size, int positions) {
positions %= size;
if (positions == 0) return;
double temp[size];
for (int i = 0; i < size; i++) {
temp[(i + positions) % size] = arr[i];
}
for (int i = 0; i < size; i++) {
arr[i] = temp[i];
}
}
//VARIANCE:
double variance_1d(double arr[], int size) {
double mean = average_1d(arr, size), var = 0;
array.c: Source file implementing all algorithms
for (int i = 0; i < size; i++) {
var += (arr[i] - mean) * (arr[i] - mean);
}
return var / size;
}
//2_Sorting algorithms:
//BUBBLE SORT:
void bubble_sort(double arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
double temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
//SELECTION SORT:
void selection_sort(double arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
double temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
array.c: Source file implementing all algorithms
//INSERTION SORT:
void insertion_sort(double arr[], int size) {
for (int i = 1; i < size; i++) {
double key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
//QUICK SORT:
void quick_sort(double arr[], int low, int high) {
if (low < high) {
double pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
double temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
double temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
//BINARY SEARCH:
int binary_search_1d(double arr[], int low, int high, double target) {
if (low > high) {
return -1; // Element not found
}
int mid = low + (high - low) / 2; // mid should be an integer
if (arr[mid] == target) {
return mid; // Element found
}
if (arr[mid] > target) {
return binary_search_1d(arr, low, mid - 1, target); // Search in left half
} else {
return binary_search_1d(arr, mid + 1, high, target); // Search in right half
}
}
//LINEAR SEARCH:
int linear_search_1d(double arr[], int size, double but, int i) {
if (i >= size) {
return -1; // Return -1 if the target is not found
}
if (arr[i] == but) {
return i; // Return the index of the target if found
}
return linear_search_1d(arr, size,but, i + 1); // Recurse to the next element
}
//2D arrays:
//tronspose a matrice:
array.c: Source file implementing all algorithms
void transpose_2d(double matrice[][100], double transposed[][100], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed[j][i] = matrice[i][j];
}
}
}
// Sum of matrice:
double sum_2d(int rows, int cols, double arr[rows][cols]) {
double total = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
total += arr[i][j];
}
}
return total;
}
// Sub of matrice:
double sub_2d(int rows, int cols, double arr[rows][cols]) {
double total = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
total -= arr[i][j];
}
}
return total;
}
//multipication of matrice:
double mult_2d(int rows, int cols, double arr[rows][cols]) {
double total = 1;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
total *= arr[i][j];
}
array.c: Source file implementing all algorithms
}
return total;
}
// Average of matrice:
double average_2d(int rows, int cols, double arr[rows][cols]) {
if (rows == 0 || cols == 0){
return 0;
}
return (double)sum_2d(rows, cols, arr) / (rows * cols);
}
// mirror a matrice:
void mirror_2d(double matrice[][100], double matrice2[][100], int rows, int cols) {
char x;
printf("Enter 'H' for horizontal flip or 'V' for vertical flip: ");
scanf(" %c", &x); // Read user input for flip type
// the user choose whether he wants to perform the operations on the rows or the columns
double addition_2d(double matrice[][100], int rows, int cols, int index, char type) {
double result = 0;
array.c: Source file implementing all algorithms
return result;
}
return result;
}
return result;
}
// Validate inputs
if ((type == 'r' && (index < 0 || index >= rows)) ||
(type == 'c' && (index < 0 || index >= cols))) {
printf("Error: Index out of bounds.\n");
return 0; // Return 0 for invalid input
}
return result;
}
break;
default:
printf("Invalid operation.\n");
break;
}
}