Average (Mean), Median
Average of all Array Elements
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(float *array, int current_size);
float summation_array(float *array, int current_size);
float average_array(float *array, int current_size);
int main(){
float a[TOTAL_SIZE] = {10, 20, 30, 40, 50.5};
int current_size = 5;
print_array(a, current_size);
printf("Summation: %.2f\n", summation_array(a, current_size));
printf("Average: %.2f\n", average_array(a, current_size));
return 0;
}
float summation_array(float *array, int current_size){
int i = 0;
float sum = 0.0;
for(i=0; i<current_size; i++){
sum += array[i]; //sum = sum + a[i];
}
return sum;
}
float average_array(float *array, int current_size){
return summation_array(array, current_size) / (float)current_size;
}
void print_array(float *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}
else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%.2f ", array[i]);
}
printf("\n");
}
}
Median
#include<stdio.h>
#define TOTAL_SIZE 10
void print_array(float *array, int current_size);
float medaian(float *array, int current_size);
int main(){
float a[TOTAL_SIZE] = {10, 20, 30, 40, 50.5};
int current_size = 5;
print_array(a, current_size);
printf("Midean: %.2f\n", medaian(a, current_size));
return 0;
}
float medaian(float *array, int current_size){
float median = 0.0;
if(current_size%2==0){
median = (array[current_size/2 - 1] + array[current_size/2])/2.0;
}else{
median = array[current_size/2];
}
return median;
}
void print_array(float *array, int current_size){
int i;
if(current_size==0){
printf("Array is Empty\n");
}
else{
printf("Array Elements are: ");
for(i=0; i<current_size; i++){
printf("%.2f ", array[i]);
}
printf("\n");
}
}
Last updated