2 Replies - 977 Views - Last Post: 19 November 2012 - 02:43 PM Rate Topic: -----

#1 Walshy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ];



Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,212
  • 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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1