I have written a successful piece of code that takes a random assortment of numbers and puts them in numerical order and then finds the mean and median of the list. My professor wants us to make it as fast as possible and i think i have a few things that could be cleaned up, and i still don't fully understand C so any help would be grateful.
Edit: Basically all i need is some help finding where i can cut down on processes to make the run time faster.
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void GetValues(int x[], int *pN);
void SortValues(int x[], int n);
double GetMean(int x[], int n);
double GetMedian(int x[], int n);
void main()
{
int a[100],b[100],pN,i;
double mean,median;
do
{
scanf("");
GetValues(a,&pN); // get values and put them into "a" array
if(pN!=0)
{
for(i=0;i<pN;i++)
{
b[i]=a[i]; // is there a better way of doing this?? i feel like it adds alot of unneccasry steps
}
SortValues(b,pN); //sort values using the "b" array
mean=GetMean(b,pN); //get mean of "b" array
median=GetMedian(b,pN); //get median
printf("\n Original Sorted\n"); //print everything
for(i=0;i<pN;i++)
{
printf("%13d %11d\n",a[i],b[i]);
}
printf("\nMean:%11.2lf\n",mean);
printf("Median:%9.2lf\n\n",median);
}
}
while(pN!=0);
}
//GetValues
void GetValues(int x[], int *pN)
{
int size,i;
printf("Enter number of integer values: ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
printf("Enter Value %d: ",i+1);
scanf("%d",&x[i]);
}
*pN=size;
return;
}
//SortValue
void SortValues(int x[], int n)
{
int i,j,t;
for (i=0; i<n; i++)
{
for (j=0; j<(n-1); j++)
{
if (x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
return;
}
//GetMean
double GetMean(int x[], int n)
{
int i;
double sum=0;
for(i=0;i<n;i++)
{
sum=sum+x[i];
}
sum=sum/n;
return sum;
}
//GetMedian
double GetMedian(int x[], int n)
{
int j,k,k1;
double median;
j=n%2;
if(j==0)
{
k=n/2;
k1=k-1;
median=x[k]+x[k1];
median=median/2;
}
else
{
k=n/2;
median=x[k];
}
return median;
}