array.
c: Source file implementing all algorithms
#include <stdio.h>
#include "myarray.h"
//1D arrays:
//1_Analysis functions:
//PRINTING ARRAYS:
void print_1d(double arr[], int size) {
printf("Your array is: ");
for (int i = 0; i < size; i++) {
printf("%lf ", arr[i]);
printf("\n");
//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];
array.c: Source file implementing all algorithms
}
return max;
//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];
array.c: Source file implementing all algorithms
}
//VARIANCE:
double variance_1d(double arr[], int size) {
double mean = average_1d(arr, size), var = 0;
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]) {
array.c: Source file implementing all algorithms
min_idx = j;
double temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
//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;
}
array.c: Source file implementing all algorithms
}
double temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
quick_sort(arr, low, i);
quick_sort(arr, i + 2, high);
//3_Search algorithms:
//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) {
array.c: Source file implementing all algorithms
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:
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;
}
array.c: Source file implementing all algorithms
// 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];
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);
// Find Maximum in matrice:
double max_2d(int rows, int cols, double arr[rows][cols]) {
if (rows == 0 || cols == 0){
return 0;
array.c: Source file implementing all algorithms
}
double max = arr[0][0];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
return max;
// Find Minimum in matrice:
double min_2d(int rows, int cols, double arr[rows][cols]) {
if (rows == 0 || cols == 0){
return 0;
double min = arr[0][0];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] < min) {
min = arr[i][j];
return min;
// 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
array.c: Source file implementing all algorithms
if (x == 'H') { // Horizontal flip
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrice2[i][j] = matrice[i][cols - j - 1];
} else if (x == 'V') { // Vertical flip
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrice2[i][j] = matrice[rows - i - 1][j];
} else {
printf("Invalid input; use 'H' for horizontal flip or 'V' for vertical flip\n");
//basic arithmetic operations:
// 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;
// Validate the index
if ((type == 'r' && (index < 0 || index >= rows)) || (type == 'c' && (index < 0 || index >= cols))) {
printf("Error\n");
return 0; // Return 0 for invalid input
if (type == 'r') { // Row operation
for (int j = 0; j < cols; j++) {
result += matrice[index][j];
array.c: Source file implementing all algorithms
}
} else if (type == 'c') { // Column operation
for (int i = 0; i < rows; i++) {
result += matrice[i][index];
} else {
printf("Error: Invalid type. Use 'r' for row or 'c' for column.\n");
return 0; // Return 0 for invalid type
return result;
// Function to perform subtraction on a specific row or column
double Subtraction_2d(double matrice[][100], int rows, int cols, int index, char type) {
double result = 0;
// Validate the index
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
if (type == 'r') { // Row operation
result = matrice[index][0];
for (int j = 1; j < cols; j++) {
result -= matrice[index][j];
} else if (type == 'c') { // Column operation
result = matrice[0][index];
for (int i = 1; i < rows; i++) {
result -= matrice[i][index];
}
array.c: Source file implementing all algorithms
} else {
printf("Error: Invalid type. Use 'r' for row or 'c' for column.\n");
return 0; // Return 0 for invalid type
return result;
// Function to perform multiplication on a specific row or column
double Multiplication_2d(double matrice[][100], int rows, int cols, int index, char type) {
double result = 1;
// Validate the index
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
if (type == 'r') { // Row operation
for (int j = 0; j < cols; j++) {
result *= matrice[index][j];
} else if (type == 'c') { // Column operation
for (int i = 0; i < rows; i++) {
result *= matrice[i][index];
} else {
printf("Error: Invalid type. Use 'r' for row or 'c' for column.\n");
return 0; // Return 0 for invalid type
return result;
array.c: Source file implementing all algorithms
}
// Function to perform division on a specific row or column
double Division_2d(double matrice[][100], int rows, int cols, int index, char type) {
double 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
if (type == 'r') { // Row operation
result = matrice[index][0];
for (int j = 1; j < cols; j++) {
if (matrice[index][j] != 0) {
result /= matrice[index][j];
} else {
printf("Error: Division by zero in row %d at column %d.\n", index, j);
return 0; // Indicate division by zero error
} else if (type == 'c') { // Column operation
result = matrice[0][index];
for (int i = 1; i < rows; i++) {
if (matrice[i][index] != 0) {
result /= matrice[i][index];
} else {
printf("Error: Division by zero in column %d at row %d.\n", index, i);
return 0; // Indicate division by zero error
}
array.c: Source file implementing all algorithms
} else {
printf("Error: Invalid type. Use 'r' for row or 'c' for column.\n");
return 0; // Return 0 for invalid type
return result;
// Function to print the result of the operation
void printingresult(char operation, double theresult, double result2) {
switch (operation) {
case 'a':
printf("Result of addition: %d\n", theresult);
break;
case 's':
printf("Result of subtraction: %d\n", theresult);
break;
case 'm':
printf("Result of multiplication: %d\n", theresult);
break;
case 'd':
printf("Result of division: %.2f\n", theresult);
break;
default:
printf("Invalid operation.\n");
break;
// Function to handle the operation and user input
void Operation_perform_2d(double matrice[][100], int index, int rows, int cols, char type, char operation) {
double theresult = 0; // Primary result variable
array.c: Source file implementing all algorithms
double result2 = 0; // Secondary result variable for division (if needed)
// Validate input dimensions
if (index < 0 || (type == 'r' && index >= rows) || (type == 'c' && index >= cols)) {
printf("Invalid index.\n");
return;
// Perform operation based on the case
switch (operation) {
case 'a':
theresult = addition_2d(matrice, rows, cols, index, type);
break;
case 's':
theresult = Subtraction_2d(matrice, rows, cols, index, type);
break;
case 'm':
theresult = Multiplication_2d(matrice, rows, cols, index, type);
break;
case 'd':
result2 = Division_2d(matrice, rows, cols, index, type);
break;
default:
printf("Invalid operation.\n");
return;
// Print the result(s)
if (operation == 'd') {
printingresult(operation, theresult, result2);
} else {
printingresult(operation, theresult, 0); // Pass 0 or a dummy value for `result2`