In statics when a set of values is sorted in ascending or descending order, its median is in the middle value. If the set contains an even number of values, the, median is the mean, average, of the two middle values. Write a function that accepts as arguments the following

A)An Array of integers
B)An integer that indicates the number of elements in the array
The function should determine the median of the array. This value should be returned as a double. (Assume the values in the array are alreadysorted.)
Demonstrate your pointers prowess by using pointer motation instead of aarray notation in this function.
#include <iostream>
using namespace std;

int main ()
{
double median(int *movies, int size);
{
int x, y, tmp;

for (x = 0; x < int size-1; x++)
{
for (y = x + 1; y < size; y++)
{
if ((*(movies + x)) > (*(movies+y)))
{
tmp = *(movies + x);
*(movies + x) = *(movies + y);
*(movies + y) = tmp;
}
}
}
if (size % 2 == 1)
{

return ((double) (*(movies + (size / 2))));
}
else
{

return ((double) ((*(movies + (size / 2) - 1)) + (*(movies + (size / 2))) / 2.0);
}
}
}

I don't know about the detail. But the most significant error here is attempting to define one function inside another.

Move the definition of `median()` outside function `main()`.

You can of course call the function from main(), after you've defined suitable variables to pass as parameters.
closed account (3qX21hU5)
Hey Andrew first please use code tags when posting code in the forums (Hint: the <> off to the right when replying lol I actually got a macro for posting this now) it helps people read your code since it will be formated properly.

Now for the problems that I seen. I just did a quick scan so I probably missed a couple.

1) Your putting your functions inside your `int main() `. The functions should be outside of it. If they are in the same .cpp file as your `int main()` put the definitions below main and have the declarations above main. Here is a example

 ``1234567891011`` ``````double median(int*, int); // Declaration int main () { // Your main code here } double median(int *movies, int size); // Definition { // Your function code here }``````

Thats the main thing that I saw, and a lot of other things that were off but I don't have time to comment on each one so I will just give you a overview.

You need help on how functions work. Check out this link www.cplusplus.com/doc/tutorial/functions/

You need help on how for loops work, you forgot to declare a type in `for (y = x + 1; y < size; y++)` and the other ones. Check out this link www.cplusplus.com/doc/tutorial/control/

And your missing a ) in this line `return ((double) ((*(movies + (size / 2) - 1)) + (*(movies + (size / 2))) / 2.0));`.

Now that should fix all the error but I'm not sure if it will run as promised since I didn't have time to look to in detail at it. Once I get off work I can help more if you need it just send a PM or post here.

Thank You for the pointers. They were very helpful but I'm still stuck on how to fix it. Im really struggling in this class. I do not think i was born to program haha.
closed account (3qX21hU5)
Starting out is always hard but keep with it, because it will get a little easier.

What are you stuck on exactly?
How to correct what I have?
Topic archived. No new replies allowed.