This is a code that gets the median of an array of integers. The program is running however it only returns 0 as the median for both even and odd number of elements.
CODE
#include<stdio.h>
#include<stdlib.h>
int i;
int x[100];
int length;
int s;
int q;
int temp;
int go;
int n;
float get_median (int [], int);
int main(){
int num,length, med;
printf("How many numbers do you want?");
scanf("%d",&length);
printf("\nEnter your numbers:\n");
scanf("%d",&num);
for(i=1;i<length;i++)
scanf("%d",&x[i]);
for (i=1;i<=length-1;i++){
for (s=1;s<=length-1;s++){
if (x[i]>x[i+1]){
temp=x[i];
x[i]=x[i+1];
temp=x[i+1];
}
else continue;
}
}
printf("The median is :%.2f",get_median(x, length) );
return 0;
}
float get_median(int a[],int b){
int eq;
for (n=1;n<b-1;n++)
{
for (i=1;i<b-1;i++)
{
if (a[i]>a[i+1])
{
eq=a[i];
a[i]=a[i+1];
a[i+1]= eq;
}
}
}
if (b%2==0)
{
return (float)(a[b/2]+a[b/2+1])/2;
}
else
{
return (float)a[b/2];
}
}
anyone who knows how I could fix this?