i have a question

Write a function that returns a pointer to the maximum value of an array of floating-point data:
double* maximum(double a[], int a_size)
If a_size is 0, return NULL.


first of all i don't want the code for the entire program .. can someone tell me how do we create a pointer function like double*maximum(double a[],int a_size) i just remember doing double maximum dun knw how to do double*maximum.... any kind of help will be greatly appreciated ... have this due within next 16 hrs ...

this is what i have got so far : it is for vector ... i cant figure it out how to do it for a pointer array

double max(vector<double> v)
{
    int i;
    double max = v[0];
    for(i = 1; i < v.size(); i++)
    {
        if(max < v[i])
            max = v[i];
    }
    return max;
}

how would u do this for an array and instead of double max how would do it for double* max like the problem mentioned

write down the algorithm in english first..
algorithm meaning step by step what are you going to do. E.g. it's VERY clear that you have to look at every element in the array at least once, so first step in your algorithm is clear..

how would u do this for an array and instead of double max how would do it for double* max like the problem mentioned

I'm sure you understand that type of both of following variable is "double *".
double* double_arr1 ;
double double_arr2[10] ;

Because of this the subscript operator ( operator [] ) will work on both double_arr1 and double_arr2 just the same way.

So all you gotta change in the vector-wala code to make it work with double array is change the terminating condition ( i < v.size(); ) of for loop.

how would u make the function return the pointer to the maximum value of array

???????

#include <iostream>
#include <vector>
using namespace std; 

double  maxi(double a[], int a_size)
{
   double sum1 = a[0];
   double* p = a;
   for (int i = 1; i < a_size; i++)
   {
      if(sum1 < a[i])
        sum1= a[i];
   }
   return sum1;
}

int main()
{
   double data[] = { 1, 4, 9, 16, 25 };
   cout <<" highesht array is "<< maxi(data, 5) << "\n";
   return 0;
}

this is what i have got so far ... now the following questions:

how do i set the pointer to maximum value of array?? and

how do i use the double*maximum( double a[],int a_size) from what i have ????

Pointers can be tricky until you've used them a while. Basically a pointer is an address of something. There are three ways you can get the address of something:

You can use
1) pointers:
int * a; //means the address of an int variable is held in a

2) the address of operator
int b ;
&b; //means the address of b
and with arrays:
int b2[4];
&b2[1]; //means the address of the second element in b2;

3) the name of an array is used as a pointer to the first element in the array
int array[6];
array; //means the address of the first element of array, ie &array[0] == array.

In your case you know how to get the max but you want to store the address of the max and return it to the calling function. Since the address of the max will be an address in the array you sent the called function and arraya are always sent by reference from the calling function, the address you return should remain in scope.

ok but how do we do the double*maxi rather than just double maxi ... i am lost big time on that one

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.