# Thread: I'd appreciate any help I could get with this calculation of the median.

1. ## I'd appreciate any help I could get with this calculation of the median.

I'm working on a function that performs both an average output and the median of string of numbers. To perform the median calculation first I must be able to sort the functions properly, but I am having problems with it so far. This is what I have. Thanks everyone.

Code:
```#include <stdio.h>

temp bubble_sort(int start, int end)
int main (){

char buffer[512]; /* see previous program */
int entries, current;
double input, average=0;
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%d", &entries);
for (current=0; current<entries; ++current){

printf(" Please enter number %d: ", current+1);

fgets(buffer, sizeof(buffer), stdin);

sscanf(buffer, "%lf", &input);

average= ((average*current) + input)/(current+1); /* sufficient for
small sets */
/* printf("Current average: %g\n", average); */ /* debug */
getchar();
}

printf("Average = %g\n", average);

return 0;
getchar();
}
temp bubble_sort(int i, int n, int y, int x) {

for (int i =0; i < n; i++)
{

int index_of_min = x;
for (int y=i; y<i; y++)
{
if( array [index_of_min] >array [y] )
{
index_of_min = y;
}
}
int temp array =array[x];
array [x] = array [index_of_min];
array [index_of_min] = temp;

getchar();
}

}```

2. What is temp?

3. And why aren't you passing the array into your function to be sorted? And why are you passing in a loop counter? And to sort, you need to do more than "find the smallest number and put it in position 0", as generally speaking there are a lot more positions than that in your array.

4. Prelude has a great tutorial that should get you going with sorting.

5. Couldn't you just do something simple without a function, like:

Code:
```#include <stdio.h>

int main ()
{
float average, number, count = -1.0, total = 0.0;

do {
printf ("Enter number. Enter 0 to compute.\n\n");
scanf ("%f", &number);
total += number;
count ++;
printf ("\n");
} while (number != 0);

average = total / count;

printf ("The average of the numbers entered is: %.2f\n", average);

return 0;
}```
Sorry if this is more simplistic that what you are looking for, I'm fairly new to C programming, but I ran this and it finds the median just fine.

6. Unfortunately, average and median are actually two different things.

7. @Sanglant
1. We have a policy against handing out code here, since it keeps people from actually learning. Read the forum guidelines.
2. Your code does not find the median, it finds the mean. Look up mean, median and mode on Wikipedia.
3. You completely ignored the OPs actual question which was about sorting.
4. Using functions is a really good idea, and makes sense when calculating multiple statistics, like mean and median.

We love having more people around to help out and answer questions for people, but please put more thought into your replies.

Bubble sort - Wikipedia, the free encyclopedia

Tim S.

9. Originally Posted by anduril462
@Sanglant
[list="1"][*]We have a policy against handing out code here, since it keeps people from actually learning. Read the forum guidelines.
That's false (read the forum guidelines). There is no policy against posting code (it is a programming forum), there's a policy discouraging people who ask others to do their homework for them. Providing examples or throwing out concrete ideas does not "keep people from learning".

Catching mistakes (such as confusing median and average) in posted code is always laudable tho.

10. You're right, it's not stated explicitly, and I could have phrased that better. But posting code for somebody else's homework without any effort to teach or explain violates the spirit of the homework policy in my opinion. Somebody coming around handing out answers to homework is the flip side to asking for people to do it for you. Asked for or not, the OP will likely read the post with the solution and lose the opportunity to think through a solution on their own. That problem solving aspect is a huge part of what is taught in programming classes and it's often harder to think up your own, unique solution when you're staring at somebody else's working solution. I'm not against posting working code, but I have seen more than one person join this forum and run around handing out homework solutions, and I was mostly interested in discouraging Sanglant from doing that.