median function with twogiven arrays!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>

using namespace std;

double getMedian(int*, int);
void printArray(int[], int);



int main()
{
	const int firstSize = 3;
	const int secondSize = 5;

	int firstArray[firstSize] = { 1,3,7 };
	int *pList = firstArray;
	cout << "The values of array one are: ";
	printArray{firstArray,firstSize };
	cout << "The median of array one is: " << getMedian(pList, firstSize)
		<< endl;

	int secondArray[secondSize] = { 2, 4, 6, 8, 105 };
	*pList = secondArray;
	cout << "The values of array two are: ";
	printArray{secondArray,secondSize };
	cout << "The median of array one is: " << getMedian(pList, secondSize)
		<< endl;

	return 0;
}

double getMedian(int *array, int size)
{
	//getmedian
	int mid = (size - 1) / 2;
	double med;

	if (size % 2 == 0)
	{
		med = (*(array + mid) + *(array + (mid + 1))) / 2;
	}
	else
	{
		med = *(array + mid);
	}
	return med;



}

void printArray(int arg[], int length)
{
	for (int n = 0; n<length; ++n)
		cout << arg[n] << ' ';
	cout << '\n';
}



// i'm trying to display and determine the median for each of these two given arrays.so one with three elements and one with four elements. am i supposed to devise two separate functions for each array?
Last edited on
First, please use code tags. See http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post.

Second, what is the question?
Topic archived. No new replies allowed.