Problem with Two Functions Using Arrays and Pointers

Function a:
This function must accept 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 already sorted.)
Demonstrate your pointer prowess by using pointer notation instead of array notation in this function.
Question: I am unsure if I am using the array and pointer notation correctly, any help here is appreciated. Note - It is assumed the array is already sorted in ascending order.
My code for Function a:
1
2
3
4
5
6
7
8
9
10
11
double* median (int array[ ], int size)
{
      int size, *median;
      if(size % 2 = 0)
         ((array[size]/2) - 1) + ((array[size])/2) / 2 =  median;

      if(size % 2 = 1)
          (array[size]) / 2 = median;

      return median;
}


Function b: Write a function that accepts an int array and the array's size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a
pointer to the new array.
Question: I cannot figure out how to initialize the unused elements of the second array with 0. Also, I am unsure of my syntax here using arrays. Finally, how can pointers be implemented into this function? Thanks again.
1
2
3
4
5
6
7
int expander (int arr[ ], int arraySize)
{
     int arr[ ], arraySize, newArray[ ], newArraySize;
     newArraySize = arraySize * 2;
     for (i = 0; i <= arraySize - 1; i++)
           arr[ i ] = newArray[ i ];
1
2
3
double* median (int array[ ], int size)
{
      int size, *median;

No no no, you already HAVE an int named size. It's one of the parameters being passed in. Also, you're just asking for trouble giving variables the same name as functions.

array[size] does not exist. It's off the end of the array. I don't think you understand how to use arrays. Also, the instructions say to use pointers rather that array notation.

I cannot figure out how to initialize the unused elements of the second array with 0.

Here is an example showing how to set one element in an array to zero.
array[4] = 0;
Here is an example showing how to set a range of elements in an array to zero, using pointer notation rather than array notation.
1
2
3
4
for (int i=startPos; i<endPos; i++)
{
  *(array+i) = 0;
}
Topic archived. No new replies allowed.