Array notation and Pointer notation

I have to find the median by using array and pointer.
I have to use pointer notation and not array notation.
There is my work with array notation, how can I change it into pointer notation?
Also, can I have some examples of array notation and pointer notation?
Because I don't really know the differences.
Is there any way to change line(25) to pointer form?

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
# include <iostream>
using namespace std;

double median(int arr[], int size);

int main()
{
	int arr[10]={1,2,3,4,5,6,7,8,9,10};   // even
	int arry[9]={1,2,3,4,5,6,7,8,9};      // odd 
	double oddMedian;
	double evenMedian;
	evenMedian = median(arr,10);
	oddMedian = median(arry,9);
	cout << "The median of odd number array is: " << oddMedian << endl;
	cout << "The median of even number array is: " << evenMedian << endl;
	system("pause");
	return 0;
}

double median(int arr[],int size)
{
	double finalMedian;
	if(size % 2 != 0)
	{
		finalMedian = arr[size/2];
	}
	else
	{
		finalMedian = (arr[size/2]+arr[size/2+1])/2.0;
	}
	return finalMedian;
}
Last edited on
Is there any way to change line(25) to pointer form?


1
2
3
4
5
6
7
8
9
//pass the array as functions argument exactly like you are doing now but this time it will 
//implicitly convert to pointer to its 1st element
double median(int* arr,int size)      
{
                //than you can add value to this pointer to 1st element to travel to specific position
                //the result will be pointer to that new position and than you need to dereference it to get the value it points to.
                
		finalMedian = *(arr + size/2);  
}
Last edited on
Topic archived. No new replies allowed.