c++ please
1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. The function has the following prototype.
int maximum ( int *p [ ], int n);
#include <iostream>
#include <fstream>
using namespace std;
int maximum(int *p[],int n){
int max = *p[0]; // assign variable 0 to be max
for(int i = 1; i < n ; i++){ // find max in index 1
to n
if(max < *p[i]){
max =
*p[i];
}
}
return max;
}
int main()
{
int n = 9;
int var[n] = {10, 100, 200, 50, 30, 40, 2, 47, 80}; // create
array
int i, *ptr[n]; // create array of pointer
for ( i = 0; i < n; i++) {
ptr[i] = &var[i]; /* assign the address of integer. */
}
cout<<"Maximum is: "<<maximum(ptr,n);
}
Get Answers For Free
Most questions answered within 1 hours.