# Problem Using Two Functions with Pointers

Page 1 of 1

## 2 Replies - 1819 Views - Last Post: 19 November 2012 - 02:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=300858&amp;s=d668254cde9c3adef33a4bfb7799fcc8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Walshy

Reputation: 0
• Posts: 2
• Joined: 19-November 12

# Problem Using Two Functions with Pointers

Posted 19 November 2012 - 01:39 PM

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:
```double* median (int array[ ], int size)
{
int size, *median;
if(size mod 2 = 0)
((array[size]/2) - 1) + ((array[size])/2) / 2 =  median;

if(size mod 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.
```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 ];

```

Is This A Good Question/Topic? 0

## Replies To: Problem Using Two Functions with Pointers

### #2 Walshy

Reputation: 0
• Posts: 2
• Joined: 19-November 12

## Re: Problem Using Two Functions with Pointers

Posted 19 November 2012 - 01:41 PM

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:
```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.
```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 ];

```

### #3 snoopy11

• Engineering ● Software

Reputation: 983
• Posts: 3,029
• Joined: 20-March 10

## Re: Problem Using Two Functions with Pointers

Posted 19 November 2012 - 02:43 PM

Hi,

Quote

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:

Answer No you're not getting it...

example code follows

we have a function....

lets call it average

it takes an array of integers and works out the average and it includes the size of the array as a separate
int value..

it returns the average of the array of integers that is its function as a float.

declaration

float average(int* array, int size);

implementation
float average(int* array, int size)
{
float holder = 0;

for(int x=0; x<size; x++)

{
holder = holder + array[x];
}
holder= holder/size;

return holder;
}

Full code Example.

```#include <iostream>

using namespace std;
float average(int* array, int size);

int main()
{
float av;
int myarray[10]= {1,2,3,4,5,6,7,8,9,10};
av = average(myarray, 10);
cout << "The average is " << av << endl;
return 0;
}

float average(int* array, int size)
{
float holder = 0.0f;

for(int x=0; x<size; x++)

{
holder = holder + array[x];
}
holder= holder/size;

return holder;
}

```

Best Wishes

Snoopy.