Does anyone know how to change the code below from enthusiasticgeek to allow for user input (e.g. Input n, and then n integers)? Output these n integers and the median. n is odd and positive and is less than 1 million.
Code pasted from enthusiasticgeek.com:
#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 6
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, ELEMENTS, sizeof(int), compare);
for (n=0; n<ELEMENTS; n++)
{ printf ("%d ",values[n]); }
printf ("median=%d ",values[ELEMENTS/2]);
return 0;
}
use:
instead of just hardcoding the values for the values array.
Make sure you either dynamically declare the array, or statically make it large enough to accept the number of integers you want, 1,000,000 but thats a ton of memory. No one’s going to take that much time to enter that many numbers manually. Probably safe with declaring it at 30 tops.
Or if you’re planning to use the “define elements” part, you only need to declare the values array to be 6.