# Untitled

unknown
plain_text
2 months ago
1.0 kB
3
Indexable
Never
```#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>

using namespace std;

// Q1.

// a) Write a function called max_array(p,n) where
// p is a pointer to a 1D array of doubles and n is the number
// of elements in the array.  The function returns
// the maximum element of the array.

double max_array(double *p, int n);

// b) Write a main() to illustrate the function from part a) for
// a 1D array with five elements given by: 1.1, 2, -3, 6, 4.5

int main()
{
int n = 5;
double A[5] = { 1.1, 2, -3, 6, 4.5 };
double max_A;

max_A = max_array(A,n);

cout << "\nmax_A = " << max_A;

cout << "\n\ndone.\n";
getchar();

return 0;
}

double max_array(double *p, int n)
// the function sets p = A for this example
{
int i;
double ans;

ans = -1.0e100;
//	ans = p[0]; // alternative
for(i=0;i<n;i++) {
if( p[i] > ans ) ans = p[i];
// alternative using pointer notation
//		if( *(p + i) > ans ) ans = *(p + i);
}

return ans;
}```